TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: leekh on July 22, 2020, 11:11:29 PM

Title: getpoint error calculator help
Post by: leekh on July 22, 2020, 11:11:29 PM
Code - Auto/Visual Lisp: [Select]
  1. (defun c:t1( / pt1 pt2)
  2.   (princ "\ndist 0.2 chk")
  3.   (setq pt1 (getpoint "\ndown point : "))
  4.   (setq pt2 (getpoint pt1 "\nup point :"))  
  5.   (setq aa (- (- (cadr pt2) (cadr pt1)) 0.2))
  6.   (princ (strcat "\n cal-chk : " (vl-princ-to-string aa)))
  7.   (princ)
  8.   )
  9.  
  10. (defun c:t2( / pt1 pt2)
  11.   (princ "\ndist 0.2 chk")
  12.   (setq pt1 (getpoint "\ndown point : "))
  13.   (setq pt2 (getpoint pt1 "\nup point :"))  
  14.   (setq aa (- (distance pt1 pt2) 0.2))
  15.   (princ (strcat "\n chk : " (vl-princ-to-string aa)))
  16.   (princ)
  17.   )

Code: [Select]
command: t1
dist 0.2 chk
down point:
up point:
 chk : 2.83107e-15

command:
command: t2
dist 0.2 chk
down point:
up point:
 chk : 2.83107e-15

_$ (- (cadr pt2) (cadr pt1))
0.2
_$ (- (- (cadr pt2) (cadr pt1)) 0.2)
2.83107e-15                              <=error      0.0  ok
_$

_$ (distance pt1 pt2)
0.2
_$ (- (distance pt1 pt2) 0.2)
2.83107e-15                               <=error      0.0  ok
_$

_$ (distance pt1 pt2)
0.2
_$ (= (distance pt1 pt2) 0.2)         <-=error  T ok
nil
getpoint or distance error help me


EDIT (John): Added code tags
Title: Re: getpoint error calculator help
Post by: leekh on July 23, 2020, 05:04:43 AM
command offset 0.2 work

_$ (- (distance pt1 pt2) 0.2)
-1.13798e-14

error

_$ (- (read (vl-princ-to-string (distance pt1 pt2))) 0.2)
0.0

ok

(- (distance pt1 pt2) 0.2)   I don't know why I am getting the error?
Title: Re: getpoint error calculator help
Post by: VovKa on July 23, 2020, 10:56:03 AM
(rtos (distance pt1 pt2) 2 16)
Title: Re: getpoint error calculator help
Post by: leekh on July 23, 2020, 09:44:57 PM
(rtos (distance pt1 pt2) 2 16)

 pt1 pt2 dist = 0.2

 offset 0.2 use

_$ (rtos (distance pt1 pt2) 2 16)
"0.1999999999999993"

why not "0.1999999999999993" is "0.2" ?
Title: Re: getpoint error calculator help
Post by: Dlanor on July 24, 2020, 04:27:03 AM
because you are asking for it to be accurate to 16 significant digits. This is a result of the way floating point math works on computers. GOOGLE "Floating-point error mitigation"

The error is your assumtion that anything that is computed by AutoCAD (which is almost everything) has the value you think it does i.e. the distance between two points is 0.2 where it could be 0.200000000000001 or 0.199999999999998. To all intents and purposes this is 0.2 since (equal 0.199999999999998 0.2 1.0e-13) will return T. AutoCAD's smallest unit is the angstrom (1.0e-10) so the error is insignificant as it is a 1/1000000th of an angstrom. The largest IIRC is the parsec 3.0e16 metres where such an error will be significant, but this is only because of the limited number of computational registers when doing the math, and who apart from astronomers or astrophysicist works with parsecs.
Title: Re: getpoint error calculator help
Post by: Lee Mac on July 24, 2020, 01:32:49 PM
Related:

https://www.theswamp.org/index.php?topic=54883.msg592690#msg592690
https://www.theswamp.org/index.php?topic=44694
https://www.theswamp.org/index.php?topic=53285
https://www.theswamp.org/index.php?topic=8787.msg112317#msg112317
https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems
Title: Re: getpoint error calculator help
Post by: roy_043 on July 24, 2020, 02:28:55 PM
@Dlanor:
What you are saying is confusing. Internally AC works with drawing units (DU). They can be considered mm, inches, angstrom etc. A tolerance of 1.0e-13 DU would be relatively the same whatever 1 DU represents.
Title: Re: getpoint error calculator help
Post by: roy_043 on July 24, 2020, 02:32:33 PM
@leekh:
Some decimal numbers cannot be exactly translated to the binary floating point format. Some loss of accuracy therefore has to be taken into account.
Title: Re: getpoint error calculator help
Post by: leekh on July 26, 2020, 08:42:18 PM
because you are asking for it to be accurate to 16 significant digits. This is a result of the way floating point math works on computers. GOOGLE "Floating-point error mitigation"

The error is your assumtion that anything that is computed by AutoCAD (which is almost everything) has the value you think it does i.e. the distance between two points is 0.2 where it could be 0.200000000000001 or 0.199999999999998. To all intents and purposes this is 0.2 since (equal 0.199999999999998 0.2 1.0e-13) will return T. AutoCAD's smallest unit is the angstrom (1.0e-10) so the error is insignificant as it is a 1/1000000th of an angstrom. The largest IIRC is the parsec 3.0e16 metres where such an error will be significant, but this is only because of the limited number of computational registers when doing the math, and who apart from astronomers or astrophysicist works with parsecs.

Thank you for your explanation Now I can understand
Title: Re: getpoint error calculator help
Post by: leekh on July 26, 2020, 08:43:54 PM
Related:

https://www.theswamp.org/index.php?topic=54883.msg592690#msg592690
https://www.theswamp.org/index.php?topic=44694
https://www.theswamp.org/index.php?topic=53285
https://www.theswamp.org/index.php?topic=8787.msg112317#msg112317
https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems

Thank you for your explanation Now I can understand
Title: Re: getpoint error calculator help
Post by: leekh on July 26, 2020, 08:46:24 PM
@leekh:
Some decimal numbers cannot be exactly translated to the binary floating point format. Some loss of accuracy therefore has to be taken into account.

Thank you for your explanation Now I can understand