SitePoint Sponsor 

User Tag List
Results 1 to 6 of 6
Thread: math problem in php

May 10, 2003, 09:30 #1
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!

May 10, 2003, 10:21 #2
 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.

May 10, 2003, 10:21 #3
 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 10:38.

May 10, 2003, 10:27 #4
 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

May 10, 2003, 10:28 #5
 Join Date
 Sep 2001
 Location
 Singapore
 Posts
 5,269
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Originally Posted by website

May 10, 2003, 11:04 #6
got it, thanks mates.. great help
Bookmarks