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

Different result in JS & PHP because of different integer range

<script>
function hs(e, n) {
    return e * n % e;
}
alert(hs(1752865668, 1716717484170));
//result:  1752622684 
</script>

<?php
function hs($e, $n) {
    return $e * $n % $e;
}
echo hs(1752865668, 1716717484170);
//result: 386969652
?>

Both results are different because of their different 53-bit & 32-bit integer range. How can I have the same result that JS calculated in PHP in this senario? (Without using GMP: GNU Multiple Precision) (Using PHP 8+)

In short: I need the PHP code snippet that exactly shows the same result as JS.

Thank you!

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

>Solution :

function hs($e, $n) {
    return bcmod(sprintf('%f', $e * $n), $e);
}

This code gives me the same result, it needs the BCMath extension.


Explanation: Firstly, I found the following facts

BigInt(1752865668 * 1716717484170)      = 3009175139656926232576
1716717484169 * 1752865668 + 1752622684 = 3009175139656926232576

But the precise result should be 3009175139656926475560, so I realized that JavaScript may perform a modulo operation on a double floating number.

But PHP always converts the operands to integers. However the result of $e * $n here has exceeded the range of 64-bit integer, so another way of calculation is needed. Coincidentally, the BCMath extension meets the criteria. The extension requires operands to be strings in integer form, while sprintf converts floating numbers to their integral representations.

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