How does autocad/lisp round numbers?

Discussion in 'AutoCAD' started by ffejgreb, Jan 22, 2004.

  1. ffejgreb

    ffejgreb Guest

    I am wondering because I created a routine that calculates square
    footages/acreages and then creates a string of text in the editor. I was
    rounding the number down to 2 places after the decimal but I was getting
    some incorrect totals when I added a few together. Just for kicks I changed
    the rounding to 4 places and found the following:

    One area calc created an area of 9.1264 acres. When it was rounded to 2
    places this was represented as 9.12 acres. I think it should be 9.13. Does
    anyone know why this is? Is there a way to account for it?

    TIA,

    Jeff
     
    ffejgreb, Jan 22, 2004
    #1
  2. ffejgreb

    Paul Turvill Guest

    Apply the rounding *after* doing the addition.
    ___
     
    Paul Turvill, Jan 22, 2004
    #2
  3. ffejgreb

    Jeff Mishler Guest

    Hmmm, this little bit of code seems to show correct rounding:
    (setq count 0)
    (repeat 9
    (princ (strcat "\n" (rtos 9.1264573249 2 count)))
    (setq count (1+ count))
    )

    returns:
    9
    9.1
    9.13
    9.126
    9.1265
    9.12646
    9.126457
    9.1264573
    9.12645732
     
    Jeff Mishler, Jan 22, 2004
    #3
  4. ffejgreb

    ffejgreb Guest

    Here is the routine I spoke of:

    (defun c:act (/ b1 acre p1 h1)
    (setq b1 (entsel))
    (command ".area" "e" b1)
    (setq b1 (getvar "area"))
    (setq acre 43560)
    (setq b1 (/ b1 acre))
    (setq b1 (rtos b1 2 2))
    (setq b1 (strcat b1 " AC"))
    (setq p1 (getpoint "\nPick start point for CENTER Justified text: "))
    (setq h1 (getdist "\nText height: "))
    (command ".text" "j" "c" p1 h1 "0" b1)
    (princ)
    )

    I use it to select a polygon and then do the calcs. Could you give it a go
    and see if I'm doing something wrong?

    I appreciate any help you can provide.

    Jeff
     
    ffejgreb, Jan 22, 2004
    #4
  5. ffejgreb

    Tom Berger Guest

    First: LISP allows you to use other symbol names besides b1 too

    Second: what does (setq b1 (getvar "area")) return in your case? Use
    (rtos b1 2 20) to tell us the number in full accuracy.

    I did never find an error with AutoCAD's way to round up real numbers.
    Jeff already showed you that your initial statement is not true.

    Tom Berger
     
    Tom Berger, Jan 22, 2004
    #5
  6. ffejgreb

    Jeff Mishler Guest

    Jeff,
    I cannot duplicate your rounding error. Maybe if you use vlide and place a
    break point after the entsel, then use F8 to step thru the routine. Make
    sure to have the watch window open and observe the "LAST-VALUE" to see if
    you can tell where the error gets introduced.

    Jeff
     
    Jeff Mishler, Jan 22, 2004
    #6
  7. ffejgreb

    ffejgreb Guest

    I will give that a try. Thank you for the info.

    Jeff
     
    ffejgreb, Jan 22, 2004
    #7
  8. ffejgreb

    Joe Burke Guest

    Jeff,

    This probably doesn't apply to your question. Take care when dividing by integers.
    The divide function is a bit bizarre in that regard.
    Command: (/ 5 3)
    1
    Command: (/ 5 3.0)
    1.66667

    Given one or both numbers is a real, the divide function returns the result you'd
    expect. Two integers... avoid it.

    So I'd suggest (setq acre 43560.0). Or see help regarding the float function.

    Joe Burke
     
    Joe Burke, Jan 23, 2004
    #8
  9. ffejgreb

    ffejgreb Guest

    Thank you Joe. I will alter my routine and see what happens.
     
    ffejgreb, Jan 23, 2004
    #9
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.