### Author Topic: math formula  (Read 6598 times)

0 Members and 1 Guest are viewing this topic.

#### Mark

• Custom Title
• Seagull
• Posts: 28679 ##### math formula
« 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
TheSwamp.org  (serving the CAD community since 2003)

• Guest ##### math formula
« Reply #1 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))`

#### Mark

• Custom Title
• Seagull
• Posts: 28679 ##### math formula
« Reply #2 on: September 26, 2003, 03:16:57 PM »
Perfect, thanks Stig.
TheSwamp.org  (serving the CAD community since 2003)

#### daron

• Guest ##### math formula
« Reply #3 on: September 26, 2003, 03:17:41 PM »
The only problem I see with it, is that it rounds it down, not up.

• Guest ##### math formula
« Reply #4 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))`

#### John Kaul (Se7en) ##### math formula
« Reply #5 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!*
Common sense is not so common. ~Voltaire

--> Donate to TheSwamp.org <--

• Guest ##### math formula
« Reply #6 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?

#### Mark

• Custom Title
• Seagull
• Posts: 28679 ##### math formula
« Reply #7 on: September 26, 2003, 03:53:37 PM »
Quote
(bet it can be shortened some)

sounds like a challenge to me <g>
TheSwamp.org  (serving the CAD community since 2003)

#### John Kaul (Se7en) ##### math formula
« Reply #8 on: September 26, 2003, 03:57:00 PM »
...Should one register here?
Common sense is not so common. ~Voltaire

--> Donate to TheSwamp.org <--

#### Mark

• Custom Title
• Seagull
• Posts: 28679 ##### math formula
« Reply #9 on: September 26, 2003, 03:58:21 PM »
Code: [Select]
`(- 8523 (rem 8523.00 1000))`

TheSwamp.org  (serving the CAD community since 2003)

• Guest ##### math formula
« Reply #10 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).

#### Mark

• Custom Title
• Seagull
• Posts: 28679 ##### math formula
« Reply #11 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))    )  )`
TheSwamp.org  (serving the CAD community since 2003)

#### daron

• Guest ##### math formula
« Reply #12 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. BTW Stig, Welcome.

• Guest ##### math formula
« Reply #13 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!

#### Mark

• Custom Title
• Seagull
• Posts: 28679 ##### math formula
« Reply #14 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.
TheSwamp.org  (serving the CAD community since 2003)