This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-03-07 12:13:37

mandostop1
New Scratcher
Registered: 2010-11-14
Posts: 4

NaN message in browser - program works fine in Scratch window

I created a program that simulates using the quadratic formula. The program outputs the roots of a quadratic equation given 3 inputs, a, b, and c, which are set up as variables.

I teach high school math, so I'm demonstrating to the students how scratch can be used in fun ways, to supplement what we're already doing in the classroom.

(Anyways, for those who need a brief refresher, some quadratic equations have
complex roots, where the discriminant in less than zero. In other words, the graph does not cross the x-axis in the real number plane. In other words, there are imaginary roots, with the number "i", the square root of negative one.)

It appears to me that the math functions built into scratch do not allow for
complex roots or "i" as a number. Therefore, I have sort of an error check built into the
user interface, where, if a,b, and c, produce a parabola which has complex roots, then the character on the screen, says something like this: "Sorry, you're quadratic equation does not cross the x-axis. Scratch doesn't do imaginary roots. Hit the green arrow to start over with a problem that has real roots."

So, when I view the program from the scratch application interface, to test the program,
this error-checking message works fine.
I uploaded the program to the Scratch website. On the Scratch website, on a web browser, this message does not appear when there are imaginary roots.
The message the appears is "The roots are NaN, and NaN..."
This clearly indicates to me that the program is interpreting complex roots and
giving them the value 'NaN' which is some type of error checking variable within
the Scratch API.

Can someone tell me what to do to make the program work on the website
in the exact same way it works from the Scratch application on my desktop.
This is the program:

http://scratch.mit.edu/projects/mandostop1/2374673

Thank you very much.

Tim May
(mandostop1)

Offline

 

#2 2012-03-07 12:34:53

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: NaN message in browser - program works fine in Scratch window

Wouldn't it be possible to check if the output given is NaN?

if <(variable) = [NaN]>
say [Doesn't cross the X axis blah blah...]
end
Edit: I'm pretty sure this would work, seeing as scratch doesn't really diffrentiate between string and special values (such as NaN). So it probably would be just stored as a string.

Last edited by rookwood101 (2012-03-07 12:37:35)


http://i.imgur.com/zeIZW.png

Offline

 

#3 2012-03-07 14:02:58

mandostop1
New Scratcher
Registered: 2010-11-14
Posts: 4

Re: NaN message in browser - program works fine in Scratch window

Interesting suggestion from rookwook101.
I couldn't make that work on the first attempt.
It gave me other ideas though, when I looked at the code.
For the time being I have kludged it so the user doesn't see the message for a looooong time. I still haven't eliminated the message entirely if you wait long enough, when the
roots are imaginary.
While on the topic, does anyone know how to handle complex numbers in Scratch?
The square root function appears to only work numbers greater than or equal to zero.

Offline

 

#4 2012-03-07 15:10:31

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: NaN message in browser - program works fine in Scratch window

Why not calculate

if <(((b) * (b)) - (((4) * (a)) * (c))) < (0)>
think [No real roots!] for (3) secs
else
think [Everything's OK!] for (3) secs
end
to determine if real roots exist?

Last edited by BoltBait (2012-03-07 15:13:48)


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#5 2012-03-07 15:19:47

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: NaN message in browser - program works fine in Scratch window

roots of imaginary  hmm
maybe try to code it by hard?
http://en.wikipedia.org/wiki/Square_roo … ex_numbers
maybe my 4-operations calc will help?  smile

anyway, my complex numbers understanding is not that great, but i guess a maths teacher could code that  smile

Offline

 

#6 2012-03-08 05:14:22

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: NaN message in browser - program works fine in Scratch window

I've done a Mandelbrot Set grapher (which needs complex numbers in the iteration process), which can be seen here. About the imaginary problem (the numbers are imaginary, not the problem  tongue ), how are you error catching?


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#7 2012-03-08 13:30:06

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: NaN message in browser - program works fine in Scratch window

he checks if the discriminant is lower than 0^^

Offline

 

Board footer