473,738 Members | 9,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does 1.4 - 0.5 result in 0.8999999999999 999?

I use IE 6.0 and does some calculations in a javascript.

If I run this code:
alert(1.4 - 0.5);

i get the result 0.8999999999 and not 0.9
Does anybody know why? Is this a bug in IE or what? Any fast and easy
ways to work around this problem?

B.R
The Duke
Jul 23 '05 #1
10 2908
On 2 Jun 2004 02:04:54 -0700, Fredrik Celin wrote:
i get the result 0.8999999999 and not 0.9


<http://www.jibbering.c om/faq/#FAQ4_7>

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Jul 23 '05 #2
In article <fb************ **************@ posting.google. com>, Fredrik
Celin <fr***********@ bilia.se> wrote:
I use IE 6.0 and does some calculations in a javascript.

If I run this code:
alert(1.4 - 0.5);

i get the result 0.8999999999 and not 0.9
Does anybody know why? Is this a bug in IE or what? Any fast and easy
ways to work around this problem?

B.R
The Duke

Computers use binary for computations. Some fractions do not convert
exactely. My advice is always round off to the number of decimal places
required.

--
Dennis Marks
http://www.dcs-chico.com/~denmarks/
Mail to the return email address is bounced.
Go to web site for active email address.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 23 '05 #3
"Fredrik Celin" <fr***********@ bilia.se> wrote in message
news:fb******** *************** ***@posting.goo gle.com...
I use IE 6.0 and does some calculations in a javascript.

If I run this code:
alert(1.4 - 0.5);

i get the result 0.8999999999 and not 0.9
Does anybody know why? Is this a bug in IE or what? Any fast and easy
ways to work around this problem?


Computers don't compute, sorry for any confusion. They can store a number in
memory, or retrieve a number from memory. They can also add one to a number,
or subtract one from a number. They can also add a number to another number.
That's about it.

--
Mabden
Jul 23 '05 #4
Mabden wrote:
"Fredrik Celin" <fr***********@ bilia.se> wrote in message
news:fb******** *************** ***@posting.goo gle.com...
I use IE 6.0 and does some calculations in a javascript.

If I run this code:
alert(1.4 - 0.5);

i get the result 0.8999999999 and not 0.9
Does anybody know why? Is this a bug in IE or what? Any fast and easy
ways to work around this problem?


Computers don't compute, sorry for any confusion. They can store a number in
memory, or retrieve a number from memory. They can also add one to a number,
or subtract one from a number. They can also add a number to another number.
That's about it.

--
Mabden


That's not really an explanation for what he's experiencing. For example take:
0.05+0.01

The above operation probably involved storing number(s) in memory, retrieving
number(s) from memory and adding a number to another number. All of these things
are things you say a computer can do, yet the result is not equal to 0.06. In
fact, the outcome of the operation 0.05+0.01 has nothing to do with a computers
ability to "compute", it has to do with a computers ability to represent decimal
values in binary.

If the question the OP posed were phrased like this

"If I run this code alert(one-third + two-thirds); I get 0.999999999... and not
1"

It becomes obvious why this occurred if you change the representation of
"one-third" to 0.33333.... and "two-thirds" to 0.666666... 0.3333... +
0.6666... = 0.9999..., not 1. No one would suggest for a moment that the human
doing this math isn't "computing" right, or is only capable of storing numbers
in their brain and adding them.

It's simply an issue of representation and loss of precision.

As for the solution, the solution is to round the result to the appropriate
level of precision required for the task. If the numbers you are computing
involve money, it's usually best to do all your math using integers only (ie -
store the values as number of cents, or tenths of cents, or whatever level of
precision is required) and do the final formatting on output of the result. In
other words, don't add 0.05 dollars to 0.01 dollars. Add 6 cents to 1 cent, then
position the decimal point two digits from the right when outputting the result
(note: don't divide by 100, because you risk introducing the same problem you
are trying to avoid, instead, actually output the result as a string, with the
decimal point positioned when you require it).

--
| Grant Wagner <gw*****@agrico reunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #5
Lee
Fredrik Celin said:

I use IE 6.0 and does some calculations in a javascript.

If I run this code:
alert(1.4 - 0.5);

i get the result 0.8999999999 and not 0.9
Does anybody know why? Is this a bug in IE or what? Any fast and easy
ways to work around this problem?


The value I get is 0.8999999999999 999

