Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

high precision calculation

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
aqdam1978
662 Views, 3 Replies

high precision calculation

Hi

I prepared a lisp code that needs high precision calculation:

 

(defun Time2Frac ( h m s hsec)
(+ (/ h 24.0) (/ m 1440.0) (/ s 86400.0) (/ hsec 8640000.0))
)

 

 

test:

(Time2Frac 23 59 59 99)

it returns 1.0 but the real result is 0.9999998842592

 

so, how can I change the precision of AutoCAD for some calculation?

 

Thanks

 

3 REPLIES 3
Message 2 of 4
Kent1Cooper
in reply to: aqdam1978


@aqdam1978 wrote:

....

I prepared a lisp code that needs high precision calculation:

 

(defun Time2Frac ( h m s hsec)
(+ (/ h 24.0) (/ m 1440.0) (/ s 86400.0) (/ hsec 8640000.0))
)

test:

(Time2Frac 23 59 59 99)

it returns 1.0 but the real result is 0.9999998842592

 

so, how can I change the precision of AutoCAD for some calculation?

....


It actually "knows" the precise value, even beyond your real result, but shows it rounded off only as a matter of display.  You can prove it by converting it to a text string and asking for loads of decimal places:

 

Command: (rtos (Time2Frac 23 59 59 99) 2 16)
"0.9999998842592592"

 

You should be able to affect its display rounding with Units settings.

Kent Cooper, AIA
Message 3 of 4
aqdam1978
in reply to: Kent1Cooper

Hi kent,

 

I prepared reverse function and you are right!

 

(defun Time2Frac ( h m s hsec)
(+ (/ h 24.0) (/ m 1440.0) (/ s 86400.0) (/ hsec 8640000.0))
)


(defun Frac2Time ( F / hsec s m h)
(setq F (* F 8640000))
(setq h (fix(/ F 360000)) F (rem F 360000))
(setq m (fix(/ F 6000)) F (rem F 6000))
(setq s (fix(/ F 100)) F (rem F 100))
(if (> (- F (fix F)) 0.9) (setq hsec (1+ (fix F))) (setq hsec (fix F)))
(if (= hsec 100) (progn (setq hsec 0) (setq s (1+ s))))
(princ h)(princ ":")(princ m)(princ ":")(princ s)(princ ".")(princ hsec)(princ)
)

;;FOR TEST:
;;(Frac2Time (Time2Frac 23 59 59 99))
;;result = 23:59:59.99

 

 

Thanks for your help.

 

Abbas

Message 4 of 4
Kent1Cooper
in reply to: aqdam1978


@aqdam1978 wrote:

....

I prepared reverse function and you are right!

.... 

Thanks for your help.

....


You're welcome.  [I can't take full credit for having known that all along, but similar questions have come up here before, and I thought I could just answer it about as quickly as Search for one of those other threads.]

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost