Page 1 of 1

Trigonometric accuracy

Posted: Sun Dec 22, 2019 2:37 pm
by CVH
Andrew,

Knowing trig math is not flawless at certain limits.
Like the sine of a very small angle usually is not that good.

What follows I still find unacceptable: e_surprised

Code: Select all

var ori = 0
var sinOri = Math.sin(deg2rad(ori));
var cosOri = Math.cos(deg2rad(ori));
var tanOri = Math.tan(deg2rad(ori));
debugger;
ori = 45
sinOri = Math.sin(deg2rad(ori));
cosOri = Math.cos(deg2rad(ori));
tanOri = Math.tan(deg2rad(ori));// IS NOT 1
var flaw = 1-Math.tan(deg2rad(ori));//flaw =1.1E-17
debugger;
ori = 90
sinOri = Math.sin(deg2rad(ori));
cosOri = Math.cos(deg2rad(ori));// IS NOT 0 flaw =6.1E-17
tanOri = Math.tan(deg2rad(ori));// IS NOT N/A is huge
debugger;
ori = 135
sinOri = Math.sin(deg2rad(ori));
cosOri = Math.cos(deg2rad(ori));
tanOri = Math.tan(deg2rad(ori));// IS NOT -1
var flaw = 1+Math.tan(deg2rad(ori));//flaw =-2.2E-16
debugger;
ori = 180
sinOri = Math.sin(deg2rad(ori));
cosOri = Math.cos(deg2rad(ori));// IS NOT 0 flaw =1.2E-16
tanOri = Math.tan(deg2rad(ori));// IS NOT 0 flaw =-1.2E-16
debugger;
ori = 225
sinOri = Math.sin(deg2rad(ori));
cosOri = Math.cos(deg2rad(ori));
tanOri = Math.tan(deg2rad(ori));// IS NOT 1
var flaw = 1-Math.tan(deg2rad(ori));//flaw =3.3E-16
debugger;
ori = 270
sinOri = Math.sin(deg2rad(ori));
cosOri = Math.cos(deg2rad(ori));// IS NOT 0 flaw =-1.8E-16
tanOri = Math.tan(deg2rad(ori));// IS NOT N/A is huge
debugger;
ori = 315
sinOri = Math.sin(deg2rad(ori));
cosOri = Math.cos(deg2rad(ori));
tanOri = Math.tan(deg2rad(ori));// IS NOT -1
var flaw = 1+Math.tan(deg2rad(ori));//flaw flaw =-4.4E-16
debugger;
ori = 360
sinOri = Math.sin(deg2rad(ori));
cosOri = Math.cos(deg2rad(ori));// IS NOT 0 flaw =-2.4E-16
tanOri = Math.tan(deg2rad(ori));// IS NOT 0 flaw =-2.4E-16
debugger;
In radians it is somewhat better but with eg. Simple.js or Hatch patterns the angles are in degrees.
The only option I have is to round trigs to 15 digits or to catch these flaws every use of a trig function.

Regards,
CVH

Re: Trigonometric accuracy

Posted: Mon Dec 23, 2019 11:50 am
by andrew
These are typical floating point number problems, common to all computers. It has to do how numbers are stored and how computers are (by nature) limited.

See also:
https://floating-point-gui.de/

Re: Trigonometric accuracy

Posted: Mon Dec 23, 2019 12:25 pm
by CVH
I know that, wrote it myself with lesser words.

However, the mentioned trigonometric identities are unique, universal and fixed.

eg.
tan(90°) or tan(pi/2) = N/A
And is not equal to 'as huge as possible'.
nor it is 16331239353195370
nor that of 270° is 5443746451065123

eg.
sin(90°) or sin(pi/2) = 0
And not almost zero.

Regards,
CVH

Re: Trigonometric accuracy

Posted: Mon Dec 23, 2019 12:33 pm
by andrew
Not sure what to do with this.. this is standard ECMAScript / JavaScript (same in C/C++) and unrelated to QCAD. You might want to implement your own trigonometric functions that handle these cases differently (e.g. function myTan(v) { .... }).

Re: Trigonometric accuracy

Posted: Mon Dec 23, 2019 12:50 pm
by CVH
Did it, Its a stupid looking way around.

But functional at least.
:roll: