Search code examples
c++mathfloating-point-precision

how to handle result of cos(M_PI/2)


M_PI is a macro which is defined as 3.14159265358979323846, which seems more precise than a double. I'm trying to get the answer in a float and no amount of casting will help me, the result is always 6.12323426e-017 or -4.37113883e-008 if i try and cast M_PI to a float.

The answer should be 0 and I would like to store that in a float.


Solution

  • That's impossible. There's no way to represent the exact value of pi in a finite floating-point data type.

    In general, you can't expect any operation on floating-point values to be exactly equal to the corresponding mathematical operation on real values. The usual approach is to compare with a tolerance, in this case perhaps something like

    if (std::abs(result) < tolerance) {
        // treat it as zero
    }
    

    Choosing a suitable value of tolerance to get the accuracy you need for your particular problem is the hard part.