473,800 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FAQ Topic - Why does simple decimal arithmetic give strange results?

-----------------------------------------------------------------------
FAQ Topic - Why does simple decimal arithmetic give strange
results?
-----------------------------------------------------------------------

For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does
not give exactly 0.07 in javascript.

Javascript numbers are represented in binary as IEEE-754 (IEC 559)
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.

In particular, non-integer results should not normally be
compared for equality; and non-integer computed results
commonly need rounding; see 4.6.

http://msdn.microsoft.com/library/de...6dbf483b41.asp
===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javas cript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers.

Aug 15 '07 #1
3 1996
In comp.lang.javas cript message <46************ ***********@new s.sunsite.
dk>, Wed, 15 Aug 2007 23:00:01, FAQ server <ja********@dot internet.be>
posted:
>
For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does
not give exactly 0.07 in javascript.
True; but "exactly 0.07" is ambiguous. How best can we state, briefly
BOTH that (0.06+0.01).toS tring() -"0.069999999999 99999"
AND that (0.06+0.01) - 0.07 != 0.0 // ??

The recent short list of FAQ changes has an error.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Aug 16 '07 #2
Dr J R Stockton wrote:
>
The recent short list of FAQ changes has an error.
Well, you're about as helpful as a government employee.

--
John Hosking
Aug 17 '07 #3
Dr J R Stockton said the following on 8/16/2007 4:27 PM:
In comp.lang.javas cript message <46************ ***********@new s.sunsite.
dk>, Wed, 15 Aug 2007 23:00:01, FAQ server <ja********@dot internet.be>
posted:
>For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does
not give exactly 0.07 in javascript.

True; but "exactly 0.07" is ambiguous. How best can we state, briefly
BOTH that (0.06+0.01).toS tring() -"0.069999999999 99999"
AND that (0.06+0.01) - 0.07 != 0.0 // ??

The recent short list of FAQ changes has an error.
Well, are you going to tell what the error was or simply mention it and
want someone to beg you to disclose what you think the error was?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1957
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 5 * 1.015 != 5.075 or 0.06+0.01 != 0.07? ----------------------------------------------------------------------- Javascript numbers are represented in binary as IEEE-754 (IEC 559) 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,...
2
1581
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does not give exactly 0.07 in javascript. Javascript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an...
1
1246
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does not give exactly 0.07 in javascript. Javascript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an...
2
1899
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, ` 5 * 1.015 ` does not give exactly ` 5.075 ` and ` 0.06+0.01 ` does not give exactly ` 0.07 ` in javascript. ECMAScript numbers are represented in binary as IEEE-754 (IEC 559)
1
1632
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results? ----------------------------------------------------------------------- For example, ` 5 * 1.015 ` does not give exactly ` 5.075 ` and ` 0.06+0.01 ` does not give exactly ` 0.07 ` in javascript. ECMAScript numbers are represented in binary as IEEE-754 (IEC 559)
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.