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

Converting double to string creates inaccuracy

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
matus.brlit
484 Views, 3 Replies

Converting double to string creates inaccuracy

I have this interresting problem, it occured just here, in other situations, the script works fine.

 

I am counting length from coordinates:

 

Command: (setq bod1 ppo_bod1)
(-0.821965 -1.7232 0.0)

Command: (setq bod2 ppo_bod2)
(25.428 13.1268 0.0)

Command: !mfac
0.025

Command: (setq x (atoi (rtos(/ (abs (- (car bod2) (car bod1))) mfac))))
1050

Command: (setq y (atoi (rtos(/ (abs (- (cadr bod2) (cadr bod1))) mfac))))
593

Command: (- (cadr bod2) (cadr bod1))
14.85

Command: (/ (abs (- (cadr bod2) (cadr bod1))) mfac)
594.0

Command: (rtos(/ (abs (- (cadr bod2) (cadr bod1))) mfac))
"593.99999998"

Command: (atoi (rtos(/ (abs (- (cadr bod2) (cadr bod1))) mfac)))
593

 

As you can see, the conversion from 594.0 (real) to string causes an inaccuracy, which is strange, because the length really is 594.0

3 REPLIES 3
Message 2 of 4
Lee_Mac
in reply to: matus.brlit

This is because doubles are allocated a limited amount of memory for storage and hence are expressed to a limited precision; consequently, small rounding errors will be introduced at the lowest level of precision and these will accumulate for every arithmetic operation thereafter. This effect is described in more detail here.

 

To circumvent the problems that arise from this inherent behaviour, you should use appropriate tolerances when comparing doubles (such as provided with the AutoLISP equal function) and round the values when an integer is required (for example, using the rtos function).

 

In your example:

 

_$ (atoi (rtos (/ (abs (- (cadr bod2) (cadr bod1))) mfac) 2 0))
594

 

 

 

Message 3 of 4
matus.brlit
in reply to: Lee_Mac

thank you

Message 4 of 4
Lee_Mac
in reply to: matus.brlit

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

Post to forums  

Autodesk Design & Make Report

”Boost