    See the top rated post in this thread. Click here

# Thread: round to nearest sixteenth

1. Login to Give a bone

## round to nearest sixteenth

Is there an easy way to round a given real number to the nearest sixteenth?

for example

1.2689 -> 1.25
1.8845 -> 1.875
0.998 ->1
0.4389 ->0.4375  Reply With Quote

2. Login to Give a bone

## Re: round to nearest sixteenth

Code:
```(distof (rtos 1.903234 5 4) 2)
;;;
;;;
: (distof (rtos 1.2689 5 4) 2)
1.25
: (distof (rtos 1.8845 5 4) 2)
1.875
: (distof (rtos 0.998 5 4) 2)
1.0
: (distof (rtos 0.4389 5 4) 2)
0.4375```  Reply With Quote

3. Login to Give a bone

## Re: round to nearest sixteenth Originally Posted by rkmcswain Code:
```(distof (rtos 1.903234 5 4) 2)
;;;
;;;
: (distof (rtos 1.2689 5 4) 2)
1.25
: (distof (rtos 1.8845 5 4) 2)
1.875
: (distof (rtos 0.998 5 4) 2)
1.0
: (distof (rtos 0.4389 5 4) 2)
0.4375```
Thank you. I have to read up on 'distof' and 'rtos' to understand what you posted. I came up with something below using Lee Mac subroutines

Code:
```;; Round Down  -  Lee Mac
;; Rounds 'n' down to the nearest 'm'

(defun LM:rounddown ( n m )
((lambda ( r ) (cond ((equal 0.0 r 1e-8) n) ((< n 0) (- n r m)) ((- n r)))) (rem n m))
)

;; Round Up  -  Lee Mac
;; Rounds 'n' up to the nearest 'm'

(defun LM:roundup ( n m )
((lambda ( r ) (cond ((equal 0.0 r 1e-8) n) ((< n 0) (- n r)) ((+ n (- m r))))) (rem n m))
)

(defun AS:round16th (x)
(setq d1 (- (LM:roundup x 0.0625) x))
(setq d2 (- x (LM:rounddown x 0.0625)))
(cond ((> d1 d2) (LM:rounddown x 0.0625))
(T (LM:roundup x 0.0625))
)
)

(setq y (AS:round16th 0.468))```
Edit:
After playing around with your code it works great (and a hell of a lot simpler!!)
Thanks  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•