简体   繁体   中英

What is the best way to avoid unexpected behavior due to the binary storage of floating point numbers?

I was writing a simple for loop recently and got some unexpected behavior:

for(double x = 0.0; x <= 1.0; x += 0.05)
{
    Console.WriteLine(x.ToString());
}

This is the output:

0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
0.95

Notice that 1 doesn't appear even though the condition for continuing the for loop seems to include it. I realize that the reason for this is because decimal numbers are being stored in memory as binary, ie 1 is not really exactly 1 but actually 1.0000000000000002 (according to the variable watch in Visual Studio). So my question is, what is the best way to avoid this unexpected behavior? One way would be to use the decimal type instead of double , but most of the System.Math functions work on double s only, and casting between the two isn't straightforward.

Don't test doubles for equality.

Here you could use integer arithmetic instead:

for (int i = 0; i <= 20; ++i)
{
    double x = (double)i / 20.0;
    Console.WriteLine(x);
}

In other cases it might be more appropriate to test if the difference between two doubles is sufficiently small rather than using an equality comparison.

always manage yourself the rounding, don't let the system handle it

  for(double x = 0.0; Math.Round(x,2,MidpointRounding.AwayFromZero) <= 1.0; x += 0.05)
  {
      Console.WriteLine(x.ToString());
  }

will output

0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
0.95
1

This is referred to as floating-point error and a lot has been written about it. One solution for your case would be to define an error, such as

err = .000001

and do the comparison to within err of 1.0.

can you not scale up and use int

 for(int ix=0; ix < 1000; ix += 50)
 {

  }

then divide by 1000 if you need to. but this way you get precise loop behaviour

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM