sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    ⚠️ Update | Sketchucation Tools 5.0.8 released with licensing improvements and bugfixes Download

    Strange result of a substraction

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 5 Posters 837 Views 5 Watching
    Loading More Posts
    • 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.
    • J
      Jim
      last edited by

      Much has been written about floating point errors.

      Basically, floating point numbers can not be accurately represented in binary.

      Hi

      1 Reply Last reply
      Reply Quote 0
      • Dan RathbunD
        Dan Rathbun
        last edited by

        @macgile said:

        i have tested with a = a + b the result is same

        That is because the Ruby interpreter converts
        a += b
        to
        a = a + b
        before the expression is evaluated.

        I'm not here much anymore.

        1 Reply Last reply
        Reply Quote 0
        • voljankoV
          voljanko
          last edited by

          @unknownuser said:

          i dont compare anything !!!
          i have tested with a = a + b the result is same

          I just meant that is always good to avoid considering floats as exact numbers.

          SuSolid.com - solid check - solid repair- solid intersection check - weight plugin

          1 Reply Last reply
          Reply Quote 0
          • M
            macgile
            last edited by

            @dan rathbun said:

            @macgile said:

            i have tested with a = a + b the result is same

            That is because the Ruby interpreter converts
            a += b
            to
            a = a + b
            before the expression is evaluated.

            i dont understand your explanation Dan !!!!

            1 Reply Last reply
            Reply Quote 0
            • M
              macgile
              last edited by

              @voljanko said:

              @unknownuser said:

              i dont compare anything !!!
              i have tested with a = a + b the result is same

              I just meant that is always good to avoid considering floats as exact numbers.

              oh yes 😄

              I thought the result would be near to 0 and not of 4.

              1 Reply Last reply
              Reply Quote 0
              • voljankoV
                voljanko
                last edited by

                Try to add some number,you will see that is zero and not 4.

                SuSolid.com - solid check - solid repair- solid intersection check - weight plugin

                1 Reply Last reply
                Reply Quote 0
                • Dan RathbunD
                  Dan Rathbun
                  last edited by

                  A singleton method to increment a float a, by an argument arg, to dec number of decimal places. (It defaults to 1 decimal place.)

                  a = 9.0
                  def a.incr( arg = 1.0, dec = 1 )
                    dec = dec.to_i
                    arg = round( arg.to_f * 10**dec  )
                    # self is object a
                    temp =( round( self * 10**dec ) + arg ).to_f
                    self =( temp / 10**dec )
                  end
                  

                  use it like:
                  a = 9.0 b = -1.8 a.incr(b)

                  I'm not here much anymore.

                  1 Reply Last reply
                  Reply Quote 0
                  • J
                    Jim
                    last edited by

                    @macgile said:

                    I thought the result would be near to 0 and not of 4.

                    macguile,

                    4.44089209850063e-016

                    The e-016 at the end of the number means to move the decimal place 16 places to the left - making the actual number:

                    0.0000000000000000444089209850063

                    or very nearly zero. The reason the result is not exactly zero is due to floating point errors as linked above.

                    Hi

                    1 Reply Last reply
                    Reply Quote 0
                    • M
                      macgile
                      last edited by

                      @dan rathbun said:

                      A singleton method to increment a float a, by an argument arg, to dec number of decimal places. (It defaults to 1 decimal place.)

                      a = 9.0
                      > def a.incr( arg = 1.0, dec = 1 )
                      >   dec = dec.to_i
                      >   arg = round( arg.to_f * 10**dec  )
                      >   # self is object a
                      >   temp =( round( self * 10**dec ) + arg ).to_f
                      >   self =( temp / 10**dec )
                      > end
                      

                      use it like:
                      a = 9.0 b = -1.8 a.incr(b)

                      THANK for this solution Dan

                      Best Regard 😄

                      1 Reply Last reply
                      Reply Quote 0
                      • M
                        macgile
                        last edited by

                        @jim said:

                        @macgile said:

                        I thought the result would be near to 0 and not of 4.

                        macguile,

                        4.44089209850063e-016

                        The e-016 at the end of the number means to move the decimal place 16 places to the left - making the actual number:

                        0.0000000000000000444089209850063

                        or very nearly zero. The reason the result is not exactly zero is due to floating point errors as linked above.

                        thank i nderstand now 😄

                        regard

                        1 Reply Last reply
                        Reply Quote 0
                        • thomthomT
                          thomthom
                          last edited by

                          http://floating-point-gui.de/

                          Thomas Thomassen — SketchUp Monkey & Coding addict
                          List of my plugins and link to the CookieWare fund

                          1 Reply Last reply
                          Reply Quote 0

                          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                          With your input, this post could be even better 💗

                          Register Login
                          • 1 / 1
                          • First post
                            Last post
                          Buy SketchPlus
                          Buy SUbD
                          Buy WrapR
                          Buy eBook
                          Buy Modelur
                          Buy Vertex Tools
                          Buy SketchCuisine
                          Buy FormFonts

                          Advertisement