[erlang-questions] sine and cosine with degree angles
Richard O'Keefe
ok@REDACTED
Fri Aug 27 05:38:43 CEST 2010
On Aug 26, 2010, at 11:03 PM, Roberto Ostinelli wrote:
>
> however, due to approximations there seem to be an error in the
> computation of the cosine of 90° [which should be 0]:
>
> 1> math:sin(90 * math:pi() / 180).
> 1.0
> 2> math:sin(0 * math:pi() / 180).
> 0.0
> 3> math:cos(90 * math:pi() / 180).
> 6.123233995736766e-17
> 4> math:cos(0 * math:pi() / 180).
> 1.0
As a matter of fact cos(90) looks about as good as it can
get. You can't get *precisely* close to pi/2; the closest
you can get is pi/2 +/- epsilon, and the cosine of that
will be -/+ epsilon.
> cos(1.57079632679489661923132169163975144)
[1] 6.123234e-17
Now Ada lets you write
cos(90, cycle => 360)
and Sun's math library lets you write
cospi(90.0/180.0)
where cospi(x) = cos(pi * x) as if computed with an infinitely
precise pi. We would expect either of those to return an
exact 0.
With the traditional C interface, 6.123233995736766e-17 is as
close as you are going to get.
More information about the erlang-questions
mailing list