Discuss Scratch

tirod
Scratcher
1 post

Infinity -Infinity NaN Also, the wiki.

I was trying to think of the most streamlined way to connect some dots, and I realized that I had no idea how Scratch handles division by zero and such, so I tried searching the wiki for a page on Infinity or NaN or error values—something like that. The most I found on the subject was in the Workaround section of the () divided by () (block) page, and that is very brief and apparently out of date.

I used a few scripts to try to test things out for myself.
say ((10) / (0))
says Infinity.
say ((-10) / (0))
says -Infinity.
say ((0) / (0))
says NaN.
say <((10) / (0)) > [0] >
says true.
say <((-10) / (0)) > [0] >
says false.
say <((0) / (0)) > [0] >
says false.
say <((10) / (0)) > ((0) / (0)) >
says false.

So I think that I got a pretty good idea of how these things are handled, but I saw an example somewhere (I didn't note where) that looked like Infinity is used for overflow errors, and I don't really know where to look for anything definite or comprehensive. I understand the behavior well enough for my current scratch, but if there's a place to find a more thorough explanation of special cases, I'd love some help finding it. Also, I'm too new to expand the wiki, so I can't add even the corrections that I've found, so I'm wondering if I should ask for someone to update the wiki. I don't really know if that's the right thing to do in the Scratch community or whether this would be the right place to make such a request, but I it looks to me like this is the place to ask for help, so I'm starting here.

Thanks to anyone who clues me in on anything above!
Paddle2See
Scratch Team
1000+ posts

Infinity -Infinity NaN Also, the wiki.

I think this would make a great addition to the Wiki. That's the best place to store this kind of information. I suggest that you contact some of the Wikians and see if they can incorporate your findings in a Wiki article. There are a lot of Wikians frequenting the forums so hopefully we'll get some good advice from them

Scratch Team Member, kayak and pickleball enthusiast, cat caregiver.

This is my forum signature! On a forum post, it is okay for Scratchers to advertise in their forum signature. The signature is the stuff that shows up below the horizontal line on the post. It will show up on every post I make.
(credit to Za-Chary)



;
bobbybee
Scratcher
1000+ posts

Infinity -Infinity NaN Also, the wiki.

IIRC this is mostly standards behavior for IEEE 754 doubles, which is what Scratch internally uses. (This is a byproduct of Scratch being written in ActionScript, an ECMAScript dialect. You should be able to observe the same behavior in other ECMAScripts, like JavaScript, and possibly in other languages that use a standard `double` type)

“Ooo, can I call you Señorita Bee?” ~Chibi-Matoran
thegoldfish
Scratcher
100+ posts

Infinity -Infinity NaN Also, the wiki.

tirod wrote:

I was trying to think of the most streamlined way to connect some dots, and I realized that I had no idea how Scratch handles division by zero and such, so I tried searching the wiki for a page on Infinity or NaN or error values—something like that. The most I found on the subject was in the Workaround section of the () divided by () (block) page, and that is very brief and apparently out of date.

I used a few scripts to try to test things out for myself.
say ((10) / (0))
says Infinity.
say ((-10) / (0))
says -Infinity.
say ((0) / (0))
says NaN.
say <((10) / (0)) > [0] >
says true.
say <((-10) / (0)) > [0] >
says false.
say <((0) / (0)) > [0] >
says false.
say <((10) / (0)) > ((0) / (0)) >
says false.

So I think that I got a pretty good idea of how these things are handled, but I saw an example somewhere (I didn't note where) that looked like Infinity is used for overflow errors, and I don't really know where to look for anything definite or comprehensive. I understand the behavior well enough for my current scratch, but if there's a place to find a more thorough explanation of special cases, I'd love some help finding it. Also, I'm too new to expand the wiki, so I can't add even the corrections that I've found, so I'm wondering if I should ask for someone to update the wiki. I don't really know if that's the right thing to do in the Scratch community or whether this would be the right place to make such a request, but I it looks to me like this is the place to ask for help, so I'm starting here.

Thanks to anyone who clues me in on anything above!
@tirod,

I read your post above and it does seem like a good idea to include in the wiki. Personally I think the best idea would be for one of us to create a whole new page from Scratch about Infinity and NaN, but I'll talk to the other Wikians about the idea. Thank you for your ideas!

DigiTechs
Scratcher
500+ posts

Infinity -Infinity NaN Also, the wiki.

I'd just like to point out that comparing the mathematical constants is not done correctly, from what I recall.

For example, NaN = NaN returns ‘true’ instead of ‘false’ which is what the expected return value is.

I do, in fact, have my own site; it's here.
I'm also working on a thing called Fetch. Look at it here!
@thisandagain pls explain. @thisandagain pls explain. @thisandagain pls explain. @thisandagain pls explain. @thisandagain pls explain.
djdolphin
Scratcher
1000+ posts

Infinity -Infinity NaN Also, the wiki.

DigiTechs wrote:

I'd just like to point out that comparing the mathematical constants is not done correctly, from what I recall.

For example, NaN = NaN returns ‘true’ instead of ‘false’ which is what the expected return value is.
Yeah, this is basically what Scratch does:
number1 = argument1 as number
number2 = argument2 as number
IF number1 is NaN OR number2 is NaN
string1 = argument1 as lowercase string
string2 = argument2 as lowercase string
compare string1 and string2
ELSE
compare argument1 and argument2
ENDIF
So NaN = NaN is changed to “nan” = “nan”, which is true.

Last edited by djdolphin (July 2, 2015 21:08:32)


!
TheLogFather
Scratcher
1000+ posts

Infinity -Infinity NaN Also, the wiki.

Yup, for comparison operators, anything that's not a number is a string, as far as Scratch is concerned.
Since “NaN” is not a number, it must be a string, then…

But “Infinity” and “-Infinity” are numbers, so it will compare those numerically.

Since it also requires the exact strings above, they are case-sensitive. I haven't checked for sure, but I expect it means the following are compared in different ways internally:
( ((10)/(0)) = [Infinity] ) // compared as numbers
( ((10)/(0)) = [infinity] ) // compared as strings

One curious (but not hugely useful) side-effect is that you can use “Infinity” as a way to test for case of any of the letters it contains.

Since Infinity minus Infinity is NaN, and Scratch treats any non-numerical value (i.e. string) in a numerical operator as zero, you can see the difference in the following:
set [var v] to [Infinity] // this is a number, as far as Scratch is concerned
( (var) - (var) ) // gives NaN
set [var v] to [INfinity] // this is a string, since case of "N" is wrong
( (var) - (var) ) // gives zero

Creating an overflow to get Infinity is straightforward enough:
set [var v] to ( (1e200) * (1e200) ) // var will become "Infinity", since value is too large

But watch out for a curious discrepancy on the way there…
set [var v] to ( (1e152) * (1e152) ) // var shows "Infinity" in its watcher, but still has value ~1e304
See https://github.com/LLK/scratch-flash/issues/614
(I think above is caused by actionscript's “toFixed”, which is used to turn a float into a string with a certain number of places. It appears to actually multiply the number by 10^(number-of-places -you-want-to-show), in order to work out the digits it needs to show… )

Last edited by TheLogFather (July 2, 2015 22:49:06)


Siggy the Kumquat slayer:
Main account: DadOfMrLog –– Frameworks for basic pen-rendered 3D in scratch (see studio). Examples:

- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -

Powered by DjangoBB