Math woes

Discussion in 'AutoCAD' started by dblaha, Jul 28, 2004.

  1. dblaha

    dblaha Guest

    Can anyone else duplicate these results at the command line?:

    Command: (setq a 2.015)
    2.015

    Command: (setq b (- a 2))
    0.015

    Command: (= 0.015 b)
    nil

    What am I missing here?
     
    dblaha, Jul 28, 2004
    #1
  2. dblaha

    T.Willey Guest

    I got the same result, so I tried something else:

    Command: (= (rtos 0.015) (rtos b))
    T

    and it worked. Not sure why they didn't equal before.

    Tim
     
    T.Willey, Jul 28, 2004
    #2
  3. Try using (equal) and apply a fuzz factor.
     
    Jason Piercey, Jul 28, 2004
    #3
  4. dblaha

    dblaha Guest

    OK:

    Command: (setq a 2.015)
    2.015

    Command: (setq b (- a 2))
    0.015

    Command: (= 0.015 b)
    nil

    Command: (equal 0.015 b 0.0001)
    T

    Thanks! The fuzz works. But why is this necessary in this situation?
     
    dblaha, Jul 28, 2004
    #4
  5. Command: (setq a 2.015)
    2.015

    Command: (setq b (- a 2))
    0.015

    Command: (rtos b 2 16)
    "0.0150000000000001"
     
    Jason Piercey, Jul 28, 2004
    #5
  6. dblaha

    dblaha Guest

    OK, by converting both sides of the equation to a string and back(and thus controlling the decimal accuracy of all values), I can get the equation to work every time. (BTW, the purpose of the equation is simply to strip away the integer from variable A and compare the remaining value to variable B):

    Command: (setq A 2.018)
    2.018

    Command: (setq B 0.018)
    0.018

    Command: (= (atof (rtos (- A (atof (rtos A 2 0))) 2 3))(atof (rtos B 2 3)))
    T

    That still seems like an awful lot of trouble, but at least it works. Thanks to Jason, Bill and Tim for the assistance in sorting this out.
     
    dblaha, Jul 28, 2004
    #6
  7. dblaha

    David Bethel Guest

    How about;

    (equal (rem a 1) b 1e-14)

    -David
     
    David Bethel, Jul 28, 2004
    #7
  8. dblaha

    randy benson Guest

    You're not seeing the big picture Jason is showing you; use "equal" and a fuzz
    factor instead of "=", and you won't have to do the string conversions.
     
    randy benson, Jul 28, 2004
    #8
  9. This is because of the fundamental incapacity of a computer using base-2
    arithmetic to represent exactly every base-10 floating point number.
    This is in no way AutoCad -specific, the same problem occurs in just
    about every programming language.

    For further reading, see any introductory numerical analysis or
    computing science text. On a little Googling I found for example
    http://www.cs.princeton.edu/introcs/91float/
    which has more about this than I ever bothered to learn.

    --
     
    Martti Halminen, Jul 29, 2004
    #9
  10. dblaha

    dblaha Guest

    Thanks for the explanation.
     
    dblaha, Jul 29, 2004
    #10
  11. dblaha

    dblaha Guest

    The equation I posted was just a test. The equation I'm actually using in my routine involves a "<" comparison for which the equal function doesn't seem to fit in very well.

    (< (atof (rtos (- A (atof (rtos A 2 0))) 2 3))(atof (rtos B 2 3)))
     
    dblaha, Jul 29, 2004
    #11
  12. dblaha

    dblaha Guest

    The equation I posted was a just test of my somewhat convoluted solution. The equation I'm actually using in my routine requires a "<" comparison for which the equal function doesn't seem to fit in very well.

    (< (atof (rtos (- A (atof (rtos A 2 0))) 2 3))(atof (rtos B 2 3)))
     
    dblaha, Jul 29, 2004
    #12
  13. dblaha

    dblaha Guest

    That should have read "just a test". I'd like to think it was "a just test" but I don't feel qualified to judge such matters...
     
    dblaha, Jul 29, 2004
    #13
  14. dblaha

    BillZ Guest

    = works good on integers.

    (or (equal item1 item2 1e-6)
    (< item1 item2)
    )

    Would be what I use if item1 could not be larger than item2.

    Bill
     
    BillZ, Jul 29, 2004
    #14
  15. dblaha

    dblaha Guest

    In my code item1 cannot be less than item2, so with a minor change...

    (or (equal item1 item2 1e-6)
    (> item1 item2)
    )

    ...this will work. Thank you!
     
    dblaha, Jul 29, 2004
    #15
  16. dblaha

    BillZ Guest

    You're welcome.





    Bill
     
    BillZ, Jul 29, 2004
    #16
  17. dblaha

    BTO Guest

    This is because of the fundamental incapacity of a computer using base-2
    Hello,

    but "=" should compare number with a floating point precision of 10-14
    because AutoCAD is sold as this.
    It's a very strange to compare number over 14 significant digits of accuracy
    while "real numbers are stored in double-precision floating-point format,
    providing at least 14 significant digits of precision" as help file says

    Effectively, it's not specifique to AutoCAD.

    Bruno Toniutti.
    (sorry for my english level)
     
    BTO, Aug 10, 2004
    #17
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.