Why can't Javascript give accurate results for scientific calculations compared to python

D4tech

I tried performing Modular Exponentiation in Javascript to verify an algorithm and was shocked to find that Javascript was not giving accurate results compared to Python, Why is it so. I think it has something to do with the way Javascript handles datatypes(as Text), But I would like to know more about it and I know the purpose why both the langs were designed.

Modular Exponentiation result comparison in .js and .py

poke

It gets a bit clearer, when you look at the intermediary results before the modulo operation first:

> Math.pow(17, 22)
1.1745628765211486e+27
>>> pow(17, 22)
1174562876521148458974062689

As you can see, the Python result has a lot more digits than the JavaScript result. This is due to how each language handles numbers or integers.

Python has an int type, which is basically unlimited: “These represent numbers in an unlimited range, subject to available (virtual) memory only.” So as long as you have memory available, the integers that can be represented with this type can be as big as you want—without any loss in precision.

Enter JavaScript and the ECMA standard. Unlike Python and other languages, we only have a single type responsible for all numeric types: Number. This type holds integers and decimals without any differentiation. They are internally represented as double precision floating point numbers. As such, they are subject to those restrictions, allowing only a certain amount of precision for big numbers. Hence, the best you can get for 17^22 is the above result, with the rest of the precision lost in the process.


If you are not too focused on performance, you could write your own pow function that additional takes a third parameter to apply a modulo operation to the result, similar to how Python’s pow does.

function modpow (base, exponent, modulo) {
    var result = base;
    while (exponent > 1 ) {
        result = (result * base) % modulo;
        exponent--;
    }
    return result;
}

Of course, this is a lot less efficient than the internal pow and only works for integer exponents, but at least it would solve your job correctly:

> modpow(17, 22, 21)
4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"subset" and "[" on dataframe give slightly different results, why?

From Dev

Javascript substracting numbers is not accurate - why

From Dev

Why does t-test in Python (scipy, statsmodels) give results different from R, Stata, or Excel?

From Dev

In JavaScript, in what situation(s) is a === b, but using a or b can give different results?

From Dev

Why won't my JavaScript calculations involving if statements work?

From Dev

Can I grep for results compared to a timestamp?

From Dev

Why can a list be compared with an integer in Python

From Dev

Having in SQL statement isn't returning accurate results

From Dev

Why does a FLOAT give me a more accurate result than a DECIMAL?

From Dev

Publishing interactive scientific results in Python

From Dev

Python max() function doesnt give correct results when two strings are compared

From Dev

Why can't size_t be directly compared with negative int?

From Dev

How can i get accurate resultsof mathematical calculations in objective-c?

From Dev

Why does Find in Excel give unexpected results?

From Dev

Why can't Javascript give accurate results for scientific calculations compared to python

From Dev

Why in Python I can't concatenate string and number like in JavaScript?

From Dev

why int can't be compared to null but Integer can be compared to null

From Dev

javascript: Why does obj.constuctor.name give 2 different results if prototype is missing?

From Dev

Can I grep for results compared to a timestamp?

From Dev

Why can a list be compared with an integer in Python

From Dev

Why the getFont() method can't be called and give out NullPointerException.

From Dev

Why does the "which" command give duplicate results?

From Dev

Why does "cgps -s" give me no results?

From Dev

Math.sin() does not give accurate results [I'm using Math.toRadians to pass the right values]

From Dev

Why doesn't scientific notation work in Numpy

From Dev

SQL can't get the accurate data

From Dev

Why don't my 2 functions give the same results?

From Dev

Python calculations without scientific notation

From Dev

Why JB isn't doing accurate CMP

Related Related

  1. 1

    "subset" and "[" on dataframe give slightly different results, why?

  2. 2

    Javascript substracting numbers is not accurate - why

  3. 3

    Why does t-test in Python (scipy, statsmodels) give results different from R, Stata, or Excel?

  4. 4

    In JavaScript, in what situation(s) is a === b, but using a or b can give different results?

  5. 5

    Why won't my JavaScript calculations involving if statements work?

  6. 6

    Can I grep for results compared to a timestamp?

  7. 7

    Why can a list be compared with an integer in Python

  8. 8

    Having in SQL statement isn't returning accurate results

  9. 9

    Why does a FLOAT give me a more accurate result than a DECIMAL?

  10. 10

    Publishing interactive scientific results in Python

  11. 11

    Python max() function doesnt give correct results when two strings are compared

  12. 12

    Why can't size_t be directly compared with negative int?

  13. 13

    How can i get accurate resultsof mathematical calculations in objective-c?

  14. 14

    Why does Find in Excel give unexpected results?

  15. 15

    Why can't Javascript give accurate results for scientific calculations compared to python

  16. 16

    Why in Python I can't concatenate string and number like in JavaScript?

  17. 17

    why int can't be compared to null but Integer can be compared to null

  18. 18

    javascript: Why does obj.constuctor.name give 2 different results if prototype is missing?

  19. 19

    Can I grep for results compared to a timestamp?

  20. 20

    Why can a list be compared with an integer in Python

  21. 21

    Why the getFont() method can't be called and give out NullPointerException.

  22. 22

    Why does the "which" command give duplicate results?

  23. 23

    Why does "cgps -s" give me no results?

  24. 24

    Math.sin() does not give accurate results [I'm using Math.toRadians to pass the right values]

  25. 25

    Why doesn't scientific notation work in Numpy

  26. 26

    SQL can't get the accurate data

  27. 27

    Why don't my 2 functions give the same results?

  28. 28

    Python calculations without scientific notation

  29. 29

    Why JB isn't doing accurate CMP

HotTag

Archive