If you store a floating point value in an integral type (such as int
), you are going to lose any precision after the decimal point. Integral types simply only store integers. If you store a float
in an int
, you will lose anything after the decimal point.
If you add 0.1 to a float
, you simply get a floating point value approximately 0.1 greater than the original value. If you assign that to an int
, you'll have exactly the same problem. You're just going to truncate a different value.
If you need a float
to retain its precision, simply store it in a float
(or a double
for even higher precision).
If perhaps you are doing a conversion like int -> float -> int
where the original int
is of one value and the value you get at the end is a different int
, well that's just what happens. The conversion to a float
is not symmetric. The int
to float
conversion will convert to the nearest representable float
value, whereas the float
to int
conversion will truncate.
A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. [...]
A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating point type. The result is exact if possible. If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value. [...]
The solution to this is to not convert to an intermediate float
. If you want an int
, keep it an int
. If for some reason it makes sense for your function to return a float
despite the fact that the thing it calculate was an int
then you have to accept the consequences.