SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast JohnLee's Avatar
    Join Date
    Sep 2001
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    math problem in php

    Let's say I give an option to my user to input two number values, A and B. Then the form will use them to calculate Y.

    For example
    A = 2.5
    B = 2.8

    So the formula is

    Y * 2.5 - 10,000 = 10,000 * 2.8 - Y / 2
    2.5Y - 10,000 = 28,000 - Y / 2
    2.5Y - 10,000 = 14,000 - 0.5Y
    2.5Y + 0.5Y = 14,000 + 10,000
    3Y = 24,000
    Y = 8,000

    the main question is.... how am I supposed to put that into php coding!

    thanks in advance for any help!

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Y*A - 10000 = (10000B -Y) / 2
    Y*(A+0.5) = 5000(B + 2)
    Y = 5000(B + 2)/(A + 0.5)

    Just use that last formula to find Y. You can pretty much enter it as it is (you just need to replace Y, A and B with the appropriate variables).

    EDIT: just saw Chris' post. My formula above is assuming you meant to have brackets so that it looks like this:
    Y * 2.5 - 10,000 = (10,000 * 2.8 - Y) / 2
    I deduced that from your steps in solving the equation.

  3. #3
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you have an error in your calculation:

    aY - 10,000 = 10,0000b - Y /2
    aY + Y / 2 = 10,000 (b + 1)
    Y(a + 0.5) = 10,000 (b + 1);
    Y = 10,000 (b + 1) / (a + 0.5)

    So the solution for this equation is always:

    Y = 10,000 (b + 1) / (a + 0.5)

    This makes it a lot easier to calculate the result in PHP

    PHP Code:
    $a 2.5;
    $b 2.8;

    function 
    solve($a$b) {
       return 
    10000 * ($b 1) / ($a 0.5);
    }

    echo 
    "Using \$a = $a and \$b = $b the result is: ".solve($a$b); 

    NOOOO not again redemption was again faster than me

    edit: In regards to redemption's post concerning missing brackets and website's suggestion for checking that a != 0.5 here is an updated version:

    PHP Code:
    $a 2.5;
    $b 2.8;

    function 
    solve($a$b) {
       if (
    $a == -0.5
          return 
    'NaN';

       return 
    5000 * ($b 2) / ($a 0.5)
    }

    echo 
    "Using \$a = $a and \$b = $b the result is: ".solve($a$b); 
    Last edited by Chris82; May 10, 2003 at 09:38.

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but, hang on, you can't devide by number that may possible be 0, in this case, if a is set in to be -0,5 the script fails ...
    - website

  5. #5
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    but, hang on, you can't devide by number that may possible be 0, in this case, if a is set in to be -0,5 the script fails ...
    Well good point. It would make sense to validate any user input first, and make sure A isn't -0.5.

  6. #6
    SitePoint Enthusiast JohnLee's Avatar
    Join Date
    Sep 2001
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got it, thanks mates.. great help


Bookmarks

Posting Permissions

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