Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: All
Nov 4, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Representation of Floating Point Numbers

People sometimes complain about the inaccuracy of floating point arithmetic. To demonstrate the level of floating pint inaccuracy, consider the following program:
  #include <iostream> 
  using namespace std;
  int main()
    float f1 = 2000.58;
    float f2 =  2000.0;
    cout << f1 - f2 << endl;
On my machine, this program prints 0.579956 instead of 0.58. More complex calculations yield higher inaccuracy. What is going on here?

First, remember that rounding, approximation and truncation are not the responsibility of C++ but rather, they depend on the particular hardware that your machine uses. Note also that floating point numbers are merely an approximation based on the IEEE standard. On most machines, type float occupies 32 bits. The IEEE standard requires that 1 bit be used for the sign representation (i.e., negative/positive values), 8 bits for exponent, and the remaining 23 bits are for the mantissa. Because a mantissa always has the form 1.nnnn

Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date