# TheSwamp

## Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on September 26, 2003, 02:40:55 PM

Title: math formula
Post by: Mark on September 26, 2003, 02:40:55 PM
Mathematically speaking, does anyone know of a formula to do this:

given the number 502,345 -- I want in return 502,000
given the number 1,345,899  -- I want 1,346,000
given the number 18,373 -- I want 18,000
given the number 5389 -- I want 5000
given the number 130  -- I want 100
Title: math formula
Post by: smadsen on September 26, 2003, 03:03:40 PM
Not a formula .. and a bit crude, but will this do?

Code: [Select]
`(defun round (a / b)  (cond ((>= a (setq b 1000.0)))        ((>= a (setq b 100.0)))        ((>= a (setq b 10.0)))        ((< a 99) (setq b 1.0))  )  (* (fix (/ a b)) b))`
Title: math formula
Post by: Mark on September 26, 2003, 03:16:57 PM
Perfect, thanks Stig.
Title: math formula
Post by: daron on September 26, 2003, 03:17:41 PM
The only problem I see with it, is that it rounds it down, not up.
Title: math formula
Post by: smadsen on September 26, 2003, 03:33:06 PM
Duh! Didn't see that .. thanks Daron.

Even cruder (bet it can be shortened some):

Code: [Select]
`(defun round (a / b)  (cond ((>= a (setq b 1000.0)))        ((>= a (setq b 100.0)))        ((>= a (setq b 10.0)))        ((< a 99) (setq b 1.0))  )  (if (>= (- (abs (setq a (/ a b))) (fix (abs a))) 0.5)    (cond ((minusp a) (setq a (1- a)))          (T (setq a (1+ a))))    a  )  (* (fix a) b))`
Title: math formula
Post by: JohnK on September 26, 2003, 03:47:39 PM
Stig, How did you get here?
Mark, Now we are sittin' at the cool table!

*Se7en does the "Saweeet Stig is here!" dance!*
Title: math formula
Post by: smadsen on September 26, 2003, 03:53:35 PM
Well, if ya don't get an invite ya just gotta crash in, right?  :)

Cool forum, by the way. Nice to see some people are still fond of AutoLISP :)

Should one register here?
Title: math formula
Post by: Mark on September 26, 2003, 03:53:37 PM
Quote
(bet it can be shortened some)

sounds like a challenge to me <g>
Title: math formula
Post by: JohnK on September 26, 2003, 03:57:00 PM
...Should one register here?
Title: math formula
Post by: Mark on September 26, 2003, 03:58:21 PM
Code: [Select]
`(- 8523 (rem 8523.00 1000))`

Title: math formula
Post by: smadsen_000 on September 26, 2003, 04:04:19 PM
Thanks Se7en .. will do!

Yup, REM will do, too. I bet that code could be shortened to a couple of lines once someone's head's been wrapped around it (mine doesn't fit today).
Title: math formula
Post by: Mark on September 27, 2003, 07:16:45 AM
Code: [Select]
`(defun rndx (x)  (cond    ((>= x 1000) (* (atof (rtos (/ x 1000.0) 2 0)) 1000))    ((>= x 100) (* (atof (rtos (/ x 100.0) 2 0)) 100))    ((< x 100) (* (atof (rtos (/ x 10.0) 2 0)) 10))    )  )`
Title: math formula
Post by: daron on September 27, 2003, 03:43:11 PM
Quote
Well, if ya don't get an invite ya just gotta crash in, right?

I want to invite everybody, but not sure about ethical limits. I get the feeling some boards don't like other boards coming in and posting a different site all over theirs. I figured by just linking a possible soultion to this site IS an invite for all who select the link to look around and hopefully register and become an active board member. :D  BTW Stig, Welcome.
Title: math formula
Post by: SMadsen on September 27, 2003, 06:37:19 PM
Daron, thank you.

Mark, interesting solution using string conversion. Using your idea, here's one that returns roundup to nearest ones, tens, hundreds etc..

Code: [Select]
`(defun roundit (a)  (setq b 1.0)  (repeat (1- (strlen (rtos a 2 0)))(setq b (* b 10.0)))  (* (atof (rtos (/ a b) 2 0)) b))`

^Doesn't accept negative numbers though!
Title: math formula
Post by: Mark on September 27, 2003, 07:33:18 PM
This is why I want to round off to the nearest number.

http://theswamp.org/mark.lisp/files/ModelSpace-Grid/

Thanks for the help Stig.