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.
  • V vonb

    0.3 ???

    The signature is in building process.. Please wait...

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

    So you'd think - But unfortunately not :/

    -= Reelix =-

    J L 2 Replies Last reply
    0
    • R Rutvik Dave

      0.20.1 :~

      Remind Me This - Manage, Collaborate and Execute your Project in the Cloud

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

      It remains numeric - But nice try :)

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

        B Offline
        B Offline
        Brisingr Aerowing
        wrote on last edited by
        #6

        0.30000000000000000004 :doh: (Chrome & Firefox)

        Gryphons Are Awesome! ‮Gryphons Are Awesome!‬

        R K 2 Replies 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 =-

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #7

          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

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

          R 1 Reply Last reply
          0
          • 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
                                          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