Reputation: 623
i have a client that wants to turn this calculation into a function in PHP. i been given test numbers with an answer for it and i am out from this answer by too much for it to be correct im not sure if the calculation is wrong or im just not seeing the issue.
function Density($m1 , $m2, $m3, $pw, $psm){
return $m1 / (($m2 - $m3) / $pw) - (($m2 - $m1) / $psm) ;
}
$Density = ( Density(746.2, 761.7, 394.6, (998.1*1000), (761.7-746.2)) / 1000000)
output : 2.02882553228
answer : 2.127
also i have tried it like this as well but this is way far out for it to be right
function Density($m1 , $m2, $m3, $pw, $psm){
return $m1 / ((($m2 - $m3) / $pw) - (($m2 - $m1) / $psm ));
}
output : -0.001
answer : 2.127
i know it should be as close to the answer as it can get 0.010 out fromthe correct answer but i dont see what im doing wrong please help internet.
Upvotes: 0
Views: 439
Reputation: 14868
Your implementation is different from the required.
You should ensure operator precedence rule is correctely observed:
return $m1 / ((($m2 - $m3) / $pw) - (($m2 - $m1) / $psm)) ;
Upvotes: 0
Reputation: 360702
Remember BEDMAS - brackets exponents division multiplication addition subtraction. Your code is wrong for the order-of-operations:
m1 / (((m2 - m3) / pw) - ((m2 - m1) / psm))
or, if that equation had been typeset properly:
m1
--------------------
m2 - m3 m2 - m1
------- - -------
pw psm
Upvotes: 3