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.

Some examples

Can you help me please?

有帮助吗?

解决方案 4

Maybe that happens because of float precision. Try using double.

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top