Home > Software design >  Floor function: Google Sheets vs PHP
Floor function: Google Sheets vs PHP

Time:12-31

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?

CodePudding user response:

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
  • Related