/    Sign up×
Community /Pin to ProfileBookmark

Why this calculation is like this

I have Intel P4 1.6 MHz, win XP professional 2002 SP2, apache 2.2.3, php 5 and five major browsers. the following simple javascript code
——————————————

<script type=”text/javascript”>
document.write(8.8+0.8)

</script>
—————————————-


gives output
———————————–


9.600000000000001
——————————

Why? It works fine with PHP. Why should one use data conversion and all other things for this simple calculation.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisNov 09.2006 — I'm afraid you'll just have to live with it.Javascript numbers are represented in binary as IEEE-754 Doubles, with a resolution of 53 bits, giving an accuracy of 15-16 decimal digits; integers up to about 9e15 are precise, but few decimal fractions are. Given this, arithmetic is as exact as possible, but no more. Operations on integers are exact if the true result and all intermediates are integers within that range.[/quote]So if you did(88 + 8) / 10 you will get 9.6. ?
Copy linkTweet thisAlerts:
@CharlesNov 09.2006 — Put more shortly, unlike calculators computers are bad at math.
Copy linkTweet thisAlerts:
@mandar_shikharauthorNov 09.2006 — thanks for the response

Actually I was designing a web calculator

http://www.familyvillage.org.np/xml/calc.html

I'm not sure if i convert the numbers to integers or other limited precision types to limit the calculation capacity of the calculator.
×

Success!

Help @mandar_shikhar 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.17,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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