Home / Science / Order of magnitude
Practice implementation
(defun my-within-order-of-magnitude-p (x y &optional n b)
"Return t if X is within N orders of base-B magnitudes of Y.
By default, check for 1 order of base-10 magnitude."
(let ((n (or n 1))
(b (or b 10)))
(and (> (/ (float x) y) (expt b (- 0 n)))
(< (/ (float x) y) (expt b (+ 0 n))))))
(and (my-within-order-of-magnitude-p 1.00 1.01)
(my-within-order-of-magnitude-p 1000 1010))
t
(seq-map (lambda (number)
(list number
(my-within-order-of-magnitude-p 1 number)))
(number-sequence 1 15))
| 1 | t |
| 2 | t |
| 3 | t |
| 4 | t |
| 5 | t |
| 6 | t |
| 7 | t |
| 8 | t |
| 9 | t |
| 10 | nil |
| 11 | nil |
| 12 | nil |
| 13 | nil |
| 14 | nil |
| 15 | nil |