• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Strange result of a substraction

Scheduled Pinned Locked Moved Developers' Forum
14 Posts 5 Posters 220 Views
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.
  • V Offline
    voljanko
    last edited by 13 Jul 2011, 20:40

    This is almost zero πŸ˜„
    It is always a risk when you use floats, in all languages.You should never compare float == float if you don't want problems.

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

    1 Reply Last reply Reply Quote 0
    • M Offline
      macgile
      last edited by 13 Jul 2011, 20:48

      @voljanko said:

      This is almost zero πŸ˜„
      It is always a risk when you use floats, in all languages.You should never compare float == float if you don't want problems.

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

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by 13 Jul 2011, 20:51

        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
        • D Offline
          Dan Rathbun
          last edited by 13 Jul 2011, 20:55

          @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
          • V Offline
            voljanko
            last edited by 13 Jul 2011, 21:02

            @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 Offline
              macgile
              last edited by 13 Jul 2011, 21:03

              @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 Offline
                macgile
                last edited by 13 Jul 2011, 21:07

                @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
                • V Offline
                  voljanko
                  last edited by 13 Jul 2011, 21:11

                  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
                  • D Offline
                    Dan Rathbun
                    last edited by 13 Jul 2011, 21:25

                    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 Offline
                      Jim
                      last edited by 13 Jul 2011, 21:32

                      @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 Offline
                        macgile
                        last edited by 13 Jul 2011, 21:36

                        @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 Offline
                          macgile
                          last edited by 13 Jul 2011, 21:38

                          @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
                          • T Offline
                            thomthom
                            last edited by 14 Jul 2011, 10:10

                            Link Preview Image
                            The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic

                            Aims to provide both short and simple answers to the common recurring questions of novice programmers about floating-point numbers not 'adding up' correctly, and more in-depth information about how IEEE 754 floats work, when and how to use them correctly, and what to use instead when they are not appropriate.

                            favicon

                            (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
                            • 1 / 1
                            1 / 1
                            • First post
                              11/14
                              Last post
                            Buy SketchPlus
                            Buy SUbD
                            Buy WrapR
                            Buy eBook
                            Buy Modelur
                            Buy Vertex Tools
                            Buy SketchCuisine
                            Buy FormFonts

                            Advertisement