I'm investigating floating point accuracy in Fortran 95 and I have the following code:
PROGRAM MAIN
IMPLICIT NONE
integer, parameter :: dp = SELECTED_REAL_KIND(15,307)
REAL(dp), PARAMETER :: DEG2RAD = ATAN(1.d0)/45.d0
! ---- Calculate sine and cosine
WRITE (*,*) COS(60.d0*DEG2RAD) - DBLE(0.5)
WRITE (*,*) SIN(60.d0*DEG2RAD) - SQRT(3.d0)/2.d0
END PROGRAM MAIN
I'm expecting both of the printed lines to be zero, but only the latter one is. Could anyone explain why that is? Of course I understand that the errors are smaller than double precision, but still the first one prints 1e-16, while the second one shows only zeros. Is there a way to make them both "exactly" zero?