Does casting to or from a double and a float preserve infinity and NaN?

Viewed 1318

When casting double infinity to float and vice versa, will it still be infinity? Is it the same with NaN?

1 Answers

Converting any float to a double is guaranteed to preserve the value. Converting a double to float is guaranteed to preserve the value if the original value is representable as float.

If your system conforms to IEEE-754, then float is able to represent infinity and NaN. Otherwise, you can use <numeric_limits> to check whether that is the case. The payload of a double NaN is not necessarily representable by a float NaN.

Related