NaN and Infinity

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

NaN and Infinity

Post by Clyde » Wed Jul 17, 2019 11:30 pm

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 8 with Windows IDE

User avatar
Clyde
Posts: 195
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: NaN and Infinity

Post by Clyde » Fri Jul 19, 2019 10:39 am

*push* :-)
Currently using: Hollywood 8 with Windows IDE

User avatar
airsoftsoftwair
Posts: 3149
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: NaN and Infinity

Post by airsoftsoftwair » Mon Jul 22, 2019 5:52 pm

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: 819
Joined: Sun Feb 14, 2010 7:11 pm

Re: NaN and Infinity

Post by Bugala » 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. Not really a big deal, since it is easy to fix, but slight annoyance every time.

User avatar
airsoftsoftwair
Posts: 3149
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: NaN and Infinity

Post by airsoftsoftwair » Mon Jul 22, 2019 6:24 pm

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: 195
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

Re: NaN and Infinity

Post by Clyde » Fri Jul 26, 2019 12:31 am

Perfect, thanks a lot, Andreas!
Currently using: Hollywood 8 with Windows IDE

Post Reply