     1565 Posts in 517 Topics by 752 Members - Latest Member: zaikunzhang September 25, 2021, 05:09:29 PM    Home | Help | Search | Login | Register |     ### Author Topic: sine and cosine of large angle  (Read 2267 times)

#### Jean-Claude Arbaut

• Newbie
• • Posts: 15 ##### sine and cosine of large angle
« on: October 28, 2016, 05:11:35 AM »
On page 202 of the Absoft Language Reference Manual, there is an example shoing that for large angles, sine losses almost all its precision and becomes useless. This is a known 'feature' of floating point computations. However, the limitation in Absoft's compiler seems to be relatively low.

The example in the LRM uses single precision floating point.
Here is a program using double precision numbers, showing the value of cos(2**n) and sin(2**n), together with some quick checks (sum of square should be 1, and double-hangle formulas should hold).

Code: [Select]
`program trig    implicit none    double precision :: t, x, y, s, xp, yp, sx, sy    integer :: n        xp = cos(1d0)    yp = sin(1d0)    do n = 1, 99        t = 2d0**n        x = cos(t)        y = sin(t)        sx = abs(xp**2 - yp**2 - x)        sy = abs(2d0 * xp * yp - y)        s = hypot(x, y) - 1d0        print "(I2,2F12.8,3E12.4)", n, x, y, sx, sy, s        xp = x        yp = y    end doend program`
What happens is this: starting from 2**28, both sine and cosine are zero.
A closer look shows that the maximum argument giving relevant (nonzero) values is roughly pi*2**26. 

It's not very large, though, since it's close to 2.1d8, far below the precision limit of double precision numbers (which affords 17 digits).
That is, pi*2**26 should give 8 or 9 digits of precision after the decimal separator, and trigonometric functions would still be usable,
though not with extra high precision.

I am not sure this limit is documented in the manual, but it's worth knowing.

Tests were made with Absoft 2017 on Windows, both 64 bits.

 Roughly, but not exactly. For cosine, the factor is not pi but 3.141592628198916999, and for sine it's 3.141592651605606399,
while pi=3.141592653589793238.
« Last Edit: October 28, 2016, 05:15:13 AM by Jean-Claude Arbaut »    