Trigonometric accuracy

If you are having problems with QCAD, post here. Please report bugs through our Bug Tracker instead.

Always attach your original DXF or DWG file and mentions your QCAD version and the platform you are on.

Moderator: andrew

Forum rules
Always indicate Operating system, QCAD version
Attach Drawing files, Screenshots
Post Reply
CVH
Premier Member
Posts: 509
Joined: Wed Sep 27, 2017 4:17 pm
Location: Belgium; Antwerp

Trigonometric accuracy

Post by CVH » Sun Dec 22, 2019 2:37 pm

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
Last edited by CVH on Mon Dec 23, 2019 12:26 pm, edited 1 time in total.
QCADpro 3.24.3.0 / win7pro 32 bit / 4GB / 1e generation i7 2.6Ghz / HDD GeForce GT 520

User avatar
andrew
Site Admin
Posts: 6641
Joined: Fri Mar 30, 2007 6:07 am

Re: Trigonometric accuracy

Post by andrew » Mon Dec 23, 2019 11:50 am

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/

CVH
Premier Member
Posts: 509
Joined: Wed Sep 27, 2017 4:17 pm
Location: Belgium; Antwerp

Re: Trigonometric accuracy

Post by CVH » Mon Dec 23, 2019 12:25 pm

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
QCADpro 3.24.3.0 / win7pro 32 bit / 4GB / 1e generation i7 2.6Ghz / HDD GeForce GT 520

User avatar
andrew
Site Admin
Posts: 6641
Joined: Fri Mar 30, 2007 6:07 am

Re: Trigonometric accuracy

Post by andrew » Mon Dec 23, 2019 12:33 pm

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) { .... }).

CVH
Premier Member
Posts: 509
Joined: Wed Sep 27, 2017 4:17 pm
Location: Belgium; Antwerp

Re: Trigonometric accuracy

Post by CVH » Mon Dec 23, 2019 12:50 pm

Did it, Its a stupid looking way around.

But functional at least.
:roll:
QCADpro 3.24.3.0 / win7pro 32 bit / 4GB / 1e generation i7 2.6Ghz / HDD GeForce GT 520

Post Reply

Return to “QCAD Troubleshooting and Problems”