very strange operation result

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

    #1

    very strange operation result

    You can try this in javascript:

    A) alert( 1.9 * 50 ); = 95
    B) alert( 2.9 * 50 ); = 145
    C) alert( 3.9 * 50 ); = 195
    D) alert( 4.9 * 50 ); = 245.00000000000 003
    E) alert( 5.9 * 50 ); = 295

    Why D) = 245.00000000000 003 ?

    Thanks,
    Maori.


  • Henry

    #2
    Re: very strange operation result

    On Feb 8, 4:20 pm, maori wrote:
    You can try this in javascript:
    >
    A) alert( 1.9 * 50 );    =   95
    B) alert( 2.9 * 50 );    =   145
    C) alert( 3.9 * 50 );    =   195
    D) alert( 4.9 * 50 );    =   245.00000000000 003
    E) alert( 5.9 * 50 );    =   295
    >
    Why D)  = 245.00000000000 003 ?
    Javascript's only numeric data type is an IEEE double precision
    floating point number. That type of number can represent many numbers
    precisely, and others it can only approximate. You have encountered
    one of the numbers that can only be approximated.

    This should not be an unexpected feature as it is common in systems
    for representing numbers. For example, a decimal representation of 1
    divided by 3 is unlikely to be precise (because it would be infinitely
    long), while the same number can be precisely represented in base 3 as
    0.1.

    Comment

    • maori

      #3
      Re: very strange operation result

      And, how can I deal with this in javascript, please?


      "Henry" <rcornford@rain drop.co.ukescri bió en el mensaje
      news:b94412b3-85ca-49d6-b3fb-86387322d6b4@d4 g2000prg.google groups.com...
      On Feb 8, 4:20 pm, maori wrote:
      You can try this in javascript:
      >
      A) alert( 1.9 * 50 ); = 95
      B) alert( 2.9 * 50 ); = 145
      C) alert( 3.9 * 50 ); = 195
      D) alert( 4.9 * 50 ); = 245.00000000000 003
      E) alert( 5.9 * 50 ); = 295
      >
      Why D) = 245.00000000000 003 ?
      Javascript's only numeric data type is an IEEE double precision
      floating point number. That type of number can represent many numbers
      precisely, and others it can only approximate. You have encountered
      one of the numbers that can only be approximated.

      This should not be an unexpected feature as it is common in systems
      for representing numbers. For example, a decimal representation of 1
      divided by 3 is unlikely to be precise (because it would be infinitely
      long), while the same number can be precisely represented in base 3 as
      0.1.


      Comment

      • Evertjan.

        #4
        Re: very strange operation result

        timothytoe wrote on 08 feb 2008 in comp.lang.javas cript:
        What you found did not surprise me. I've seen that kind of things for
        decades. The only system I can remember that didn't have that problem
        was the Atari 8-bit, which turned on the binary coded decimal bit in
        the 6502 for Atari BASIC.
        >

        Central Data Basic for the Signetics/Philips 2650 microprosessor,
        rumoured to have been written by a youth called William Gates,
        usded BCD as it's standard Math engine.

        And there were more:

        <http://en.wikipedia.or g/wiki/Binary-coded_decimal>

        It is stil used in financial programming, in the code most radio clocks in
        Europe receive from the Frankfurt long wave transmitter, and many other
        applocations.

        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        • Doug Miller

          #5
          Re: very strange operation result

          In article <Xns9A3EE689DEB 6Aeejj99@194.10 9.133.242>, "Evertjan." <exjxw.hannivoo rt@interxnl.net wrote:
          >timothytoe wrote on 08 feb 2008 in comp.lang.javas cript:
          >
          >What you found did not surprise me. I've seen that kind of things for
          >decades. The only system I can remember that didn't have that problem
          >was the Atari 8-bit, which turned on the binary coded decimal bit in
          >the 6502 for Atari BASIC.
          Pfui. IBM mainframes -- System/360 and its descendants (S/370, 309x, 40xx,
          etc.) -- have had true decimal arithmetic at the hardware level for more than
          forty years.

          --
          Regards,
          Doug Miller (alphageek at milmac dot com)

          It's time to throw all their damned tea in the harbor again.

          Comment

          • timothytoe

            #6
            Re: very strange operation result

            On Feb 8, 4:24 pm, spamb...@milmac .com (Doug Miller) wrote:
            In article <Xns9A3EE689DEB 6Aeej...@194.10 9.133.242>, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
            >
            timothytoe wrote on 08 feb 2008 in comp.lang.javas cript:
            >
            What you found did not surprise me. I've seen that kind of things for
            decades. The only system I can remember that didn't have that problem
            was the Atari 8-bit, which turned on the binary coded decimal bit in
            the 6502 for Atari BASIC.
            >
            Pfui. IBM mainframes -- System/360 and its descendants (S/370, 309x, 40xx,
            etc.) -- have had true decimal arithmetic at the hardware level for more than
            forty years.
            >
            --
            Regards,
            Doug Miller (alphageek at milmac dot com)
            >
            It's time to throw all their damned tea in the harbor again.
            I didn't say the Atari was the only one. I said it was the only one I
            remembered.

            Comment

            • Dr J R Stockton

              #7
              Re: very strange operation result

              In comp.lang.javas cript message <7cede433-bdb0-4fae-b55d-da5d23d3e971@p6
              9g2000hsa.googl egroups.com>, Sat, 9 Feb 2008 11:50:46, VK
              <schools_ring@y ahoo.composted:
              >though I would argue with some rounding
              >ideas in FAQ4_6
              The code there is correct but outdated. For more recent code, go via
              the first link.

              --
              (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6.
              Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
              I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
              free, DOS/Win/UNIX, <URL:http://www.idiotsdelig ht.net/minitrue/unsupported.

              Comment

              • Randy Webb

                #8
                Re: very strange operation result

                mk834tt@yahoo.c om said the following on 2/11/2008 6:16 PM:
                Randy seems to have a low threshold... or he's bored.
                Neither.
                Is there anyone out there that has escaped him?
                Yes. The many that use a little intelligence to solve a problem rather
                than wanting someone to spoon feed them.
                Said on top for Randy. :-)
                That's nice[1].

                --
                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/

                Comment

                • mk834tt@yahoo.com

                  #9
                  Re: very strange operation result


                  Thanks Randy!

                  On Feb 11, 6:51 pm, Randy Webb <HikksNotAtH... @aol.comwrote:
                  mk83...@yahoo.c om said the following on 2/11/2008 6:16 PM:
                  >
                  Randy seems to have a low threshold... or he's bored.
                  >
                  Neither.
                  >
                  Is there anyone out there that has escaped him?
                  >
                  Yes. The many that use a little intelligence to solve a problem rather
                  than wanting someone to spoon feed them.
                  >
                  Said on top for Randy. :-)
                  >
                  That's nice[1].
                  >
                  --
                  Randy
                  Chance Favors The Prepared Mind
                  comp.lang.javas cript FAQ -http://jibbering.com/faq/index.html
                  Javascript Best Practices -http://www.JavascriptT oolbox.com/bestpractic

                  Comment

                  • timothytoe

                    #10
                    Re: very strange operation result

                    What is it about comp.lang.javas cript that makes people behave as if
                    they were in 2nd grade? We should add a "Why do people act childishly
                    in clj?" to the FAQ.

                    How old are you people?

                    Comment

                    • Evertjan.

                      #11
                      Re: very strange operation result

                      timothytoe wrote on 13 feb 2008 in comp.lang.javas cript:
                      What is it about comp.lang.javas cript that makes people behave as if
                      they were in 2nd grade?
                      You must be first grade, we are only second grade.

                      Or are you referring to some local school system, unknown to many of "us"?
                      We
                      Are you plural, or childish?
                      should add a "Why do people act childishly in clj?" to the FAQ.
                      Perhaps you can give us some first grade advice about Javascript?

                      --
                      Evertjan.
                      The Netherlands.
                      (Please change the x'es to dots in my emailaddress)

                      Comment

                      Working...