Search code examples
c++cmath

Different results using same function from different libraries


Here is some code:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    long long int a, b;
    long long int c;

    cin >> a >> b;

    c = abs(a) + abs(b);
    cout << c;
    cout << endl;
}

Which supposed to return 1000000000000000002 when I input 1000000000000000000 and 2.

If I try to do it with cmath it will return 1000000000000000000, but if I use cstdlib it will return 1000000000000000002. Why is that even happening?

Also considering that I'm using cmath, shouldn't it work even more proper?

I'm using Linux Mint 18.2 64bit, Eclipse Platform.


Solution

  • The cmath version is a float one. So when you only have that one, you actually do the computation on floats and convert back into a long long at the end. Float precision not being enough to hold 18 digits, the +2 simply gets lost.

    The cstdlib version is a integer one. Which gives the expected result.

    As pointed out in the comments, in C++11, cmath also defines a version of abs that takes integers. However, “these overloads effectively cast x to a double before calculations (defined for T being any integral type)”.

    I believe your compiler should give you a warning for the conversions if you use -Wall -Wextra or similar flags while only inclulding cmath.