Why does 1.4 - 0.5 result in 0.8999999999999999?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredrik Celin

    Why does 1.4 - 0.5 result in 0.8999999999999999?

    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
  • Andrew Thompson

    #2
    Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

    On 2 Jun 2004 02:04:54 -0700, Fredrik Celin wrote:
    [color=blue]
    > i get the result 0.8999999999 and not 0.9[/color]

    <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

    Comment

    • Dennis M. Marks

      #3
      Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

      In article <fb9654f9.04060 20104.463607bd@ posting.google. com>, Fredrik
      Celin <fredrik.celin@ bilia.se> wrote:
      [color=blue]
      > 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[/color]
      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

      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! =-----

      Comment

      • Mabden

        #4
        Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

        "Fredrik Celin" <fredrik.celin@ bilia.se> wrote in message
        news:fb9654f9.0 406020104.46360 7bd@posting.goo gle.com...[color=blue]
        > 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?[/color]

        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


        Comment

        • Grant Wagner

          #5
          Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

          Mabden wrote:
          [color=blue]
          > "Fredrik Celin" <fredrik.celin@ bilia.se> wrote in message
          > news:fb9654f9.0 406020104.46360 7bd@posting.goo gle.com...[color=green]
          > > 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?[/color]
          >
          > 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[/color]

          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 <gwagner@agrico reunited.com>

          * Client-side Javascript and Netscape 4 DOM Reference available at:
          *


          * Internet Explorer DOM Reference available at:
          *
          Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


          * 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


          Comment

          • Lee

            #6
            Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

            Fredrik Celin said:[color=blue]
            >
            >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?[/color]

            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.

            Comment

            • Mabden

              #7
              Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

              "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
              news:40C08EE8.7 8C9F4AA@agricor eunited.com...[color=blue]
              > Mabden wrote:
              >[color=green]
              > > "Fredrik Celin" <fredrik.celin@ bilia.se> wrote in message
              > > news:fb9654f9.0 406020104.46360 7bd@posting.goo gle.com...[color=darkred]
              > > > 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?[/color]
              > >
              > > Computers don't compute, sorry for any confusion. They can store a[/color][/color]
              number in[color=blue][color=green]
              > > memory, or retrieve a number from memory. They can also add one to a[/color][/color]
              number,[color=blue][color=green]
              > > or subtract one from a number. They can also add a number to another[/color][/color]
              number.[color=blue][color=green]
              > > That's about it.
              > >
              > > --
              > > Mabden[/color]
              >
              > That's not really an explanation for what he's experiencing. For example[/color]
              take:[color=blue]
              > 0.05+0.01
              >
              > The above operation probably involved storing number(s) in memory,[/color]
              retrieving[color=blue]
              > number(s) from memory and adding a number to another number. All of these[/color]
              things[color=blue]
              > are things you say a computer can do, yet the result is not equal to 0.06.[/color]
              In[color=blue]
              > fact, the outcome of the operation 0.05+0.01 has nothing to do with a[/color]
              computers[color=blue]
              > ability to "compute", it has to do with a computers ability to represent[/color]
              decimal[color=blue]
              > values in binary.[/color]

              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


              Comment

              • Dr John Stockton

                #8
                Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

                JRS: In article <40C08EE8.78C9F 4AA@agricoreuni ted.com>, seen in
                news:comp.lang. javascript, Grant Wagner <gwagner@agrico reunited.com>
                posted at Fri, 4 Jun 2004 14:57:56 :[color=blue]
                >
                >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.[/color]

                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.

                Comment

                • Grant Wagner

                  #9
                  Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

                  Dr John Stockton wrote:
                  [color=blue]
                  > JRS: In article <40C08EE8.78C9F 4AA@agricoreuni ted.com>, seen in
                  > news:comp.lang. javascript, Grant Wagner <gwagner@agrico reunited.com>
                  > posted at Fri, 4 Jun 2004 14:57:56 :[color=green]
                  > >
                  > >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.[/color]
                  >
                  > Not a good example, since in javascript 1/3 + 2/3 gives exactly 1, and
                  > numbers are NOT stored as decimals.[/color]

                  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.
                  [color=blue]
                  > The OP should have read the newsgroup FAQ.[/color]

                  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 <gwagner@agrico reunited.com>

                  * Client-side Javascript and Netscape 4 DOM Reference available at:
                  *


                  * Internet Explorer DOM Reference available at:
                  *
                  Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

                  * 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


                  Comment

                  • Grant Wagner

                    #10
                    Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

                    Mabden wrote:
                    [color=blue]
                    > "Grant Wagner" <gwagner@agrico reunited.com> wrote in message
                    > news:40C08EE8.7 8C9F4AA@agricor eunited.com...[color=green]
                    > > Mabden wrote:
                    > >[color=darkred]
                    > > > "Fredrik Celin" <fredrik.celin@ bilia.se> wrote in message
                    > > > news:fb9654f9.0 406020104.46360 7bd@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[/color][/color]
                    > number in[color=green][color=darkred]
                    > > > memory, or retrieve a number from memory. They can also add one to a[/color][/color]
                    > number,[color=green][color=darkred]
                    > > > or subtract one from a number. They can also add a number to another[/color][/color]
                    > number.[color=green][color=darkred]
                    > > > That's about it.
                    > > >
                    > > > --
                    > > > Mabden[/color]
                    > >
                    > > That's not really an explanation for what he's experiencing. For example[/color]
                    > take:[color=green]
                    > > 0.05+0.01
                    > >
                    > > The above operation probably involved storing number(s) in memory,[/color]
                    > retrieving[color=green]
                    > > number(s) from memory and adding a number to another number. All of these[/color]
                    > things[color=green]
                    > > are things you say a computer can do, yet the result is not equal to 0.06.[/color]
                    > In[color=green]
                    > > fact, the outcome of the operation 0.05+0.01 has nothing to do with a[/color]
                    > computers[color=green]
                    > > ability to "compute", it has to do with a computers ability to represent[/color]
                    > decimal[color=green]
                    > > values in binary.[/color]
                    >
                    > 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[/color]

                    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 <gwagner@agrico reunited.com>

                    * Client-side Javascript and Netscape 4 DOM Reference available at:
                    *


                    * Internet Explorer DOM Reference available at:
                    *
                    Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


                    * 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


                    Comment

                    • Lasse Reichstein Nielsen

                      #11
                      Re: Why does 1.4 - 0.5 result in 0.8999999999999 999?

                      Grant Wagner <gwagner@agrico reunited.com> writes:
                      [color=blue]
                      > 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.[/color]

                      Ok, I didn't say anything the first time, but now I can't keep myself
                      from nitpicking any more :)

                      0.333... + 0.666... = 0.999... = 1

                      That is, if the "..." means an infinite repeating sequence of decimals.
                      [color=blue]
                      > 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[/color]

                      (decimal *representation *)
                      [color=blue]
                      > of 1 divided by 3 can only be an approximation and does not exactly
                      > represent the value of the ratio (and never can).[/color]

                      Yep.
                      [color=blue]
                      > 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.[/color]

                      Yes. The problem is not the the base of the representation, but the
                      finiteness of it. While 0.3 has a finite representation in base 10, it
                      doesn't in base 2. Just as 1/3 doesn't have a finite representation in
                      base 10, but does in base 3 ("0.1").
                      [color=blue]
                      > 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).[/color]

                      Are you sure you mean *imaginary* numbers (because you sure can make
                      decimal/binary representations of some of those, usually as pairs of
                      numbers).

                      We can represent all integers precisely and finitely in a positional
                      number system with any integer base larger than 1 (although there is
                      no upper bound on the sizes of the representations , each one of them
                      is finite).

                      We can also represent all rational numbers precisely and finitely. The
                      representation can be a pair of integers. However, we cannot represent
                      all rational numbers finitly in a single positional number system, no
                      matter what base (>=2).

                      We cannot represent all real numbers finitly with a finite alphabet,
                      no matter how we allow definitions to be written (including arbitrary
                      prose prose with a one-billion letter alphabet).

                      Whoa... better get back on track.
                      [color=blue]
                      > 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.[/color]

                      Thanks for answering that. I'm not sure I could ... coherently. :)

                      /L
                      --
                      Lasse Reichstein Nielsen - lrn@hotpop.com
                      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                      'Faith without judgement merely degrades the spirit divine.'

                      Comment

                      Working...