Author Topic: Stringless Rounds?  (Read 8694 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.