Author Topic: Real To Integer - Trying To Find A Better Way:  (Read 8021 times)

0 Members and 1 Guest are viewing this topic.

deegeecees

  • Guest
Real To Integer - Trying To Find A Better Way:
« 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

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Real To Integer - Trying To Find A Better Way:
« Reply #1 on: January 26, 2006, 02:35:58 PM »
Try
Code: [Select]
(setq ge_dim_scl1 240.00)
(fix ge_dim_scl1)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

deegeecees

  • Guest
Re: Real To Integer - Trying To Find A Better Way:
« Reply #2 on: January 26, 2006, 03:55:04 PM »
That gave me 239 when I tried it. Hmmm....

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Real To Integer - Trying To Find A Better Way:
« Reply #3 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
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

CADmium

  • Newt
  • Posts: 33
Re: Real To Integer - Trying To Find A Better Way:
« Reply #4 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
"Bei 99% aller Probleme ist die umfassende Beschreibung des Problems bereits mehr als die Hälfte der Lösung desselben."

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Real To Integer - Trying To Find A Better Way:
« Reply #5 on: January 27, 2006, 02:51:41 AM »
Nice Contribution ! !
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Joe Burke

  • Guest
Re: Real To Integer - Trying To Find A Better Way:
« Reply #6 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)
  )
)

Fatty

  • Guest
Re: Real To Integer - Trying To Find A Better Way:
« Reply #7 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'~

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Real To Integer - Trying To Find A Better Way:
« Reply #8 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
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Real To Integer - Trying To Find A Better Way:
« Reply #9 on: January 27, 2006, 10:19:06 PM »
wow !
Quote
Speaking of skinning cats ;

hehehehehe
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

zoltan

  • Guest
Re: Real To Integer - Trying To Find A Better Way:
« Reply #10 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?