Reputation: 278
Im trying to do an elipsoid and to do so I need the following operation
float result = this->eValues[0]+this->eValues[2]-this->eValues[1]
in this case
eValues[0]=-1.86265e-09;
eValues[1]=8.999999;
eValues[2]=8.999999;
It supposed it should return -1.86265e-09 but the result is 0
Thanks
Upvotes: 0
Views: 188
Reputation: 5894
This is a problem of floating point precision, it is made more clear by the order that the additions is carried out:
#include <iostream>
int main() {
float a = -1.86265e-09;
float b = 8.999999;
float c = 8.999999;
std::cout << ((a + b) - c) << std::endl; // 0
std::cout << (a + (b - c)) << std::endl; // -1.86265e-09
return 0;
}
The reason why the first output produces 0 is because -1.86265e-09
is very small relative to 8.999999
. The difference in the exponent of how floating point numbers is stored is too great for the float to produce the correct arithmetic answer
Upvotes: 7