Home:ALL Converter>Precision of Sin vs Cos functions in Fortran

Precision of Sin vs Cos functions in Fortran

Ask Time:2018-01-26T16:46:24         Author:user1887919

Json Formatter

Consider the code,

PROGRAM TRIG_TEST
IMPLICIT NONE

DOUBLE PRECISION, PARAMETER :: PI=4.D0*DATAN(1.0D)

print *, sin(PI/2.0), cos(PI/2.0)

END PROGRAM TRIG_TEST

Compiling with gfortran outputs,

1.0000000000000000 6.1232339957367660E-017

I am aware of the usual floating point issues but is there a reason why the sin function is identically 1, but the cos function is not identically zero?

Author:user1887919,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48458081/precision-of-sin-vs-cos-functions-in-fortran
Eric Postpischil :

The following assumes double is the IEEE 754 basic 64-bit binary format. Common implementations of the trigonometric routines are less accurate than the format supports. However, for this answer, let’s assume they return the most accurate results possible.\nπ cannot be exactly represented in double. The closest possible value is 884279719003555 / 281474976710656 or 3.141592653589793115997963468544185161590576171875. Let’s call this p.\nThe sine of p/2 is about 1 − 1.8747•10−33. The two values representable in double on either side of that are 1 and 0.99999999999999988897769753748434595763683319091796875, which is about 1 − 1.11•10−16. The closer of those is 1, so the closest representable value to sine of p/2 is exactly 1.\nThe cosine of p/2 is about 6.123233995736765886•10−17. The closest value representable in double to that is 6.12323399573676603586882014729198302312846062338790031898128063403419218957424163818359375•10−17.\nSo the results you observed are the closest possible results to the true mathematical values.",
2018-01-26T11:45:12
yy