Videos
PHP uses double-precision floating point numbers. Neither of the results of the two logarithms can be represented exactly, so the result of dividing them is not exact. The result you get is close to, but slightly less than 3. This gets rounded to 3 when being formatted by echo. floor, however returns 2.
You can avoid the inexact division by taking advantage of the fact that log(x, b) / log(y, b) is equivalent to log(x, y) (for any base b). This gives you the the expression log(238328, 62) instead, which has a floating point result of exactly 3 (the correct result since 238328 is pow(62, 3)).
It's due to the way floating point numbers are polished in PHP.
See the PHP Manual's Floating Point Numbers entry for more info
A workaround is to floor(round($value, 15));. Doing this will ensure that your number is polished quite accurately.
floor() will simply drop decimal value and return only integer.
So floor(1.2) => 1 and floor(1.9) => 1.
Meanwhile round() will round number that has decimal value lower than 0.5 to lower int, and when more than 0.5 to higher int:
So round(1.2) => 1 but round(1.9) => 2
Also round() has more options, like precision and rounding options.
Example:
$nums = [-1.5, -1, -.8, -.4, 0, .4, .8, 1, 1.5];
echo " \tround\tfloor\tceil" . PHP_EOL;
foreach ($nums as $a) {
echo $a . ": \t" . round($a) . "\t" . floor($a) . "\t" . ceil($a) . PHP_EOL;
}
/*
round floor ceil
-1.5: -2 -2 -1
-1: -1 -1 -1
-0.8: -1 -1 -0
-0.4: -0 -1 -0
0: 0 0 0
0.4: 0 0 1
0.8: 1 0 1
1: 1 1 1
1.5: 2 1 2
*/
floor() will always remove the values after the decimal, and only rounds down. round() will round up if the value after the integer provided is equal or higher than .5, else it will round down.
Example 1: round(1.5) returns 2 while floor(1.5) returns 1.
Example 2: Both round(3.2) and floor(3.2) return 3.
Example 3: round(2.9) returns 3 while floor(2.9) returns 2.