/    Sign up×
Community /Pin to ProfileBookmark

Javascript arithmetic error

Hi folks. I’m going a little crazy here. I’m running into a javascript multiplication issue. Here’s the sample code (entire page); it just calculates a floating point multiplication problem and prints it to the screen:

[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD
XHTML 1.1//EN” “http://www.w3.org/TR/2001/REC-xhtml11-20010531/DTD/xhtml11-flat.
dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Language” content=”en-gb” />
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<meta name=”robots” content=”index, follow” />
<title>JS Test</title>
<script type=”text/javascript”>
//<![CDATA[
function jstest(){
document.getElementById(“testresult”).appendChild(
document.createTextNode(“.4 * .4 = “+(.4 * .4))
);
}
//]]>
</script>
</head>
<body onload=”jstest()”>
<p id=”testresult”></p>
</body>
</html>
[/code]

Everywhere I’ve tested this (IE, FF, Safari), it gives a page with the same result:

[B].4 * .4 = 0.16000000000000003[/B].

Where the heck is that .00000000000000003 coming from? Anyone seen this before?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangJul 09.2007 — All JavaScript maths uses floating point arithmetic, it is prudent to round off values.
Copy linkTweet thisAlerts:
@angrytunaauthorJul 09.2007 — I suppose so. I've replicated the result in mysql:

<i>
</i>select .400000000000000000000000 * .4000000000000000000000000;
+--------------------------------------------------------+
| .400000000000000000000000 * .4000000000000000000000000 |
+--------------------------------------------------------+
| 0.1600000000000000310862447 |
+--------------------------------------------------------+


So I suppose it's nothing more than a consequence of multiplying float values limited register size. In my solution, I've already rounded appropriately; I just don't like that it's there. Where are those extra numbers coming from? What calculation is being done when it proceeds beyond the end of the register?
Copy linkTweet thisAlerts:
@Orc_ScorcherJul 09.2007 — 0.4 is not a number that can be exactly represented in floating point arithmetic, instead the computer's will use the best possible approximation:

0.40000000000000002220446049250313080847263336181640625
×

Success!

Help @angrytuna spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...