#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,153
    Rep Power
    220

    A simple math problem with PHP


    Hi;

    I had asked the similar thing before.

    I want to come up with a formula that calculates width, length and height based on the ratio.

    The ratio is 0.63 which is very simple:
    Code:
    width = height = length/0.63
    So to calculate these values based on the volume of a rectangular tank (which is 83 cubic inch ) I thought:

    PHP Code:
    $dimentions['width'] = $dimentions['height'] = round(pow(83/3,0.63)) ;
     
    $dimentions['length'] = $dimentions['width']/(0.63); 
    But I don't get correct results. It's very off! I get
    83 cubic inch ===> Array ( [height] => 8 [width] => 8 [length] => 12.6984126984 )
    I am looking for bbc5f715bcd97caec4c79ff949a8fa1a6419cfaf54.png
    What am I doing wrong lord?

    Thanks
    Last edited by English Breakfast Tea; April 29th, 2015 at 02:00 AM.
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,263
    Rep Power
    9645
    So... the end face of the rectangular prism is square (width = height) and the each of those is the length / 0.63 (that is, roughly 1.5 times longer)?

    Code:
    width * height * length = volume
      width = height = A
    A * A * length = volume
      A = length / 0.63
      A * 0.63 = length
    A * A * A * 0.63 = volume
    A**3 * 0.63 = volume
    A**3 = volume / 0.63
    A = (volume / 0.63) ** (1/3)
    PHP Code:
    $width $height pow($volume 0.631/3);
    $length $width 0.63
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,153
    Rep Power
    220
    Originally Posted by requinix
    So... the end face of the rectangular prism is square (width = height) and the each of those is the length / 0.63 (that is, roughly 1.5 times longer)?

    Code:
    width * height * length = volume
      width = height = A
    A * A * length = volume
      A = length / 0.63
      A * 0.63 = length
    A * A * A * 0.63 = volume
    A**3 * 0.63 = volume
    A**3 = volume / 0.63
    A = (volume / 0.63) ** (1/3)
    PHP Code:
    $width $height pow($volume 0.631/3);
    $length $width 0.63
    Hi;

    This works better:
    $dimentions['width'] = $dimentions['height'] = round(pow($_REQUEST['maximum_volume_inch'],1/3)) ;
    $dimentions['length'] = round($dimentions['width']/(0.63));
    Edit:

    My problem (well, main one) was not knowing how to use pow properly.
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,263
    Rep Power
    9645
    But that's not what you described. And you're rounding too early: the length should be calculated using the original, unrounded value of the width (or height).
  8. #5
  9. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,464
    Rep Power
    70
    So, is the response your equation is offering you close to the following?
    When standing in front of a tank...
    Length: 5.95 (The longer measurement, left to right)
    Width: 3.7485 (Depth, front to back)
    Height: 3.7485 (Top down)
    This offers the .63 ratio on the longer, not square, faces of your tank, and comes just a hair over the 83 for your volume.

    Would these be (or close to, if rounding) the numbers you wish your equation to hit? Or were we aiming something else? If something else, and the answer has not been achieved, can you manually calculate what you wish your code to achieve, and let us know?

    I'm kinda lost as to the use of the 1/3 power. Unless we're each drawing this tank a little differently in our heads...
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  10. #6
  11. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,263
    Rep Power
    9645
    Originally Posted by Triple_Nothing
    I'm kinda lost as to the use of the 1/3 power. Unless we're each drawing this tank a little differently in our heads...
    If you write the volume in terms of the width (for example), you end up with width * width [because width = height] * width / 0.63 [the length] = volume. That rearranges to width ** 3 = 0.63 * volume, thus width = third root of (0.63 * volume). And the Nth root of a thing is equivalent to the thing raised to the power 1/N.
    Testing with your numbers, 83 * 0.63 = 52.29, third root of that is 3.7394. Which should be a little less than your values due to the "just a hair over the 83" bit.

    Comments on this post

    • Triple_Nothing agrees : Nice job! ;-)
    • Will-O-The-Wisp agrees

IMN logo majestic logo threadwatch logo seochat tools logo