floating-accuracy

What is the standard solution in JavaScript for handling big numbers (BigNum)? [closed]

不羁岁月 提交于 2019-11-25 22:38:39
问题 Is there a bignum library for JavaScript or built-in that I can include like <script type=\"text/javascript\" src=\"the_bignum_library.js\"></script> ? I think my users would prefer to enter numbers in a web page and wait 7 seconds for a result, rather than download an executable and click through a bunch of \"this executable could possibly harm your computer\" warning screens to install it. I\'ve considered basing my own off of http://github.com/silentmatt/javascript-biginteger or http://www

Large numbers erroneously rounded in JavaScript

狂风中的少年 提交于 2019-11-25 21:58:44
问题 See this code: <html> <head> <script src=\"http://www.json.org/json2.js\" type=\"text/javascript\"></script> <script type=\"text/javascript\"> var jsonString = \'{\"id\":714341252076979033,\"type\":\"FUZZY\"}\'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); </script> </head> <body> </body> </html> When I see my console in Firefox 3.5, the value of jsonParsed is: Object id=714341252076979100 type=FUZZY I.e the number is rounded. Tried different values, the same

What&#39;s wrong with using == to compare floats in Java?

久未见 提交于 2019-11-25 21:58:39
问题 According to this java.sun page == is the equality comparison operator for floating point numbers in Java. However, when I type this code: if(sectionID == currentSectionID) into my editor and run static analysis, I get: \"JAVA0078 Floating point values compared with ==\" What is wrong with using == to compare floating point values? What is the correct way to do it? 回答1: the correct way to test floats for 'equality' is: if(Math.abs(sectionID - currentSectionID) < epsilon) where epsilon is a

Floating point inaccuracy examples

こ雲淡風輕ζ 提交于 2019-11-25 21:35:52
问题 How do you explain floating point inaccuracy to fresh programmers and laymen who still think computers are infinitely wise and accurate? Do you have a favourite example or anecdote which seems to get the idea across much better than an precise, but dry, explanation? How is this taught in Computer Science classes? 回答1: There are basically two major pitfalls people stumble in with floating-point numbers. The problem of scale. Each FP number has an exponent which determines the overall “scale”

Python floating point arbitrary precision available?

坚强是说给别人听的谎言 提交于 2019-11-25 19:43:17
Just for fun and because it was really easy, I've written a short program to generate Grafting numbers , but because of floating point precision issues it's not finding some of the larger examples. def isGrafting(a): for i in xrange(1, int(ceil(log10(a))) + 2): if a == floor((sqrt(a) * 10**(i-1)) % 10**int(ceil(log10(a)))): return 1 a = 0 while(1): if (isGrafting(a)): print "%d %.15f" % (a, sqrt(a)) a += 1 This code misses at least one known Grafting number. 9999999998 => 99999.99998999999999949999999994999999999374999999912... It seems to drop extra precision after multiplying by 10**5 . >>>