NaN and Infinity

Discuss any general programming issues here
Post Reply
User avatar
Clyde
Posts: 349
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

NaN and Infinity

Post by Clyde »

Hi there,

currently I am porting some lua code to Hollywood. In this code there are checks if a number is NaN (not a number) and infinity, like so:

Code: Select all

val ~= val -- checks for NaN -> "We rely on the property that NaN is the only value that doesn't equal itself." (see http://lua-users.org/wiki/InfAndNanComparisons)
val >= math.huge
The first line could be easily transformed to val <> val. But is this legit or neccessary in Hollywood? Does NaN "exist" in Hollywood?

And what about the second line, checks agains infinity? Is that possible in Hollywood?

Thanks in advance!
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
User avatar
Clyde
Posts: 349
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: NaN and Infinity

Post by Clyde »

*push* :-)
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: NaN and Infinity

Post by airsoftsoftwair »

Nice spot, this is currently not possible without going to greater pains because in comparison to Lua, Hollywood won't allow you to divide by zero. I have now added some functions which allow you to deal with all those things:

Code: Select all

- New: Added RawDiv(); this does the same as Div() but doesn't check the divisor against 0 which allows you
  to compute machine-specific representations of NaN and Inf by computing the results of 0/0 or 1/0
- New: Added IsNan(), IsInf(), and IsFinite() to math library to check a value against NaN, infinity, and
  finiteness; also added the constants #NAN and #INF which contain the values of NaN and infinity; note
  that due to the design of the parser, you cannot use #NAN as a literal value in your scripts; instead,
  you have to use GetConstant("#NAN") to get the value of #NAN; #INF, on the other hand, can be used in
  scripts; note: do not test for NaN by comparing the number with itself, expecting to get FALSE; this
  won't work on all platforms; always use IsNan()
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: NaN and Infinity

Post by Bugala »

That was quick service. Was just about to ask if there be possibility to accept division by zero somehow, since that have been trouble so many times. Not really a big deal, since it is easy to fix, but slight annoyance every time.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: NaN and Infinity

Post by airsoftsoftwair »

Bugala wrote: Mon Jul 22, 2019 6:00 pm That was quick service. Was just about to ask if there be possibility to accept division by zero somehow, since that have been trouble so many times.
What's so funny about dividing by zero? Because it is forbidden? ;)
User avatar
Clyde
Posts: 349
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: NaN and Infinity

Post by Clyde »

Perfect, thanks a lot, Andreas!
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
Post Reply