For the original Poster @avinash00002002
Was passing and marvelled that this thread has reached 22 posts.
For giggles I added this to VSCode and added 3 prompts to GPT-5.
Note that the 3 prompts could have been combined but I chose 3 as an example of building a solution.
Original Code.
;| Using AutoLisp for AutoCAD 2025
Change all items to the nearest 0.5 precision.
|;
;; Original sample list:
(setq original_list '(25 299.4 100.3 100.8 399.3 118.9 25))
Prompt 01:
Please solve as documented.
Prompt 02
Excellent, now we want to sum the `rounded_list` values please
Prompt 03
That result is satisfactory. Now, please round the returned value from `sumList` (if it is fractional), save the value and display the summed value.
These prompts were based on the information in the initial post.
It seems to me that you could have had the following unedited solution in 10 minutes instead of waiting 3 days.
;| Using AutoLisp for AutoCAD 2025
Change all items to the nearest 0.5 precision.
This sample defines:
- original_list : the source numeric list
- round-to-0.5 : helper function (value -> nearest 0.5)
- roundListToHalf : maps a list to nearest 0.5 values
- FIRSTTEST command: prints both lists to command line
|;
;; Original sample list (fixed syntax: remove extraneous paren before first 25)
(setq original_list '(25 299.4 100.3 100.8 399.3 118.9 25))
;; Rounds a real number to the nearest 0.5 increment
;; v : real number
;; return: real rounded to nearest multiple of 0.5
;; NOTE: Using (fix (+ 0.5 x)) only works for positive numbers. For negatives
;; we mirror to positive, round, then restore the sign so that -0.3 -> -0.5 (nearest)
(defun round-to-0.5 (v / a)
(setq a (abs v))
(/ (* (if (minusp v) -1.0 1.0) (float (fix (+ 0.5 (* a 2.0))))) 2.0)
)
;; Alternate (more explicit) method using rounding of scaled value:
;;(defun round-to-0.5 (v) (/ (float (fix (+ 0.5 (* v 2.0)))) 2.0))
;; Map list values to nearest 0.5
(defun roundListToHalf (lst /)
(if lst
(cons (round-to-0.5 (car lst)) (roundListToHalf (cdr lst)))
nil
)
)
;; Sum a list of numbers (tail-recursive style helper)
(defun sumList (lst / acc)
(setq acc 0.0)
(while lst
(setq acc (+ acc (car lst))
lst (cdr lst))
)
acc
)
;; Precompute (optional) or could compute inside command each time.
(setq rounded_list (roundListToHalf original_list))
;; Store the floating sum and an integer-rounded version (nearest integer)
;; If you prefer always recomputing, move these inside the command.
(setq rounded_sum (sumList rounded_list))
(setq rounded_sum_int (fix (+ 0.5 rounded_sum))) ; nearest whole number
;; Command to test: (type FIRSTTEST at command line after loading)
(defun c:FIRSTTEST (/)
;; Recompute in case user altered original_list after load
(setq rounded_list (roundListToHalf original_list)
rounded_sum (sumList rounded_list)
rounded_sum_int (fix (+ 0.5 rounded_sum)))
(princ "\nOriginal List: ") (princ original_list)
(princ "\nRounded List: ") (princ rounded_list)
(princ "\nSum (float): ") (princ rounded_sum)
(princ "\nSum (integer): ") (princ rounded_sum_int)
(princ "\nDone. ")
(princ)
)
;; Silent load
(princ "\nTest_List_Manipulation.lsp loaded. Use FIRSTTEST.")
(princ)
All comments are added by the CHAT bot.
I haven't tried to refactor it to be more streamlined . . . but the chat bot could do that for you if needed.
The actual CHAT responses are several pages long and contain a complete explaination of all the code and the thinking behind it,
which would be ideal to learn from.
To me this is a reasonable sample of modular programming which improves the learning process as well as allows for progressive testing to proove the concepts and the code functionallity.
Live long and prosper,
Regards,
// Called Kerry or kdub in my other life.
Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub
NZST UTC+12 : class keyThumper<T> : Lazy<T>; another Swamper