View Full Version : Maths - Squaring Variables

sir pannels
10-26-2006, 08: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

10-26-2006, 08:41 AM
the 'Math' object (built-in) has a 'pow()' function that given 2 arguments, returns the first, to the power of the second.
var a = Math.pow(3,2);
var b = Math.pow(2,3);

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

Thanks for the reply..

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 :)

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

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, 09: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

Philip M
10-26-2006, 10: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, 10:56 AM
hi phil thansk for replying ..

took out the quotes. still get NaN ...

any further thoughts?

thank you

sir pannels
10-26-2006, 11:03 AM
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?

please let me know
many thanks :)

Philip M
10-26-2006, 11:37 AM
Works fine for me.

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

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

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

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


thanks for all your help, great work! :)