문제

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