CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    another float/int/for question

    Hello I have a loop like this:

    int j;
    for(float i = 0.0f, j = 0; i <= 1.0f; i=i+(1.0f/CDF_RESOLUTION, j++))
    intervalEdges.push_back(j);

    For some reason when I look at "j" value it is represented as a floating point number.

    Any idea why? Is this the best way to try to do something like this??

    Thanks!

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: another float/int/for question

    'for' begins another scope, allowing you to hide outer-scope variables.
    'float i = 0.0f, j = 0;' defines two floats, i and j.

    solution: set j = 0 before the loop:
    Code:
    int j = 0;
    for(float i = 0.0f; i <= 1.0f; i=i+(1.0f/CDF_RESOLUTION, j++))
    Jeff

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    Re: another float/int/for question

    Did you put the brace in the right place? I guess
    Code:
    for(float i = 0.0f; i <= 1.0f; i=i+(1.0f/CDF_RESOLUTION, j++))
    should be
    Code:
    for(float i = 0.0f; i <= 1.0f; i=i+(1.0f/CDF_RESOLUTION), j++)
    Besides, you don't need the loop, and intervalEdges container perhaps:
    you need to know what is the upper-bound of j right? [0, x], need to know x.
    set a = 1.0f/CDF_RESOLUTION
    x = int((1.0f - 0.0f)/a);
    Last edited by AvDav; December 13th, 2006 at 05:04 PM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: another float/int/for question

    Quote Originally Posted by lab1
    Hello I have a loop like this:

    int j;
    for(float i = 0.0f, j = 0; i <= 1.0f; i=i+(1.0f/CDF_RESOLUTION, j++))
    intervalEdges.push_back(j);

    For some reason when I look at "j" value it is represented as a floating point number.

    Any idea why? Is this the best way to try to do something like this??

    Thanks!
    This is not part of your subject line, but there are far more worse issues with this code that were discussed here already.

    http://www.codeguru.com/forum/showthread.php?t=408602
    https://www.securecoding.cert.org/co...+loop+counters
    http://www.math.ucsd.edu/~sbuss/Cou...atingperils.pdf

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured