Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 812
Evgeny Avatar asked Apr 06 '21 13:04

Evgeny


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.

like image 123
eerorika Avatar answered Nov 08 '22 15:11

eerorika