Use of trigonometry identities in code

New Topic
This topic has been archived, and won't accept reply postings.
 ablackett 04 Nov 2022

I remember a thread on here from a while back where someone had used some trig identities in their code and was getting nonsense results from the programme.  The issue came down (I think) to them having a trig function, which was evaluated as very close to zero giving a floating point error.

The suggestion was that he should have been using trig identities to tidy up his code.

Can anyone give me examples of what physical situations this might be useful in, and perhaps some examples of the functions or identities they have used in code?

I'm writing 'What's the point of....' introduction slides for the A Level Maths course and wanted to include this as an example of "What's the point of trig identities?"

Thanks

 joeramsay 04 Nov 2022
In reply to ablackett:

Not a physical situation/simulation, but an example of a function that, by using trig identities, you can better accuracy in a certain range. Apologies if this isn't quite what you were looking for and I'm teaching you to suck eggs.

Suppose you have f(x) = cos(x) - 1.

In any floating-point representation there is a maximum value of x, below which the true value of cos(x) rounds to 1 in that representation. If you look at the source code for GLIBC double-precision cos (https://github.com/lattera/glibc/blob/master/sysdeps/ieee754/dbl-64/s_sin.c), you can see that below 2^-27 they just return 1.

So for the entire tiny region, because cos(x) rounds to 1, f(x) will be 0. However for a lot of that region f(x) would actually have been representable to much greater precision than that.

By trig identities you also have f(x) = -2 * (sin(x / 2))^2 (I think?).

At tiny input, sin(x) ~= x, meaning you lose less precision with sin than cos (fp-density of input is similar to that of output). So evaluating f(x) by the second option will be nonzero down to a much smaller input.

Not a very sexy example, but hope it helps!

Post edited at 10:48
 Andy Hardy 04 Nov 2022
In reply to ablackett:

I had to program a machine to sandblast parabolic dishes out of stone. It had all the ingredients of a classic UKC thread: turntables, assumptions, a smattering of A-level maths (not much trigonometry). If you can stand the excitement, I can send you some details?

 magma 04 Nov 2022
In reply to ablackett:

maybe unrelated?, but i had a strange error that popped up in my code for a drainage app that uses a kml file of lat/lon points (manholes etc) to calculate pipe runs/lengths etc. Took a while to realize the problem site had points exactly on the Greenwich meridian (to within a couple of m) which somehow messed up the calculations to give an error (possibly the pipe length calculation using trig?) not sure if this is what you are getting at?..

 jonny taylor 04 Nov 2022
In reply to ablackett:

Hi Blackett,

Not quite what you asked for, but if you want another "what's the point of..." for the sinA+sinB formulae, my physicist's answer would be that it can help with physical interpretation of an equation. If you have two waves beating against each other:

y1 = sin(0.99*x) + sin(1.01*x)

then it's hard to visualise what that will look like, but:
y2 = 2  * sin(x) * cos(0.01*x)

is easy to visualise as a carrier wave and an envelope. (see also AM radio).

The double-angle formulae also give physical insight into how you get harmonics from an oscillator with a nonlinear response, but that's less easily accessible and familiar in terms of the physics involved.

 kestrelspl 04 Nov 2022
In reply to ablackett:

If you had got into some silly case where you were trying to calculate tan(x)*cos(x) I imagine the computer would get that very wrong around ±pi/2 (90 degrees in laymans speak). If you just used sin(x) it would be easy.

OP ablackett 04 Nov 2022
In reply to Andy Hardy:

You have told me this before, I’ve got a whole lesson based on quadratic equations and circles based on it!


New Topic
This topic has been archived, and won't accept reply postings.
Loading Notifications...