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

Warn equality with NaN #6643

Closed
vicuna opened this issue Nov 4, 2014 · 5 comments
Closed

Warn equality with NaN #6643

vicuna opened this issue Nov 4, 2014 · 5 comments

Comments

@vicuna
Copy link

vicuna commented Nov 4, 2014

Original bug ID: 6643
Reporter: bouaziz
Status: resolved (set by @xavierleroy on 2015-12-06T16:19:17Z)
Resolution: suspended
Priority: low
Severity: feature
Category: ~DO NOT USE (was: OCaml general)

Bug description

Since x = nan will always be false for any x, a warning should be issued telling the user to use Pervasives.compare instead

@vicuna
Copy link
Author

vicuna commented Nov 5, 2014

Comment author: bouaziz

Of course, the same applies for <, <=, >, >=

@vicuna
Copy link
Author

vicuna commented Dec 9, 2014

Comment author: @damiendoligez

I'm not sure I understand what you mean: the compiler cannot know in general whether a given float expression is going to be NaN.

As for emitting a warning at run-time, that cannot be done because equality comparison must not do side-effects.

@vicuna
Copy link
Author

vicuna commented Dec 10, 2014

Comment author: bouaziz

I do not want a warning at runtime but it would be great to have it at compile time when comparing with Pervasives.nan. E.g.
if x = nan then "nan" else "a number" -> warning

Of course it would not work with any other value being equal to nan (static analysis, e.g. optimization, could catch some of them though). E.g.
let y = 0. in
if x = y /. 0. then "nan" else "a number"

Another way of seeing it is to optimize ... = nan (and I mean any value known at compile time to always be equal to nan) as false and then report dead branches like the "then" branch in the previous examples.

@vicuna
Copy link
Author

vicuna commented Dec 10, 2014

Comment author: @alainfrisch

Careful with optimizations: you probably don't want to trigger a warning after inlining a function which results in comparing a float with NaN, as in:

let f lim x =
.... if x < lim then ....

let g = f nan x

@vicuna
Copy link
Author

vicuna commented Dec 6, 2015

Comment author: @xavierleroy

The use case is uncommon, and there are limits to the number of special cases the compiler can reasonably warn about. Suspending this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant