-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Add NaN, inf, is_nan and is_infinite #407
Conversation
Thank you. I thought about this in the past, but didn't get around to implementing it — thanks. I'm not sure if we really need NaN as a special token in the language, though? Could we perhaps define it in the prelude? For now, we could use something like
Note that you can use
|
I figured having 'NaN' as a reserved keyword would make it available even with the '--no-prelude' option (just as any other value) and it would protect it from being user-redefined which could lead to unexpected behavior. On the other hand also
Sounds good to me! |
If I understood it correctly that currently only works for functions with |
Do we also want to add "constants" for positive and negative infinity as well? |
I think that could be handy. Positive infinity On that occasion it would probably make sense to add |
I thought about this some more and I think I actually agree with you on this. Would you mind reverting that change again and making NaN/inf a proper builtin? (Sorry)
Yes. I thought about adding something like
which could be used like |
Of course not! That's on me, I was changing it a bit too quickly before waiting for your reply. |
@@ -1724,6 +1730,12 @@ mod tests { | |||
should_fail(&["0x1.2", "0b1.0", "0o1.0", "0x.1", "0b.0", "0o.1"]); | |||
} | |||
|
|||
#[test] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had some trouble writing a proper test for NaN
since NAN == NAN
is false
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add a test for this somewhere in the interpreter tests, and check that NaN == NaN
evaluates to false in Numbat.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a test in the interpreter tests already which works fine but I thought it might be good to have a parser test too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I missed that, but we can have something like NaN != NaN
as a test too I suppose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think we're good with the inf
test here. The code for NaN is the same.
This pull request introduces the 'NaN' keyword such that such that
NaN
values may be used directly in numbat.As far as I am aware,
NaN
can currently only be accessed by calling an FFI function that returnsNaN
(e.g.ln(-1)
orsqrt(-1))
but there is no direct way to declare aNaN
value. With this change it becomes possible to useNaN
directly, e.g:To check if a value is
NaN,
ais_nan()
function is added (see also #144).I stumbled over this limitation when trying to implement a function with a limited domain that can handle input outside of the domain in a meaningful way in numbat. Using the
NaN
keyword it is possible to write: