TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: deegeecees on January 26, 2006, 02:32:28 PM

Title: Real To Integer - Trying To Find A Better Way:
Post by: deegeecees on January 26, 2006, 02:32:28 PM
Does anyone know of a better way to do this:

Code: [Select]
(setq ge_dim_scl1 240.00)
(setq ge_dim_scl1 (atoi (rtos ge_dim_scl1 2 2)))

return val = 240
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: T.Willey on January 26, 2006, 02:35:58 PM
Try
Code: [Select]
(setq ge_dim_scl1 240.00)
(fix ge_dim_scl1)
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: deegeecees on January 26, 2006, 03:55:04 PM
That gave me 239 when I tried it. Hmmm....
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: T.Willey on January 26, 2006, 04:05:23 PM
Quote
Command: (setq ge_dim_scl1 240.00)
240.0

Command: (fix ge_dim_scl1)
240

Maybe your value isn't really set to 240.00, it might be 239.99999999999999999999999999.  Then fix will set it to the lower amount.  I have seen rounding routines around (wow that was hard to type) but I don't have one.
Quote
Command: (fix 239.99999999)
239
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: CADmium on January 27, 2006, 02:48:07 AM
Try this:
Code: [Select]
(defun ROUND (ZAHL STELLEN / TEMP )
  (if (and(numberp ZAHL) (=(type STELLEN) 'INT))
    (progn
      (setq TEMP (abs ZAHL))
      (repeat STELLEN (setq TEMP (* TEMP 10.0)))     
      (cond 
        ( (> (- TEMP (fix TEMP)) 0.5) (setq TEMP (+ (fix TEMP) 1)))
        ( (< (- TEMP (fix TEMP)) 0.5) (setq TEMP    (fix TEMP)  ))
        ( (= (- TEMP (fix TEMP)) 0.5)         
          (if (equal (/ (fix TEMP) 2.0) (fix(/ (fix TEMP) 2.0)) 0.001)
            (setq TEMP    (fix TEMP)  )
            (setq TEMP (+ (fix TEMP) 1))
          ) 
        )
      )
      (repeat STELLEN (setq TEMP (/ TEMP 10.0)))
      (setq TEMP (* TEMP (if (< Zahl 0 ) -1.0 1.0)))
    )
  )
  TEMP   
)

(round Pi 3) --> 3.142
(round  13.999 0) ->14.0
(fix(round  13.999 0)) -> 14

or this
Code: [Select]
(defun ROUND2 (ZAHL BASIS / TEMP )
  (if (and(numberp ZAHL) (=(numberp BASIS))(> BASIS 0))
    (progn     
      (setq TEMP (/ (abs ZAHL)  BASIS))
      (cond 
        ( (> (- TEMP (fix TEMP)) 0.5) (setq TEMP (+ (fix TEMP) 1)))
        ( (< (- TEMP (fix TEMP)) 0.5) (setq TEMP    (fix TEMP)  ))
        ( (= (- TEMP (fix TEMP)) 0.5)         
          (if (equal (/ (fix TEMP) 2.0) (fix(/ (fix TEMP) 2.0)) 0.001)
            (setq TEMP    (fix TEMP)  )
            (setq TEMP (+ (fix TEMP) 1))
          ) 
        )
      )     
      (setq TEMP (* BASIS TEMP (if (< Zahl 0 ) -1.0 1.0)))
    )
  )
  (if (=(type BASIS)'INT) (fix TEMP) TEMP)   
)

(ROUND2 12.63 0.5) ->12.5
(ROUND2 12.63   1) -> 13
(ROUND2 12.63   2) -> 12
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: Kerry on January 27, 2006, 02:51:41 AM
Nice Contribution ! !
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: Joe Burke on January 27, 2006, 04:47:43 AM
A couple others.

;; by Doug Broad
(defun round (value to)
  (setq to (abs to))
  (* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))
)

;; Joe Burke 2/23/03
(defun round (value to)
  (if (zerop to)
    value
    (* (atoi (rtos (/ (float value) to) 2 0)) to)
  )
)
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: Fatty on January 27, 2006, 05:15:57 AM
And my two cent :-)

Code: [Select]
(defun rin (n)
(fix (distof (rtos n 2 0))))
(rin 8.4135480789);=>8
(rin 8.5135480789);=>9

~'J'~
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: CAB on January 27, 2006, 09:17:57 PM
Code: [Select]
;;  CAB
; positive or negative numbers with variable break point
(defun rndup (num Brk)
  (float (fix (+ num (* (/ num (abs num))(- 1 brk))))))


http://www.theswamp.org/forum/index.php?topic=3076.0
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: Kerry on January 27, 2006, 10:19:06 PM
wow !
Quote
Speaking of skinning cats ;

hehehehehe
Title: Re: Real To Integer - Trying To Find A Better Way:
Post by: zoltan on January 28, 2006, 09:48:35 AM
CADmium:  Could you explain the logic of your functions line by line so I can understand them better?