Garbage bits in the floating point representation of numbers in C/C++ program

Go To StackoverFlow.com

1

Possible Duplicate:
Is JavaScript's Math broken?
Dealing with accuracy problems in floating-point numbers

Consider the code below and its output:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <limits>
#include <vector>


int main(int argc, char *argv[])
{
  double xleft  = 0;
  double xright = 1.0;

  double dx = 0.1;
  std::cout << std::setprecision(36) << "dx is " << dx << std::endl; 

  int numgridpts = ((int) ceil (( xright - xleft )/dx))  + 1; 


  for (int i = 0; i < numgridpts  ; ++i)
    {
      std::cout << std::setprecision(36) << xleft + i*dx << std::endl;
    }

  return 0;
}

[~:ICgen/$ ./a.out                                                                                                                               
dx is 0.100000000000000005551115123125782702
0
0.100000000000000005551115123125782702
0.200000000000000011102230246251565404
0.300000000000000044408920985006261617
0.400000000000000022204460492503130808
0.5
0.600000000000000088817841970012523234
0.700000000000000066613381477509392425
0.800000000000000044408920985006261617
0.900000000000000022204460492503130808
1

My question is when I print out the numbers till a precision of 36 bits, why are the numbers, 0 , 0.5 and 1.0 represented exactly, wherars the other numbers seem to have some garbage numbers placed at the end?

Also if I add the floating point representations of 0.2 and 0.1 as shown in the output above, they dont seem to add up to the representation of 0.3, in the part of the garbage -bits.

I am using Linux Ubuntu 10.10 and the gcc compiler

2012-04-04 20:57
by smilingbuddha
You will get about 10 answers in 3...2...1... (that all will tell you that floating point arithmetic cannot exactly represent a given decimal number in all cases) - Eric J. 2012-04-04 20:58
"floating point arithmetic cannot exactly represent a given decimal number in all cases - Robᵩ 2012-04-04 21:02
@Eric J. Then why are 0.5 , 1 and 0 represented exactly above - smilingbuddha 2012-04-04 21:02
Double precision is accurate to 15-16 decimal digits. But n/2^k can be represented exactly - David Heffernan 2012-04-04 21:02
@smilingbuddha: Some numbers can be exactly represented, just not all numbers - Eric J. 2012-04-04 21:03
@smilingbuddha because there are of the form n/2^ - David Heffernan 2012-04-04 21:03
@rob yes, integer - David Heffernan 2012-04-04 21:05


5

These are not garbage numbers but as as accurate values of those numbers as possible within the floating point representation according to the IEEE 754 standard.

That standard uses binary numbers, not decimal numbers. Therefore, fractions like 1/2, 1/4, 1/8 (= 0.5, 0.25, 0.125, ...) and their multiples are represented exactly, whereas fractions like 1/3 or 1/10 are not.

If this was a decimal digit system, fractions like 1/10, 1/100, 1/1000... and their multiples would be the only numbers represented exactly with a finite number of digits.

In both systems, all integers are represented exactly if they are not extremely large.

2012-04-04 21:02
by Jirka Hanika
Ads