[erlang-questions] sine and cosine with degree angles

Dmitry Belyaev rumata-estor@REDACTED
Thu Aug 26 13:36:49 CEST 2010


Suddenly
1> math:sin(math:pi()).
1.2246063538223773e-16

It's floating point, it's always not completely accurate.

Check for epsilon range:
-define(EPS, 1.0e-10).
  if -?EPS < Value andalso Value < ?EPS -> 0; _ -> Value end.

Dmitry Belyaev


On 08/26/2010 03:03 PM, Roberto Ostinelli wrote:
> dear list,
>
> sorry if this does sound stupid but i need to compute sine and cosine
> of angles expressed in degrees.
>
> the math:sin/1 and math:cos/1 functions compute respectively the sine
> and cosine of radiant angles, which can be converted to degrees simply
> like this:
>
> radians = degrees * π / 180
>
> 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
>
> any suggestions on how to proceed? am i missing some erlang functions on this?
>
> thank you,
>
> r.
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>
>    


More information about the erlang-questions mailing list