To put that result in perspective, the error is only slightly
more than 0.0000000000000 1 percent.

That same amount of error in measuring the distance around the
Earth at the equator would put you off by about one five-millionth
of an inch.

So you can see that the result is more than accurate enough for
any practical purpose. It just looks bad.
To make it look nice, you round it off, as described elsewhere.

Jul 23 '05 #6
"Grant Wagner" <gw*****@agrico reunited.com> wrote in message
news:40******** *******@agricor eunited.com...
Mabden wrote:
"Fredrik Celin" <fr***********@ bilia.se> wrote in message
news:fb******** *************** ***@posting.goo gle.com...
I use IE 6.0 and does some calculations in a javascript.

If I run this code:
alert(1.4 - 0.5);

i get the result 0.8999999999 and not 0.9
Does anybody know why? Is this a bug in IE or what? Any fast and easy
ways to work around this problem?
Computers don't compute, sorry for any confusion. They can store a number in memory, or retrieve a number from memory. They can also add one to a number, or subtract one from a number. They can also add a number to another number. That's about it.

--
Mabden


That's not really an explanation for what he's experiencing. For example

take: 0.05+0.01

The above operation probably involved storing number(s) in memory, retrieving number(s) from memory and adding a number to another number. All of these things are things you say a computer can do, yet the result is not equal to 0.06. In fact, the outcome of the operation 0.05+0.01 has nothing to do with a computers ability to "compute", it has to do with a computers ability to represent decimal values in binary.


I should have said "integer" where I said "number", which is what I meant.
By saying 0.05, you are implying a division operation (5/100) which was not
one of the operations on my list.

--
Mabden
Jul 23 '05 #7
JRS: In article <40************ ***@agricoreuni ted.com>, seen in
news:comp.lang. javascript, Grant Wagner <gw*****@agrico reunited.com>
posted at Fri, 4 Jun 2004 14:57:56 :

If the question the OP posed were phrased like this

"If I run this code alert(one-third + two-thirds); I get 0.999999999... and not
1"

It becomes obvious why this occurred if you change the representation of
"one-third" to 0.33333.... and "two-thirds" to 0.666666... 0.3333... +
0.6666... = 0.9999..., not 1. No one would suggest for a moment that the human
doing this math isn't "computing" right, or is only capable of storing numbers
in their brain and adding them.

It's simply an issue of representation and loss of precision.


Not a good example, since in javascript 1/3 + 2/3 gives exactly 1, and
numbers are NOT stored as decimals.

But (0.3 - 0.2) == 0.1 is false.

The OP should have read the newsgroup FAQ.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8
Dr John Stockton wrote:
JRS: In article <40************ ***@agricoreuni ted.com>, seen in
news:comp.lang. javascript, Grant Wagner <gw*****@agrico reunited.com>
posted at Fri, 4 Jun 2004 14:57:56 :

If the question the OP posed were phrased like this

"If I run this code alert(one-third + two-thirds); I get 0.999999999... and not
1"

It becomes obvious why this occurred if you change the representation of
"one-third" to 0.33333.... and "two-thirds" to 0.666666... 0.3333... +
0.6666... = 0.9999..., not 1. No one would suggest for a moment that the human
doing this math isn't "computing" right, or is only capable of storing numbers
in their brain and adding them.

It's simply an issue of representation and loss of precision.
Not a good example, since in javascript 1/3 + 2/3 gives exactly 1, and
numbers are NOT stored as decimals.


Which is why I wrote "one-third" and "two-thirds", and not 1/3 and 2/3. My example
was to show the same errors he accuses computers of making because they "don't
compute" can occur when humans do the math: 1 + 2 = 3, 3/3 = 1; but 0.333... +
0.666... != 1. It has to do with how values are represented. A ratio of one over
three _exactly_ represents the value with nothing repeating or being an
approximation, whereas the decimal value of 1 divided by 3 can only be an
approximation and does not exactly represent the value of the ratio (and never can).

In the same way, some values stored in decimal can exactly represent a value (0.3 or
0.2), but when that same value is stored in binary, it can only be stored as an
approximation.

I can exactly store a value represented as "2 x sqrt(2)", but the resulting value
would most likely be an approximation in both decimal and binary. In fact, with
imaginary numbers, you can exactly represent values which you can not even
approximate in decimal (or binary).

Representation is all important.
The OP should have read the newsgroup FAQ.


