Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    Maths - Squaring Variables

    Hi there,

    I have a variable which I'm dividing ..

    PHP Code:
    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

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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
    p

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Remove the ' quotes around 'total'.

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

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hi phil thansk for replying ..

    took out the quotes. still get NaN ...

    any further thoughts?

    thank you

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    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

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Works fine for me.

    <SCRIPT>
    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);
    </SCRIPT>


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

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks again Phil ..

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

    d;'oh.

    thanks for all your help, great work!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •