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.
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
Your implementation is different from the required.
You should ensure operator precedence rule is correctely observed:
return $m1 / ((($m2 - $m3) / $pw) - (($m2 - $m1) / $psm)) ;
Related
I made a simple weight tracking app in PHP and am comparing it with what I used to have in Google Sheets, and I'm noticing a difference in a number coming back from "floor".
In Google Sheets:
=FLOOR(84.614285714286 / ((179 / 100) ^ 2))
results in: "26"
in PHP 8:
floor($currentWeight / (($user['height'] / 100)^2));
Results in "28". When I have PHP spit out the value of these variables they are:
$currentWeight = 84.614285714286
$user['height'] = 179
($currentWeight / (($user['height'] / 100)^2)) = 28.204761904762
What could be the reason for this? Which is right?
Use the pow method provided by PHP to calculate powers.
Use pow((179 / 100), 2) instead of (179 / 100) ^ 2 in the PHP part.
So, the updated last line should be:
$result = floor($currentWeight / (pow($user['height'] / 100), 2));
Demo: https://3v4l.org/3VUP1
Output: 26
I'm making a calculator for pipe fittings. The idea is that the user inputs the angle of the turn, then the calculator will tell you how many of what fittings to use. I have access to 8°, 11.25°, 22.5°, 45°, and 90° fittings. But, I can simplify it into 8° and 11.25° fittings, since 22.5°, 45°, and 90° are multiples of 11.25. I can take the # of 11.25 degree fittings, then use the following code to break it into larger fittings.
$num90 = floor($angle11 / 90);
$runningtotal = $angle11 - $num90 * 90;
$num45 = floor($runningtotal / 45);
$runningtotal = $runningtotal - $num45 * 45;
$num22 = floor($runningtotal / 22.5);
$runningtotal = $runningtotal - $num22 * 22.5;
$num11 = floor($runningtotal / 11.25);
$runningtotal = $runningtotal - $num11 * 11.25;
echo "You will need:";
echo "<br>";
echo "$num90 -- 90° fittings";
echo "<br>";
echo "$num45 -- 45° fittings";
echo "<br>";
echo "$num22 -- 22.5° fittings";
echo "<br>";
echo "$num11 -- 11.25° fittings";
Basically, I need to solve the equation:
"8x + 11.25y = angle"
Where "angle" is a known value, and X and Y are integers.
I've made a list of all the possible angles using these fittings, so the program will use the closest possible angle to their request (e.g. if they need a 150° turn, they'll be shown the fittings needed for a 150.5° connection, which is possible). That means that X and Y will be whole numbers. I already have the code to select the closest angle, I'm not worried about it.
I've looked into solutions for the Change Making Problem, which deals with something extremely similar. Most of the equations and algorithms they found go way over my head in terms of complexity. I'm a recent high school graduate, so my math level isn't as good as others.
How would I go about solving this equation? Is this maybe too complicated for me, a beginner? Am I overlooking some super simple solution?
Or, should I just use the wolframalpha API to offload the math onto their side?
Any help would be very appreciated!
Try this:
print_r(get_best_fit(150));
function get_best_fit($angle){
$a = 8;
$b = 11.25;
$best_diff = $angle;
$best_fit = [0,0];
for($x = 0; $x <= $angle/$a; $x++){
$y = round(($angle-$a*$x)/$b);
$diff = $angle-($x*$a+$y*$b);
if(abs($diff) < $best_diff){
$best_diff = abs($diff);
$best_fit = [$x,$y];
}
}
return $best_fit;
}
Output
Array ( [0] => 16 [1] => 2 )
So you would need 16 x 8 + 2 x 11.25.
How do I calculate the percentage of increase or decrease of two numbers in PHP?
For example: (increase)100, (decrease)1 = -99%
(increase)1, (decrease)100 = +99%
Before anything else you need to have a solid understanding of the meaning of percentages and how they are computed.
The meaning of "x is 15% of y" is:
x = (15 * y) / 100
The arithmetic operations with percentages are similar. If a increases with 12% (of its current value) then:
a = a + (12 * a) / 10
Which is the same as:
a = 112 * a / 100
Subtracting 9% (of its current value) from b is:
b = b - (9 * b) / 100
or
b = b * 91 / 100
which actually is 91% of the value of b (100% - 9% of b).
Turn the above a, b, x, y into PHP variables (by placing $ in front of them), terminate the statements with semicolons (;) and you get valid PHP code that performs percentage operations.
PHP doesn't provide any particular function that helps working with percentages. As you can see above, there is no need for them.
My 2 cents ;)
Using PHP
function pctDiff($x1, $x2) {
$diff = ($x2 - $x1) / $x1;
return round($diff * 100, 2);
}
Usage:
$oldValue = 1000;
$newValue = 203.5;
$diff = pctDiff($oldValue, $newValue);
echo pctDiff($oldValue, $newValue) . '%'; // -79.65%
Using Swift 3
func pctDiff(x1: CGFloat, x2: CGFloat) -> Double {
let diff = (x2 - x1) / x1
return Double(round(100 * (diff * 100)) / 100)
}
let oldValue: CGFloat = 1000
let newValue: CGFloat = 203.5
print("\(pctDiff(x1: oldValue, x2: newValue))%") // -79.65%
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
Adding fractions number yields different result in PHP
$grand_total = (float)$subtotal_email + (float)$delivery_email + (float)$fuel_surcharge_email - (float)$discount_coupon_email + (float)$texas_tax_email - (float)$cancel_fee_email - (float)$refund_email - (float)$refund_tax_email - (float)$coupon_tmp;
echo (float)$subtotal_email." + ".(float)$delivery_email." + ".(float)$fuel_surcharge_email." - ".(float)$discount_coupon_email." + ".(float)$texas_tax_email." - ".(float)$cancel_fee_email." - ".(float)$refund_email." - ".(float)$refund_tax_email." - ".(float)$coupon_tmp." = ".(float)$grand_total;
When I run the above in php, I get the following output:
89.99 + 0 + 16.2 - 0 + 8.61 - 3 - 100 - 10 - 1.8 = -2.88657986403E-15
But if you look at LHS, it should be 0, and this happens with or without float....any idea why?
Floating point arithmetic is never that accurate. If you need to compare to zero, you need to take the different and compare it to some small number.
if (abs($result) < 0.00001)) {
// it's zero
} else {
}
Because float. Use ints and calculate the value with cents (* 100).
I need help to solve this formula ((n * 2) + 10) / (n + 1) = 3, preferably in PHP. (The numbers 2, 10 and 3 should be variables that can be changed.)
I'm able to solve this equation on paper quite easily. However, when I try to implement this in PHP, I'm not sure where to start. I've done several Google queries and searches on here and nothing seems to help. I'm missing the proper approach to deal with this problem.
Any tips and pointers would be great, and if you provide the exact code, please explain how you got to this result.
You're wanting to solve an equation, not implement it. There's a difference. Implementing the equation would be as simple as typing it in. You'd probably want to make it an equality operator (==) though.
Equation solvers are complicated, complicated things. I wouldn't try to make one when there are such good ones ( http://en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems ) lying around.
You can use http://pear.php.net/package/PHP_ParserGenerator/redirected to parse the math expressions into a syntax tree, then do the maths.
((n * 2) + 10) / (n + 1) = 3 would look like:
The idea is to bring on the right subtree (here ...) all the numbers, and on the left all the unknownws, just as you'd do on paper.
In the end you'll have:
+
/ \
n -7
which is 0. And there you have your solution, for any math expression (with one unknown variable).
I'll leave the algorithm to you.
<?php
// ((x * n) + y)/(n + 1) = z)
// => n=(y-z)/(z-x)
function eq ($x=0,$y=0,$z=0)
{
if ($z!=$x)
{
$n=($y-$z)/($z-$x);
} else
{
$n='NAN';
}
return $n;
}
?>
(My algebra is old and flakey but I think this is right)
how about using brute-force??!?! might be slow and not exact:
$step = 0.00001;
$err = 0.1; //error margin
$start = 0;
$response = 3;
for($i = $start;$i <= 3;$i += $step){
if((($i * 2) + 10) / ($i + 1) >= $response - $err){
echo "the answer is $i";
}
}
You could improove this answer.. on every loop you could calculate the distance between the current answer and the desired answer, and adjust the parameters acording to that..
This reminds me my old A.I. class =)
Good Luck
Here's how to solve that equation in C# with the Symbolism computer algebra library:
var n = new Symbol("n");
(((n * 2) + 10) / (n + 1) == 3)
.IsolateVariable(n)
.Disp();
The following is displayed on the console when that code is executed:
n == 7