I am porting some code from Matlab to C++.
In Matlab
format long
D = 0.689655172413793 (this is 1.0 / 1.45)
E = 2600 / D
// I get E = 3.770000000000e+03
In C++
double D = 0.68965517241379315; //(this is 1.0 / 1.45)
double E = 2600 / D;
//I get E = 3769.9999999999995
It is a problem for me because in both cases I have to do rounding down to 0 (Matlab's fix), and in the first case (Matlab) is becomes 3770, whereas in the second case (C++) it becomes 3769.
I realise that it is because of the two additional least significant digits "15" in the C++ case. Given that Matlab seems to only store up to 15 significant digits of precision in double precision (as shown above - 0.689655172413793), how can I effectively tell C++ to ignore the "15" at the back?
All calculations are done in double precision.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…