0

The Problem

I run the code below on two different compilers and each one shows different output.

#include <iostream>

using namespace std;   

int main()
{
    long double LD1 = 99999.9999;
    long double LD2 = 99999.9999 + 1e-9;
    double D = 99999.9999;

    long long a = LD1 * 10000;
    long long b = LD2 * 10000;
    long long c = D * 10000;

    cout << a << endl << b << endl << c << endl;
}

When running on ideone (chosing C++11) I get the following:

999999998
999999999
999999999

When running on MSVC13 I get the following:

999999999
999999999
999999999

The Questions

  1. How can 99999.9999 * 10000 = 99999.9998 (What is the reason behind this problem)?
  2. How does the problem get fixed by adding 1e-9?
  3. Why am I getting different values for a in the two compilers?
Gaith
  • 798
  • 8
  • 19

0 Answers0