LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

float problem 0.001

Read this piece of code....

 

        float f = 32760.0;       
        for (;;) {           
            f += 0.001;
            if (f > 32770.0) break;           
        }

 

 I expect that after some iterations the loop terminates.

The problem is that after the variable f reaches 32768.0, it won't increment anymore.

if the variable f is incremented by 0.002, the loop exits correctly.....

 

This was reported using in CVI81 and 711...

 

any ideas ?????

 

0 Kudos
Message 1 of 7
(6,863 Views)

Use a double, not a float. You are hitting the granularity of the floating point representation. There are a limited number of bits in which to store your information, and your example exceeds this limit.

 

JR

0 Kudos
Message 2 of 7
(6,861 Views)

Nice point,

 

I understand there is a granularity limit for each type, but it is surprising to see that it changes with actual value.

Maybe it is obvious once you work on the binary floating point representation.

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 7
(6,857 Views)
Using double probably would fix your issue, but mabye there's actuall more going on here.  I believe a float should be able to be incremented by the machine epsilon (something in the area of 5.96e-08).  You're certainly doing a lot more than that, so I'm not sure why it's not working with floats.
0 Kudos
Message 4 of 7
(6,828 Views)

Floating point precision is in terms of total decimal digits.  A surprise to many is that while you may be able to exactly represent, say 0.001, you may not be able to exactly represent 1000000000.001

 

You might wind up with 1000000000.00099999 for example instead.

 

Also realize that a floating point number is a binary approximation  of a decimal number.  Floating point representation allows the exact representation of a finite number of decimal values, and these numbers are not evenly distributed due to the way the binary approximation works.

 

A double allows the exact representation of a far larger number of decimal values than float, and over a larger range.

 

As an exercise it's very informative to look at the binary representation for different decinmal numbers and see for yourself how it works.  You have to be careful, though, since you can obscure the issue when print the decimal values out as the formatting may round the values to a smaller precision than is in the value itself.

 

Menchar

0 Kudos
Message 5 of 7
(6,817 Views)
Just FYI, a good site to check for binary/human readable conversions & how they work is http://babbage.cs.qc.edu/IEEE-754/32bit.html
Message 6 of 7
(6,814 Views)

That's a very cool site.

 

If you go up to the parent page for that link you see a link to a page where you can enter a decimal number and see the resulting IEEE 754 representations.

 

I remember 35 years ago figuring this all out for myself on a 16 bit HP minicomputer.

 

Menchar

Message Edited by menchar on 11-20-2009 02:52 PM
0 Kudos
Message 7 of 7
(6,806 Views)