why this div result is different in php

Viewed 73
$a = 116278458266472831;
echo intval($a/64); // 1816850910413638
echo intdiv($a, 64); //1816850910413637

when I calculate radix of a big number, I found some error, result of intval($a/64) is greater than intdiv($a, 64), could you tell me why ? maybe also you will write the deep reason, thanks a lot.

2 Answers

Based on intval() Manual user contribution notes

intval converts doubles to integers by truncating the fractional component of the number.

When dealing with some values, this can give odd results.

that's why you are getting ...638 in first code.

Based on intdiv() Manual

Returns the integer quotient of the division of dividend by divisor.

That's why you are getting ...637 in second code. As it returns only integer part.

If you wish to calculate precision to a variable amount and ensure your calculations are correct, you shouldn't use the functions in questions.

For such calculations you will need to use PHP's BCMath: https://www.php.net/manual/en/book.bc.php

Related