Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. An annoying JavaScript quirk

An annoying JavaScript quirk

Scheduled Pinned Locked Moved The Lounge
javascript
35 Posts 20 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Brisingr Aerowing

    0.30000000000000000004 :doh: (Chrome & Firefox)

    Gryphons Are Awesome! ‮Gryphons Are Awesome!‬

    R Offline
    R Offline
    Reelix
    wrote on last edited by
    #8

    Yup :p

    -= Reelix =-

    1 Reply Last reply
    0
    • R Reelix

      var j = 0; j += 0.1; j += 0.1; alert(j); // Alerts 0.2 j += 0.1; alert(j); // Take a guess

      -= Reelix =-

      L Offline
      L Offline
      lewax00
      wrote on last edited by
      #9

      That has nothing to do with JavaScript specifically, it's how floating point numbers are stored. If you want a long explanation, try the Wikipedia article.[^] If you want a short answer, there is no way to represent 0.3 exactly as a floating point (which JS uses for all numbers).

      S 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        0.30000000004 (give or take a few "0"s) It's something to do with the binary representation of the floating-point number, and it catches a lot of people out the first time they run into it.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        R Offline
        R Offline
        Reelix
        wrote on last edited by
        #10

        Correct.

        -= Reelix =-

        1 Reply Last reply
        0
        • R Reelix

          var j = 0; j += 0.1; j += 0.1; alert(j); // Alerts 0.2 j += 0.1; alert(j); // Take a guess

          -= Reelix =-

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #11

          So what exactly is the precision-related quirk? I also notice that 0.8 and 0.9 exhibit the same behavior. Can you get around it by using toFixed()?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

          M 1 Reply Last reply
          0
          • R Reelix

            var j = 0; j += 0.1; j += 0.1; alert(j); // Alerts 0.2 j += 0.1; alert(j); // Take a guess

            -= Reelix =-

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #12

            Ahhh, floating point issues. Doncha just love them. I'd imagine it was something slightly outside of 0.30000.

            I was brought up to respect my elders. I don't respect many people nowadays.
            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            1 Reply Last reply
            0
            • R Reelix

              var j = 0; j += 0.1; j += 0.1; alert(j); // Alerts 0.2 j += 0.1; alert(j); // Take a guess

              -= Reelix =-

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #13

              That is why 99% of all non-trivial code involving floating point numbers is wrong. Ok no, but close enough: the actual reason is that people don't expect it to work like this. They expect "math the way they learned it in school". An other problem is that usually the result is close enough that it's not obviously wrong (debuggers play a part in this too, by printing floats "nicely" rather than printing their actual value), so no one notices until suddenly everything breaks down.

              1 Reply Last reply
              0
              • R Reelix

                var j = 0; j += 0.1; j += 0.1; alert(j); // Alerts 0.2 j += 0.1; alert(j); // Take a guess

                -= Reelix =-

                Z Offline
                Z Offline
                ZurdoDev
                wrote on last edited by
                #14

                Annoying; however, nothing important or critical should be left to JavaScript. :)

                There are only 10 types of people in the world, those who understand binary and those who don't.

                R C S 3 Replies Last reply
                0
                • Z ZurdoDev

                  Annoying; however, nothing important or critical should be left to JavaScript. :)

                  There are only 10 types of people in the world, those who understand binary and those who don't.

                  R Offline
                  R Offline
                  Reelix
                  wrote on last edited by
                  #15

                  Tell that to the HTML5 spec :p JS is growing, and more and more stuff is being ported to JS due to WebGL :p

                  -= Reelix =-

                  F 1 Reply Last reply
                  0
                  • R Reelix

                    var j = 0; j += 0.1; j += 0.1; alert(j); // Alerts 0.2 j += 0.1; alert(j); // Take a guess

                    -= Reelix =-

                    F Offline
                    F Offline
                    Florian Rappl
                    wrote on last edited by
                    #16

                    I don't see why you think that this is a JavaScript quirk? JavaScript uses single precision floating point numbers, so you should be aware of rounding errors as usual.

                    1 Reply Last reply
                    0
                    • R Reelix

                      Tell that to the HTML5 spec :p JS is growing, and more and more stuff is being ported to JS due to WebGL :p

                      -= Reelix =-

                      F Offline
                      F Offline
                      Florian Rappl
                      wrote on last edited by
                      #17

                      I don't see what the HTML5 spec has to do with that. JS is not part of the HTML5 spec - it just defines a common interface (called the DOM) which could be accessed by programming languages. In fact the most common language to access the DOM is JavaScript - because most browsers implemented this (and sometimes only this) language.

                      1 Reply Last reply
                      0
                      • Z ZurdoDev

                        Annoying; however, nothing important or critical should be left to JavaScript. :)

                        There are only 10 types of people in the world, those who understand binary and those who don't.

                        C Offline
                        C Offline
                        Chris Maunder
                        wrote on last edited by
                        #18

                        ryanb31 wrote:

                        nothing important or critical should be left to JavaScript

                        Err...who wants to break the news to Ryan? :~

                        cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                        Z 2 Replies Last reply
                        0
                        • C Chris Maunder

                          ryanb31 wrote:

                          nothing important or critical should be left to JavaScript

                          Err...who wants to break the news to Ryan? :~

                          cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                          Z Offline
                          Z Offline
                          ZurdoDev
                          wrote on last edited by
                          #19

                          Hit me!

                          There are only 10 types of people in the world, those who understand binary and those who don't.

                          1 Reply Last reply
                          0
                          • R Reelix

                            So you'd think - But unfortunately not :/

                            -= Reelix =-

                            J Offline
                            J Offline
                            Jon Woo
                            wrote on last edited by
                            #20

                            .30000000000000004 ? why does that happen?

                            B 1 Reply Last reply
                            0
                            • R Reelix

                              So you'd think - But unfortunately not :/

                              -= Reelix =-

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #21

                              No, you wouldn't as the floating point representation of 0.3 is not possible in a digital computer; it will always be an approximation (and has nothing to do with Javascript). This is a well known issue.

                              Use the best guess

                              1 Reply Last reply
                              0
                              • R Reelix

                                var j = 0; j += 0.1; j += 0.1; alert(j); // Alerts 0.2 j += 0.1; alert(j); // Take a guess

                                -= Reelix =-

                                J Offline
                                J Offline
                                Joan M
                                wrote on last edited by
                                #22

                                That's a common problem with programming (Java and others)... then you end up comparing values by checking that its difference is smaller than the approximation you end up having there... X|

                                [www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.

                                1 Reply Last reply
                                0
                                • L lewax00

                                  That has nothing to do with JavaScript specifically, it's how floating point numbers are stored. If you want a long explanation, try the Wikipedia article.[^] If you want a short answer, there is no way to represent 0.3 exactly as a floating point (which JS uses for all numbers).

                                  S Offline
                                  S Offline
                                  Stefan_Lang
                                  wrote on last edited by
                                  #23

                                  Actually there is a rather short explanation by analogy: 0.3 cannot be represented exactly in binary for the same reasons that 1/3 cannot be represented in decimal - you'd need an endless number of digits.

                                  L 1 Reply Last reply
                                  0
                                  • Z ZurdoDev

                                    Annoying; however, nothing important or critical should be left to JavaScript. :)

                                    There are only 10 types of people in the world, those who understand binary and those who don't.

                                    S Offline
                                    S Offline
                                    Stefan_Lang
                                    wrote on last edited by
                                    #24

                                    Unless you suggest symbolic programming (e. g. MatLab, Mathematica), there is no way to avoid that issue in any language. Which of the 10 types of people do you belong to? ;)

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      ryanb31 wrote:

                                      nothing important or critical should be left to JavaScript

                                      Err...who wants to break the news to Ryan? :~

                                      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                      Z Offline
                                      Z Offline
                                      ZurdoDev
                                      wrote on last edited by
                                      #25

                                      So, curiosity got the better of me. Pleas explain. Javascript is uncompiled source code on the client. That means it can all be hacked, and somewhat easily too. Nothing critical of nature should be left to JS alone. Always validate server side. What part do you disagree with?

                                      There are only 10 types of people in the world, those who understand binary and those who don't.

                                      T C 2 Replies Last reply
                                      0
                                      • S Stefan_Lang

                                        Actually there is a rather short explanation by analogy: 0.3 cannot be represented exactly in binary for the same reasons that 1/3 cannot be represented in decimal - you'd need an endless number of digits.

                                        L Offline
                                        L Offline
                                        lewax00
                                        wrote on last edited by
                                        #26

                                        Except you did just represent it exactly in decimal: 1/3. There's also the 0.3 with a bar over the three. IEEE floating point does not support such notations though.

                                        T 1 Reply Last reply
                                        0
                                        • L lewax00

                                          Except you did just represent it exactly in decimal: 1/3. There's also the 0.3 with a bar over the three. IEEE floating point does not support such notations though.

                                          T Offline
                                          T Offline
                                          thomas michaud
                                          wrote on last edited by
                                          #27

                                          You are confusing 1/3 with 3/10th. The two are not the same. Actually the problem is 1/10th - which can't be accurately represented in binary floating point. (It can be represented in base_10 floating point - but base_10 floating point isn't perfect either. It can't represent 1/3.) The result is a rounding error on addition (1/10 + 1/10 + 1/10). Be very wary of doing financial calculations in base_2 floating point. It ALWAYS bites you eventually.

                                          L 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups