Maybe that happens because of float precision. Try using double.
C++ simple math operations with floats and a wrong result? [duplicate]
-
02-10-2022 - |
Question
I need simple math operations with some floats like "3.3 - 2.6". The result is 0.700001. I have no idea why there are numbers like 0.700001.
Can you help me please?
La solution 4
Autres conseils
The computer handles numbers in binary format with finite precision. Various simple decimal numbers cannot be precisely represented, so it gives the closest answer possible.
The usual way of dealing with the problem is simply to limit the precision you actually display. If you're using C++ streams for output then the std::fixed
and std::setprecision
manipulators should help.
3.3 is represented in memory as 3.29999
and
2.6 is represented in memory as 2.59998
so
3.3 -2.6 = 0.70001
this post might give you a little more insight on the matter
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow