Monday, November 3, 2014

Scientific Notation - best way to print floating point numbers

Dealing with floating point numbers is one of the most complex thing which a programmer and computer deal with in a day-to-day programming.

Floating point numbers are real numbers which has a fractional part.
For Eg: 1.234, -2.345 .. etc

As we all know that computers are the integer based machines, [Basically it can understand only 2 voltages high (1) and low (0)], and there are complex ways which includes lots of assumptions and approximations in representing floating point numbers, In this post i will only focus on the best way to print floating point numbers.

There are few standards set by IEEE for using floating point numbers. So coming to printing of floating point numbers on the standard o/p (monitor) or in the file, which is the best practice to be followed ? Which types of notation is more precise and more consistent.... Answer to that would be scientific notation

In scientific notation (which is considered most standard way) any number can be represented in the form :
x*10y

For Eg : 0.2 --> 2*10-1
              200 --> 2*102


The following c++ code and its output could prove to be a simple proof to explain why is scientific method the best way to print floating point numbers.


// Non-scientific way
#include<iostream>  // To print to standard output
#include <iomanip> 
using namespace std;

int main () {

    double shift = 10616.200000000001;
    double num1 = 0;
    double num2 = 0.01;
    
    std::setprecision(15);
    // cout << scientific;

    cout << "(shift + num1) * 1e-3 = " << setw (12) << (shift + num1) * 1e-3 << endl;
    cout << "(shift + num2) * 1e-3 = " << setw (12) << (shift + num2) * 1e-3 << endl;

    return 0;
}

// Output
(shift + num1) * 1e-3 =  10.6162
(shift + num2) * 1e-3 =  10.6162

From above output we can observe that even tough we had set the precision to more than what we need, final result is same irrespective of num1(0) and num2(0.01) begin different.

Now let us try to print the same numbers in scientific way and see the difference. We can just use cout << scientific to print in scientific way. From the above code we need to just remove the comment line.

// Scientific way
#include<iostream>  // To print to standard output
#include <iomanip> 
using namespace std;

int main () {

    double shift = 10616.200000000001;
    double num1 = 0;
    double num2 = 0.01;
    
    std::setprecision(15);
    cout << scientific;

    cout << "(shift + num1) * 1e-3 = " << setw (12) << (shift + num1) * 1e-3 << endl;
    cout << "(shift + num2) * 1e-3 = " << setw (12) << (shift + num2) * 1e-3 << endl;

    return 0;
}

And its output looks like this.

//Output 
(shift + num1) * 1e-3 = 1.061620e+01
(shift + num2) * 1e-3 = 1.061621e+01

We can very clearly observe that the result is different while printing in scientific notation.


Hence scientific notation is the best way as it does not lose more information due to approximations and its safe way to deal with floating point numbers.

A very big thank you to http://hilite.me/ which helped me beautify the code.

No comments:

Post a Comment