Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int_of_float nan = 0, int_of_float infinity = 0 #4207

Closed
vicuna opened this issue Feb 14, 2007 · 1 comment

Comments

@vicuna
Copy link

commented Feb 14, 2007

Original bug ID: 4207
Reporter: letaris
Status: closed (set by @xavierleroy on 2007-02-21T14:14:42Z)
Resolution: won't fix
Priority: normal
Severity: minor
Version: 3.09.3
Category: ~DO NOT USE (was: OCaml general)
Monitored by: yminsky pzimmer @mmottl

Bug description

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.

@vicuna

This comment has been minimized.

Copy link
Author

commented Feb 21, 2007

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 )
let max_float_int = (
appropriate float constant close to max_int *)
let safe_int_of_float f =
if f >= min_float_int && f <= max_float_int
then int_of_float f
else invalid_argument "safe_int_of_float"

@vicuna vicuna closed this Feb 21, 2007

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.