LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Round off Errors in LabWindows/CVI

I have a double precision variable, which is initially set to 1.0.  In a while loop (while x < 1.7) , I add 0.1 to it:  x = x + 0.1.  When I get to x = 1.6, x becomes 1.700000001.  This causes my loop to prematurely terminate. 
 
Is there anyway around this?   
0 Kudos
Message 1 of 2
(2,719 Views)

Floating point representations are not, in general, exact and you must allow for some error in the representation. For an iterative loop such as you have, it might be better to use an integer control variable:

for (i=0;i<7;i++)
{
    double x=1.0 + 0.1*i;
    ...
}

or alternatively, you must allow for some error in the representation:

for (x=1.0; x<1.75;x+=0.1)
{
   
}

 

--
Martin
Certified CVI Developer
0 Kudos
Message 2 of 2
(2,699 Views)