Updated September 8, 1998 JavaScript Math Errors in Netscape & Internet ExplorerSTUART WHITMORE was having a bad week. He was wrestling with some JavaScript math for a web development job, and the numbers just weren't coming out right. The errors weren't large, but they were real, and curiously, they seemed to vary depending on what web browser he was using. Finally, he put together some JavaScript test problems, and the Basic Math Errors in JavaScript page was born. Whitmore's original aim was to collect information on the problem, but he quickly demonstrated just how inaccurate simple multiplication in JavaScript can be. Tests by BugNet indicate that the latest versions of both Netscape Communicator and Microsoft Internet Explorer can make small mathematical errors in relatively simple multiplication problems carried out with JavaScript. Consider the problem 81.66 times 15. The answer is 1224.9, but Netscape 2, 3, and 4 in Windows 95 may calculate the answer as 1224.8999999999999. This same calculation carried out via JavaScript in Internet Explorer 3 in Windows 95 may work correctly, but Internet Explorer 4 may get it wrong. Meanwhile, Opera 2.x can handle 81.66 times 15 fine, but can trip on 266.66 times 15. "Internet Explorer 3 seems to be the most solid browser for JavaScript math," said Whitmore. "Pretty sad that it even makes a difference." Try it yourself. The following table lets you test the accuracy of your browser. (For the test to work, you must be using a JavaScript compliant browser, and have the feature enabled.) See the Basic Math Errors in JavaScript page at http://www.benesphere.com/javascript_math_errors.html for more tests. Basic Math Errors in JavaScriptMICROSOFT AND NETSCAPE'S response? The two big browser makers both say that 1224.8999999999999 is in fact the correct answer to the multiplication problem, 81.66 times 15. Before you fall on the floor laughing, please realize that there is more than one type of accuracy. In terms of mathematical accuracy, 81.66 times 15 unequivocally equals 1224.9, as any sixth grader with a pencil and a piece of paper can demonstrate. But in terms of accuracy as defined by the IEEE [Institute of Electronic Engineers] floatingpoint math standard, 1224.8999999999999 is the correct answer. In other words, the prevailing computer standard mandates a mathematically incorrect answer to the problem, 81.66 times 15. At the root of this mathematical inaccuracy is the old issue of conversion from decimal to binary numbers. The decimal number 81.66 can not be exactly translated into binary form, and as a result the answer to the problem is slightly incorrect, in mathematical terms. According to Dave Bottoms, Netscape Communicator product manager, mathematical errors of this sort are not unique to Netscape or JavaScript. "JavaScript in IE gets the same answers, but so would applications written in Java or C or C++ using IEEE754 double precision floating point (which is in hardware on all modern architectures)." In a rare show of unanimity, Microsoft's Wascha declared, "to call this a bug in a specific product is false and entirely misleading. It runs deep and affects every programming language on both the Mac and Windows." WHITMORE, WHO has nearly twenty years experience as a computer programmer, begs to differ. He notes that the same calculation errors don't show up when the math is performed via Perl, the most common scripting language on the Web. According to Whitmore, The Perl code: $Result = 81.66 * 15; will print: "The result is 1224.9," not 1224.8999999 as is the case with JavaScript on the same PC. Whitmore continued, "isn't the role of a scripting language to provide simple, efficient programming? Making people reinvent (or at least rewrite) rounding is a pretty substantial failure on the part of a scripting language." Excel and other calculation applications like the Windows 95 calculator provide the mathematically correct answers to the problems that trip JavaScript and other programming languages through rounding, which amounts to essentially correcting back for the unavoidable rounding that occurred in the original binary conversion. Neither JavaScript and Microsoft's JScript semiclone presently include this rounding ability. Microsoft said it would offer a patch which would introduce user control over rounding, but then retracted the promise the next day. "We're discussing it," said Wascha. "The problem is if we introduce rounding then we'll get developers yelling at us because they expect us to adhere to the IEEE and ECMA [European Computer Manufacturers Association] standards, and in these terms the rounded [mathematically correct] answer is the wrong answer." For now, there is no fix and no workaround for this problem, except to keep a pocket calculator handy and not believe everything you read on the screen. The answer supplied by your PC may be wrong. And the wrong answer may be right.  Bruce Brown © BugNet material copyright 19941999 by BugNet. This historic replica of BugNet from the period 19941999 BF Communications Inc. Website by Running Dog
