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

#1 2012-12-08 11:46:32

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Casting help

I think this is the right place to ask this...? Never asked anything before. :P

Soooo...

I'm working on a project that calculates the quadratic formula. Easy peasy, right? No, because I want it to deal with imaginary results as well.

Here's what I've got:
(by the way, the sqrt variable is the returned value of my sqrt function. quad answers is the returned values of the quad formula function, of course. lparen and rparen are parenthesis, but they mess up the block plugin if there.)

quadratic formula a: (a) b: (b) c: (c)

sqrt (((b)*(b))+((-4)*((a)*(c))))
if <(letter (length of (sqrt)) of (sqrt))=[i]>
add (join (((-1)*(b))/((2)*(a))) (join [ + lparen] (join (join [i√] (round ((sqrt)*(sqrt)))) (join [rparen / ] ((2)*(a)))))) to [quadratic answers v]
add (join (((-1)*(b))/((2)*(a))) (join [ - lparen] (join (join [i√] (round ((sqrt)*(sqrt)))) (join [rparen / ] ((2)*(a)))))) to [quadratic answers v]
else
add ((((-1)*(b))+(sqrt))/((2)*(a))) to [quadratic answers v]
add ((((-1)*(b))-(sqrt))/((2)*(a))) to [quadratic answers v]
end
sqrt (n)
if <not <([abs v] of (n))=(n)>>
set [sqrt v] to (join ([sqrt v] of ([abs v] of (n))) [i])
else
set [sqrt v] to ([sqrt v] of (n))
end
The problem I'm having is that if the answer is imaginary, the square root (when squared) is 0. This is because it says the string is 0 because there's a letter in it, i. How can I cast it to get rid of i?

Last edited by veggieman001 (2012-12-08 15:12:50)


Posts: 20000 - Show all posts

Offline

 

#2 2012-12-08 15:20:54

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: Casting help

Nevermind, I figured out I could just store the input for the sqrt function as a variable.

Requesting close...


Posts: 20000 - Show all posts

Offline

 

#3 2012-12-08 15:22:43

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: Casting help

Closed by request of the topic owner.


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

Board footer