Author Topic: Stringless Rounds?  (Read 8697 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
Stringless Rounds?
« on: November 09, 2004, 02:23:28 PM »
I was working on a project last night to round numbers up or down to the nearst 5. I know how to accomplish this by converting reals to strings, back and forth, but I can't imagine there isn't a way to round numbers as numbers. I'm sure I'm just missing this tree for the forest here. Could someone point out how (if) this is possible? Thanks. If it's better done in something like c++, I don't have a problem moving to that forum and picking up that language. I need to anyway.

hendie

  • Guest
Stringless Rounds?
« Reply #1 on: November 09, 2004, 02:37:09 PM »
the only way I can think of at the moment is to increment or decrement the numer by 1, each time testing if the number is divisible by 5
or do a condition where if the number ends in 2, then subtract 2, if it ends in 3 then add 2 ro whatever.

I'm sure there must be a  better way though. That would make a nice challenge though


unless there's already an Lisp method in whch case i've just made an ar$e of myself !  :P

David Bethel

  • Swamp Rat
  • Posts: 656
Stringless Rounds?
« Reply #2 on: November 09, 2004, 04:08:28 PM »
Code: [Select]
(defun rnd5 (r / tmp)
  (setq tmp (rem r 5))
  (cond ((>= tmp 2.5)  (+ r (- 5 tmp)))
        (T             (- r tmp))))


-David
R12 Dos - A2K

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Stringless Rounds?
« Reply #3 on: November 09, 2004, 04:30:32 PM »
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Stringless Rounds?
« Reply #4 on: November 09, 2004, 04:45:14 PM »
Code: [Select]
;Here is another way to handle negative numbers.
(defun rndup (num BrkPoint)
  (setq BrkPoint (cond (BrkPoint) (0.99999999)))
  (if (minusp num)
    (float (fix (- BrkPoint num)))
    (float (fix (+ BrkPoint num)))
  )
)

(defun c:test ()
  (print (rndup 1.01 0.5))
  (print (rndup 1.05 0.5))
  (print (rndup 1.99999 0.5))
  (print (rndup 0.9999 0.5))
  (print (rndup -2.01 0.5))
  (print (rndup -2.5 0.5))
  (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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Stringless Rounds?
« Reply #5 on: November 09, 2004, 05:06:40 PM »
Yeow, I don't know why I thought my round function answered your problem Daron, it doesn't. Apologies for polluting your thread with it.  While it could be modified easily enough to work for your scenario there are other answers that already exist in this thread, so the point is moo. Killing my posts.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

daron

  • Guest
Stringless Rounds?
« Reply #6 on: November 09, 2004, 05:19:33 PM »
David, that function there is a thing of beauty. Works like a charm.

MP, that's very interesting code. I will have to play around with that.

CAB, I may have to ponder your idea about negatives and see if I can do something with it. I don't think I need to worry about negatives at this point, since the only way a number will be had in the case I'm using it couldn't possibly be negative.

Thanks everybody for your input.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Stringless Rounds?
« Reply #7 on: November 09, 2004, 05:46:15 PM »
Quote from: Daron
I don't think I need to worry about negatives at this point, since the only way a number will be had in the case I'm using it couldn't possibly be negative.


Then trim the fat out of my code, you get this:
Code: [Select]
(defun rndup (num) (float (fix (+ 0.5 num))))
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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Stringless Rounds?
« Reply #8 on: November 09, 2004, 06:40:08 PM »
This quickie isn't as beautifully succinct as some other submissions but it's flexible:

Code: [Select]
(defun round ( number by )
    (if (zerop by) number
        (+  (* (fix (/ number (setq by (abs by)))) by)
            (if (< (* 0.5 by) (rem number by))
                by
                0
            )
        )
    )
)

Observe:

Code: [Select]
(progn
    (princ
        (strcat
            (rtos
                (setq number (* pi 1250))
                2
                8
            )
            " (original value)\n\n"
        )
    )
    (foreach precision
       '(   1000
             100
              50
              25
              10
               5
               0.5
               0.05
               0.005
               0.0005
               0.00005
               0.000005
        )
        (princ
            (strcat
                (substr
                    (strcat
                        (rtos (round number precision) 2 8)
                        "                   "
                    )
                    1
                    11
                )
                " rounded by "
                (rtos precision 2 8)
                "\n"
            )
        )
    )
    (princ)
)

3926.99081699 (original value)

4000        rounded by 1000
3900        rounded by 100
3950        rounded by 50
3925        rounded by 25
3930        rounded by 10
3925        rounded by 5
3927        rounded by 0.5
3927        rounded by 0.05
3926.99     rounded by 0.005
3926.991    rounded by 0.0005
3926.9908   rounded by 0.00005
3926.990815 rounded by 0.000005

(Nominally tested, may need polish).

Cheers. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Stringless Rounds?
« Reply #9 on: November 09, 2004, 10:02:35 PM »
Nice one Michael,
I'm adding it to my collection :)
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 #10 on: November 09, 2004, 10:54:05 PM »
Thanks MP, for sending on something that does have possibilities. The other one had something for me to learn from. Then again, so does this. Adding to my things to study list. Thanks. CAB, thanks to you to.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Stringless Rounds?
« Reply #11 on: November 09, 2004, 11:43:19 PM »
You're very welcome Daron.

and thanks Charles, nice of you to say.

=> Michael.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Stringless Rounds?
« Reply #12 on: November 10, 2004, 08:02:01 AM »
Quote from: David D Bethel
Code: [Select]
(defun rnd5 (r / tmp)
  (setq tmp (rem r 5))
  (cond ((>= tmp 2.5)  (+ r (- 5 tmp)))
        (T             (- r tmp))))


-David


David -
that's exactly the same approach I was taking but got hung up on the 2.5 for some reason. Thanks.

MP -
My head hurts. :D
TheSwamp.org  (serving the CAD community since 2003)

daron

  • Guest
Stringless Rounds?
« Reply #13 on: November 10, 2004, 08:09:09 AM »
Interesting you bring that up Mark. I was toying with that code last night to try and make it more flexible, like what if I wanted to round every 2 or 3, instead of five. I tried setting a local variable for the five and dividing it by 2.0, but that didn't work. I knew it wouldn't, but I had to try. I'm still pondering the idea of how to accomplish this. I'm wondering if the value needs to be broken down exponentially or something, but I think that might be too much. I'll have to experiment.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Stringless Rounds?
« Reply #14 on: November 10, 2004, 08:28:44 AM »
If you want to vary the break point you could use:
Code: [Select]
(defun rndup (num Brk)
  (float (fix (+ 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)
)


3.0
3.0
3.0
2.0
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.

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.