LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Float number in CVI 7.1

Hi,

I have a piece of code running on CVI 7.1 (Win XP). It's as simple as:

    int i;
    float f1  = 100000000;
    double d1 = 100000000;

    for (i=0; i<100; i++)
    {
        f1++; d1++;
    }

When I single step the loop, I found variable d1 incremented in each loop, but f1 stayed the same all the time. I assume f1 should have 32 bits, so 100000000 is 0x5F5E100, no overflow... If I mannually change the value of f1, it's more interesting.  When I entered from 100000000 to 100000004, it automatically went back to 100000000; entered from 100000005 to 100000011, it went back to 100000008,  etc. The next one is 10000001.  You can try more.

I'm baffled.

Could somebody tell me why? I currently redeem it's a CVI bug.

Thanks.
Jason
0 Kudos
Message 1 of 13
(4,491 Views)

You're probably getting the value from a GUI control that's constraining the maximum value of the variable.

100 e 6 is fully representable in IEEE 754 format.

 

0 Kudos
Message 2 of 13
(4,487 Views)
But if you try from 99 e 6, you get the same problem.

Jason
0 Kudos
Message 3 of 13
(4,485 Views)
I found the "magic number". It's 16777216. It will keep increasing until it reaches this number.

Jason
0 Kudos
Message 4 of 13
(4,482 Views)
Why don't you post the actual code you're using, because the code you've shown will never act the way you've described.
0 Kudos
Message 5 of 13
(4,481 Views)

I take it back, it does what you say ....

Stand by while I figure this out ...

0 Kudos
Message 6 of 13
(4,478 Views)
Maybe ++ not defined on floating point types.
0 Kudos
Message 7 of 13
(4,476 Views)
You haven't assigged the result of the f1++ to anything, so the side effect of incrementing the variable is discarded.
 
The result of an n++ operation is the original value.
 
f2 = f1++;  // f2 = f1 + 1
 
Duh.  I'm getting old.
0 Kudos
Message 8 of 13
(4,474 Views)

Certain numbers including integers between FLT_MIN and FLT_MAX cannot be represented in floating point format. To see why, check out:

http://en.wikipedia.org/wiki/IEEE_754#Single-precision_32_bit

0 Kudos
Message 9 of 13
(4,467 Views)
Yup, I'm wrong yet again.
 
floats are a discrete, finite set of representable numbers. 
 
And 100000001 isn't one of them Smiley Surprised
 
Floats are evaluated as doubles I think with guard bits to 80 bits on a PC, but when cast back to float lose the increment.
 
 
I can retire in 10 months Smiley Tongue


Message Edited by menchar on 11-14-2007 01:24 PM
0 Kudos
Message 10 of 13
(4,463 Views)