1

Possible Duplicate:
Floating point inaccuracy examples

In my computer and using g++ I found that:

21*3.6 ==  75.60000000000000852651

and that ruins a simple loop. See this code:

  double incr=3.6;
  double limit=21*3.6;
  printf (" ¿%f == %.20f? \n", limit, limit);
  for (double x=0.0; x<limit; x += incr) {
    printf (" x %f < limit %f (dif = %.20f) \n", x, limit, limit-x);
  }

and its output:

 ¿75.600000 == 75.60000000000000852651? 
x=0.000000, limit=75.600000 (dif = 75.60000000000000852651) 
x=3.600000, limit=75.600000 (dif = 72.00000000000001421085) 
x=7.200000, limit=75.600000 (dif = 68.40000000000000568434) 
x=10.800000, limit=75.600000 (dif = 64.80000000000001136868) 
x=14.400000, limit=75.600000 (dif = 61.20000000000000994760) 
x=18.000000, limit=75.600000 (dif = 57.60000000000000852651) 
x=21.600000, limit=75.600000 (dif = 54.00000000000000710543) 
x=25.200000, limit=75.600000 (dif = 50.40000000000000568434) 
x=28.800000, limit=75.600000 (dif = 46.80000000000000426326) 
x=32.400000, limit=75.600000 (dif = 43.20000000000000284217) 
x=36.000000, limit=75.600000 (dif = 39.60000000000000142109) 
x=39.600000, limit=75.600000 (dif = 36.00000000000000000000) 
x=43.200000, limit=75.600000 (dif = 32.39999999999999857891) 
x=46.800000, limit=75.600000 (dif = 28.79999999999999715783) 
x=50.400000, limit=75.600000 (dif = 25.19999999999999573674) 
x=54.000000, limit=75.600000 (dif = 21.59999999999999431566) 
x=57.600000, limit=75.600000 (dif = 17.99999999999999289457) 
x=61.200000, limit=75.600000 (dif = 14.39999999999999147349) 
x=64.800000, limit=75.600000 (dif = 10.79999999999999715783) 
x=68.400000, limit=75.600000 (dif = 7.20000000000000284217) 
x=72.000000, limit=75.600000 (dif = 3.60000000000000852651) 
x=75.600000, limit=75.600000 (dif = 0.00000000000001421085)  

that shows that the last time the loop should not be executed but for the error!

Suggestions?

Community
  • 1
  • 1
cibercitizen1
  • 20,944
  • 16
  • 72
  • 95
  • http://docs.sun.com/source/806-3568/ncg_goldberg.html – Anycorn May 12 '11 at 21:23
  • 7
    Read [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html). – Robᵩ May 12 '11 at 21:23
  • 2
    There should be a special link for UltraFAQs. – Warren P May 12 '11 at 21:25
  • have you tried float instead? – Lou May 12 '11 at 21:27
  • 1
    We need #SOSTDAnswer1: That's how floating-point works that we can flag these questions with. – S.Lott May 12 '11 at 21:27
  • In short: floating point numbers in C don't have infinite precision. That is, when doing paper+pencil arithmetic, you can be as precise as you need with numbers - just write down another digit. In computing, where a double only has 64 bits to encode *any* number, you lose precision. It's a given that C floating point arithmetic will be inexact! The solution, then, is to add some tolerance to your comparisons. Either truncate after a few decimal places, or instead of saying (a == b), use logic to say, for example, "a is within +/- 0.01 of b" – poundifdef May 12 '11 at 21:32

1 Answers1

6

You need to read this:

What Every Computer Scientist Should Know About Floating-Point Arithmetic

You are using a floating point number, and you need to know what floating point means. What it can do, and what it can't do. There are numbers that you can represent in a few digits of text, that can't be exactly represented in a fixed-point, or floating-point binary numeric form. You are making assumptions about floating point that are not specifically related only to C programming, but to any computer system using floating point numbers.

Warren P
  • 65,725
  • 40
  • 181
  • 316