...

# Maths - Squaring Variables

sir pannels
10-26-2006, 09:28 AM
Hi there,

I have a variable which I'm dividing ..

var ltotal = (data/12)

I now need to sqaure this, and also cube it after that ..

Not sure at all how to square and then cube this result, seperately .. can anyone give me some pointers?

thanks all
p :D

GJay
10-26-2006, 09:41 AM
the 'Math' object (built-in) has a 'pow()' function that given 2 arguments, returns the first, to the power of the second.
i.e.
[code]
var a = Math.pow(3,2);
//a=9
var b = Math.pow(2,3);
//b=8

sir pannels
10-26-2006, 09:45 AM
Hi Jay,

I saw Math.pow on javascriptkit but got stuck with it...

This is what I had ...

var y = Math.pow('total',2);
var x = Math.pow('total',3);
total = y + x;

So the above was meant to square total, and then cube total, and then add them both toegther...

but it prints NaN

No idea what that means or what I did wrong, any thoughts?

cheers :)
p

sir pannels
10-26-2006, 10:02 AM
yo

after reading some more... the NaN is returned because I also use Math.log ... the variable that is returned is negitive ...

Does anyone know how to print a negitive variable, rather than printing NaN?

thank you
p :)

sir pannels
10-26-2006, 10:05 AM
infact scrap that, i have look into it and the variable that is outputted should not be negitive.. so something is wrong ..

calculation is this ...

var data = 2500
var total = (data+1);
var total = Math.log(total);
var y = Math.pow('total',2);
var x = Math.pow('total',3);
total = y + x;

Now, total prints NaN .. but how can that be negitive from the above?

any ideas?

thanks again
p

Philip M
10-26-2006, 11:49 AM
Remove the ' quotes around 'total'.

'total' in quotes is a literal string varaible, which is of course NaN.

sir pannels
10-26-2006, 11:56 AM
hi phil thansk for replying ..

took out the quotes. still get NaN ...

any further thoughts?

thank you

sir pannels
10-26-2006, 12:03 PM
Had a look again and it returns NaN before it does the math.pow.. it gets NaN from the math.log ...

the data var is not 1000 in the real thing it is actually 2.4501.01325 ... is it a problem that it has decimal places etc?

many thanks :)

Philip M
10-26-2006, 12:37 PM
Works fine for me.

<SCRIPT>
var data = 2500;
var total = (data+1);
var total = Math.log(total);
var y = Math.pow(total,2);
var x = Math.pow(total,3);
total = y + x;
</SCRIPT>

Result is 540.249823..........

sir pannels
10-26-2006, 03:44 PM
Thanks again Phil ..

Started using your alerts to debug.. found the problem, variable names clashing ...

d;'oh.

thanks for all your help, great work! :)