The OP was directed to the FAQ, I was responding to someone who had responded to the
OP with a flippant "computers don't compute and that's why numbers don't get
calculated right" remark.

--
| Grant Wagner <gw*****@agrico reunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #9
Mabden wrote:
"Grant Wagner" <gw*****@agrico reunited.com> wrote in message
news:40******** *******@agricor eunited.com...
Mabden wrote:
"Fredrik Celin" <fr***********@ bilia.se> wrote in message
news:fb******** *************** ***@posting.goo gle.com...
> I use IE 6.0 and does some calculations in a javascript.
>
> If I run this code:
> alert(1.4 - 0.5);
>
> i get the result 0.8999999999 and not 0.9
>
>
> Does anybody know why? Is this a bug in IE or what? Any fast and easy
> ways to work around this problem?

Computers don't compute, sorry for any confusion. They can store a number in memory, or retrieve a number from memory. They can also add one to a number, or subtract one from a number. They can also add a number to another number. That's about it.

--
Mabden


That's not really an explanation for what he's experiencing. For example

take:
0.05+0.01

The above operation probably involved storing number(s) in memory,

retrieving
number(s) from memory and adding a number to another number. All of these

things
are things you say a computer can do, yet the result is not equal to 0.06.

In
fact, the outcome of the operation 0.05+0.01 has nothing to do with a

computers
ability to "compute", it has to do with a computers ability to represent

decimal
values in binary.


I should have said "integer" where I said "number", which is what I meant.
By saying 0.05, you are implying a division operation (5/100) which was not
one of the operations on my list.

--
Mabden


Actually, I'm not implying anything. Computers are perfectly capable of storing
the binary approximation of 0.05 and the binary approximation of 0.01 and adding
them. The answer doesn't come out to 0.06 because the binary representations of
those decimal values are approximations in binary.

Whether a CPU can or can't do division is irrelevant.

The issue is that computers can't _exactly_ represent certain floating point
values, they can only _approximate_ them. With more bits you could more
accurately _approximate_ some decimal values, but they would still be
approximations and errors would still creep into calculations involving those
approximated values.

--
| Grant Wagner <gw*****@agrico reunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #10

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

Similar topics

12
2316
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard flattery (but true) that this group has a bunch of the nicest and most knowledgeable Usenet people around, and I know for a fact that there are some pretty good spam- related tools written in Python, so I thought I might get away with it :-) Yes,...
20
10159
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
4
2181
by: David | last post by:
Hi, Buddy, a newbie's question for you guys, In C++, some functions have a return value type "result", what does this mean, I searched on web, but no hint. thanks a lot David
6
23605
by: WindAndWaves | last post by:
Hi Folks I have inhereted a script that I understand reasonably well, I just do not understand !/^\d+$/.test(el.value) what the hell does that mean? Below is the script (there are really three and they validate a four items
2
2221
by: vakap | last post by:
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += '\n'+arguments) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; } function f(){} show('delete(f):',delete(f)) ; // false g = function(){} ;
4
4483
by: P Adhia | last post by:
Hello, If the explain shows that DB2 needs to sort the result of a cursor, does that always happen? i.e. if the resultset of the cursor is empty, does the sort have any overhead? It appears that, majority of the time is spent in allocation and deallocation of the temporary file to hold the data for the sort. Do these activities still take place for an empty cursor? Also, DB2 manual appears to say that only RID sorts are done in
52
3220
by: Julie | last post by:
I'm supporting an application at work. Below are some code segments that I can't understand how they work. First let me say, I would never code this standard. I'm just really creeped out that it works. Here's the setup. I have a function, called GetEmployeeCertifications, that is going to make a call to a SQL DB and return a result set. This module calls another function, called FillParameters, to determine if SQL parameters need to...
3
2829
by: john | last post by:
Hi to All To demonstrate: public class MyBaseGenericClass<T> { } public class MyGenericClass1<T: MyBaseGenericClass<T> {
7
1506
by: Bo Yang | last post by:
Hi , I am reading some boost code now , and I got confused by the following code in the add_reference type trait . template <class TT&(* is_reference_helper1(wrap<T>) )(wrap<T>); char is_reference_helper1(...); template <class Tno_type is_reference_helper2(T&(*)(wrap<T>)); yes_type is_reference_helper2(...);
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
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
6751
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
6053
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
4570
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...
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.