'Rounding error when casting from float to double and back [duplicate]
To comply with the interface of a library I have to round a float to a double, and then back to float again, without any additional manipulation in between. I'd need to understand if the final value is guaranteed to be equal (in the sense of ==
) to the original float value, or if there are cases when this is a safe assumptions.
Solution 1:[1]
I don't know what do you mean by "rouding" float to double. But if you mean conversion, then, IMO, the value is guaranteed to be preserved.
Relevant parts of the C++ standard:
The set of values of the type
float
is a subset of the set of values of the typedouble
;...
http://eel.is/c++draft/basic.fundamental#12.sentence-3
A prvalue of floating-point type can be converted to a prvalue of another floating-point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Daniel Langr |