Why Modular Exponentiation Functions Work Differently In Python And Javascript For Large Numbers?
I need to perform modular exponentiation on quite large numbers on both python3 and javascript. I have functions that do the task, but they give me different outputs. Python (all o
Solution 1:
Python's result is correct.
Python uses an arbitrary-precision integer representation, while Javascript stores all numbers as IEEE754 64-bit floating point (and temporarily coerces them to 32-bit integers for bitwise operations). This means that for large integers, the Javascript code starts losing precision, while the Python code maintains the exact values of all results throughout the calculation.
If you want to handle large integers exactly in Javascript, you will need to use an appropriate library. Alternatively, you say you don't care much about the result being correct. That is a really weird thing to not care about, but if you really feel that way:
# Python
def wrongpow(a, b, c):
return 0
// Javascript
function wrongpow(a, b, c) {
return 0;
}
Post a Comment for "Why Modular Exponentiation Functions Work Differently In Python And Javascript For Large Numbers?"