TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Jeremy Dunn on April 29, 2021, 04:08:20 PM

Title: change a real to a list
Post by: Jeremy Dunn on April 29, 2021, 04:08:20 PM
I want to take a number and change it into a format where I have  a list of a list of digits and the exponent. Let us start with an integer first. Say we have the integer 189. We change this to scientific notation and it becomes 1.89e2 . But we actually want to shift this one place further over so we have all of the significant digits on one side. Now we have 0.189e3 . I then want this in the form ((1 8 9) 3). If the number is negative then the digits are negative so that -189 is ((-1 -8 -9) 3). If the integer is real as in 189.0 we need to be able to detect this case and chop off the trailing zeros. I wish my routine to detect general real numbers like 12.382e-3 and convert it to 0.12382e-1 and thence to ((1 2 3 8 2) -1). I was trying to do all this by turning the number into a string and then splitting it up but there must be a purely numerical way of doing this. I'm sure our local experts will have different ways of going about it. Note: 0 would just default to ((0) 1). The digit list otherwise begins with the first non-zero digit.
Title: Re: change a real to a list
Post by: BIGAL on April 29, 2021, 07:44:16 PM
Can you explain why your wanting to do this there may be better ways of a solution.
Title: Re: change a real to a list
Post by: apricot125 on November 05, 2021, 04:52:48 AM
try  :2funny:
Code: [Select]
(defun SplintbyE (string / prefix suffix len start char found)
  (setq len (strlen string) start 1 prefix "" suffix "" found nil)
  (repeat len
    (setq char (substr string start 1))
    (if (= "E" char)
      (setq found T char "")
      (if (not found)
          (setq prefix (strcat prefix char))
          (setq suffix (strcat suffix char))
        )   ; _ end of if
    )  ; _ end of if
    (setq start (1+ start))
  )  ; _ end of repeat
  (list prefix suffix)
)  ; _ end of defun

(defun strblk (string / len start result sign)
  (if (= "-" (substr string 1 1)) (setq sign "-" string (substr string 2))(setq sign ""))
  (setq len (strlen string) start 1 result '() )
  ;;  string ---> list
  (repeat len
    (setq char (substr string start 1))
    (if (/= "." char)        ; remove the dot
      (setq result (append (list char) result)))
    (setq  start (1+ start))
  ) ; _ end of repeat
  ;; remove " " or "0" in the trailing
  (while (= "0" (car result))
    (setq result (cdr result))
  )
  (if (= "-" sign) (setq result (mapcar '(lambda (x) (strcat sign x)) result)))
  (reverse result)
)  ; _ end of defun

(defun Real2List( a / temp string prefix suffix)
  (if (zerop a) (list '(0) 1)
    (progn
      (setq string (rtos a 1 16)
          temp   (SplintbyE string)
          prefix (strblk (car temp))
          suffix (1+ (atoi (cadr temp)))
      )
      (list prefix suffix)
    )  ; _ end of progn
  ) ; _ end of if
)  ; _ end of defun

(defun test()
  (princ (Real2List 189))
  (princ (Real2List -189))
  (princ (Real2List 189.0))
  (princ (Real2List 12.382e-3))
  (princ (Real2List 0))
)