Frage

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?

War es hilfreich?

Lösung 4

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

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top