Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Floor function: Google Sheets vs PHP

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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 = ($currentWeight / (pow($user['height'] / 100), 2));

Demo: https://3v4l.org/3VUP1

Output: 26
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading