int_of_float nan = 0, int_of_float infinity = 0 #4207
Original bug ID: 4207
It seems that the only sane thing for int_of_float to do when presented with NaN is to raise an exception, the same goes for infinity.
The text was updated successfully, but these errors were encountered:
Comment author: @xavierleroy
The documentation for int_of_float does warn that the result is unspecified if the float argument falls outside the range of representable integers. I will add "or if the argument is NaN".
But, more generally, int_of_float inherits its semantics from that of the double -> long conversion of C or that of the corresponding machine instruction, just like other integer and float arithmetic operations in Caml. I don't think it would make sense to raise an exception in case of overflow for int_of_float but not for other arithmetic operations.
Note that if this is a serious problem in your application, it is possible to define a safer version of int_of_float, for instance:
let min_float_int = (* appropriate float constant close to min_int )