ROUND UP A NUMBER.

Discussion in 'AutoCAD' started by Casey Roberts, Oct 20, 2004.

  1. Using lisp, how would I go about rounding up a number (real) to the nearest
    whole number (integer)

    Thanks

    Casey
     
    Casey Roberts, Oct 20, 2004
    #1
  2. Actually, upon further investigation, I want the result to be a whole
    number, but not an integer as there seems to be some issues with dividing by
    a real number.
     
    Casey Roberts, Oct 20, 2004
    #2
  3. Casey Roberts

    MP Guest

    so do you want 1.01 to be rounded up to 2.0 ???
    or is only 1.5 rounded up to 2.0???
    your original question would seem to imply the first case, but in the real
    world that would be unusual
    so it depends on *exactly* what you need to accomplish

    what about negative numbers?
    does round up mean become more negative????

    there are lots of unknowns based solely on your question as stated.
    Can you elaborate somewhat?

    The rounding operation would probably be quite easy to implement, but it
    depends on what you really need.

    Mark
     
    MP, Oct 20, 2004
    #3
  4. Casey Roberts

    Jim Claypool Guest

    (atoi (rtos (+ test 0.5) 2 0))
     
    Jim Claypool, Oct 20, 2004
    #4
  5. Casey Roberts

    Jim Claypool Guest

    (atof (rtos (+ number 0.5) 2 0))

     
    Jim Claypool, Oct 20, 2004
    #5
  6. Casey Roberts

    MP Guest

    For example,
    If I interpret your question literally, this would be what I'd try.
    (defun roundx(num)
    (* 1.0(1+ (Fix num)))
    )

    maybe what you want or may not be what you want
    Hth
    Mark
     
    MP, Oct 20, 2004
    #6
  7. Casey Roberts

    CAB2k Guest

    Call me simple, but I always did it this way:
    Code:
    (defun rndup (real)
    (float (fix (+ 0.99999999 real)))
    )
    
    (defun c:test ()
    (print (rndup 1.01))
    (print (rndup 1.05))
    (print (rndup 1.99999))
    (print (rndup 0.9999))
    (print (rndup -2.01))
    (princ)
    )
    Command: test

    2.0
    2.0
    2.0
    1.0
    -1.0
     
    CAB2k, Oct 21, 2004
    #7
  8. Casey Roberts

    CAB2k Guest

    You could handle negative numbers this way.
    Code:
    (defun rndup (num)
    (if (>= num 0)
    (float (fix (+ 0.99999999 num)))
    (float (fix num))
    )
    )
    
    (defun c:test ()
    (print (rndup 1.01))
    (print (rndup 1.05))
    (print (rndup 1.99999))
    (print (rndup 0.9999))
    (print (rndup -2.01))
    (print (rndup -0.01))
    (princ)
    )
    Command: test

    2.0
    2.0
    2.0
    1.0
    -2.0
    0.0
     
    CAB2k, Oct 21, 2004
    #8
  9. Casey Roberts

    CAB2k Guest

    This will round negative numbers up if the are >=.5
    Code:
    (defun rndup (num)
    (if (>= num 0)
    (float (fix (+ 0.5 num)))
    (float (fix (- num 0.5)))
    )
    )
    
    (defun c:test ()
    (print (rndup 1.01))
    (print (rndup 1.05))
    (print (rndup 1.99999))
    (print (rndup 0.9999))
    (print (rndup 0.49))
    (print (rndup -2.01))
    (print (rndup -0.01))
    (print (rndup -0.6))
    (princ)
    )
    Command: test

    1.0
    1.0
    2.0
    1.0
    0.0
    -2.0
    0.0
    -1.0
     
    CAB2k, Oct 21, 2004
    #9
  10. Yanked this one out of the toolbox.

    ; Doug Broad
    ; additional credits Joe Burke, Peter Toby
    (defun round (value to)
    (setq to (abs to))
    (* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))
    )
     
    Jason Piercey, Oct 21, 2004
    #10
  11. Except for one problem. This rounds 1.0 up to 2, which is not correct.

    To clarify, from my first posts, I don't have to deal with negative numbers
    and yes, 1.01 should round up to 2, but 1.0 should remain 1.0.

    What I came up with is:
    (if (/= num (fix num))(setq num (+ 1.0 (fix num))))
     
    Casey Roberts, Oct 21, 2004
    #11
  12. Casey Roberts

    MP Guest

    cool, glad you got a solution
    :)
    Mark

     
    MP, Oct 21, 2004
    #12
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.