Author Topic: Stringless Rounds?  (Read 8698 times)

0 Members and 1 Guest are viewing this topic.

David Bethel

  • Swamp Rat
  • Posts: 656
Stringless Rounds?
« Reply #15 on: November 10, 2004, 08:32:30 AM »
Taking Michael's approach and appling to my code, you get:

Code: [Select]
;;;ROUND BY
;;;ARG -> REAL to round, REAL to round by
;;;RET -> REAL
(defun rndby (r b / tmp)
  (setq tmp (rem r b))
  (cond ((>= tmp (* 0.5 b))  (+ r (- b tmp)))
        (T                   (- r tmp))))


A fairly clean piece of code.  -David
R12 Dos - A2K

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Stringless Rounds?
« Reply #16 on: November 10, 2004, 12:17:17 PM »
Got my last routine to work with neg number as well as positive ones.
Code: [Select]
; positive or negative numbers with variable break point
(defun rndup (num Brk)
  (float (fix (+ num (* (/ num (abs num))(- 1 brk))))))


(defun c:test()
  (print (rndup 2.2 0.2))
  (print (rndup 2.3 0.3))
  (print (rndup -2.5 0.3))
  (print (rndup -2.2 0.3))
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
Stringless Rounds?
« Reply #17 on: November 10, 2004, 01:52:53 PM »
David, that's exactly what I tried. Maybe I need to exactly try it again. I'm sure I didn't do it right.

David Bethel

  • Swamp Rat
  • Posts: 656
Stringless Rounds?
« Reply #18 on: November 10, 2004, 02:37:01 PM »
Daron, Mark

You could also make it an (if) statement in lieu of (cond)

Code: [Select]
(defun rndby (r b / tmp)
  (setq tmp (rem r b))
  (if (>= tmp (* 0.5 b))
      (+ r (- b tmp))
      (- r tmp)))


-David
R12 Dos - A2K

ImaJayhawk

  • Guest
Stringless Rounds?
« Reply #19 on: November 10, 2004, 05:41:20 PM »
Can you use vla-eval to call the VBA Round function?



--ImaJayhawk

daron

  • Guest
Stringless Rounds?
« Reply #20 on: November 11, 2004, 08:05:06 AM »
Wouldn't know, but I believe there have been a number of ways to skin this cat as I've asked.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Stringless Rounds?
« Reply #21 on: November 14, 2004, 12:21:12 AM »
Speaking of skinning cats ;
Code: [Select]

(defun our:RoundTo (value to-prec)
  (setq to-prec (abs to-prec))
  (* to-prec
     (fix (/ ((if (minusp value) - + )
               value
               (* to-prec 0.5)
             )
             to-prec
          )
     )
  )
)


It makes me a little self-conscious to admit how old this is.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.