@mid-awe wrote:
@dbroad FYI, your droot3 & droot4 both return only 0.0 when the integer is too big. Kent's version is still working with 12345678987654321.
Hi mid-awe,
Kent's version is still working with 12345678987654321, but errors.
The digital root from 12345678987654321 is 9, not 8.0...
AutoLISP integers are numbers with values ranging from +2,147,483,647 to -2,147,483,648
There are several approaches to deal with this limitation (if you do a search you will find several), one possible method is using a string instead of an integer, of course you have to ensure that the supplied string, represents an integer...
To test if a string represents an integer, we can use something like this
(defun IsIntStr (str)
(cond ((and (eq (type str) 'STR)
(/= str "")
)
(vl-every (function (lambda (x) (vl-position x '(48 49 50 51 52 53 54 55 56 57)))) (vl-string->list str))
)
)
)
A few tests to demonstrate what I'm saying
_$ (defun droot_h (int_str / lst)
(while (> (length (setq lst (vl-string->list int_str))) 1)
(setq int_str (itoa (apply '+ (mapcar '(lambda (x) (atoi (chr x))) lst))))
)
)
DROOT_H
_$ (defun DROOT2 (int / rem9)
(setq rem9 (rem (abs int) 9))
(cond
((= int 0) 0)
((= rem9 0) 9)
(rem9)
); cond
); defun
DROOT2
_$ (droot2 12345678987654321)
8.0
_$ (droot_h "12345678987654321")
"9"
_$ (droot2 8888888888888888888888888888888888888888)
2.0
_$ (droot_h "8888888888888888888888888888888888888888")
"5"
_$
Hope this helps,
Henrique