[erlang-questions] sine and cosine with degree angles

Dmitry Belyaev rumata-estor@REDACTED
Thu Aug 26 14:09:41 CEST 2010


I just wanted to show that the problem is not with degrees but with real 
numbers.
However, you can make your own sin/cos for _integer_ degrees:

cos(0) -> 1;
cos(90) -> 0;
cos(-90) -> 0;
cos(180) -> -1;
cos(-180) -> -1;
cos(Deg) when Deg >= 180 orelse Deg <= -180 -> cos(Deg rem 360);
cos(Deg) -> math:cos(90 * math:pi() / 180).

Dmitry Belyaev


On 08/26/2010 03:57 PM, Roberto Ostinelli wrote:
> 2010/8/26 Dmitry Belyaev<rumata-estor@REDACTED>:
>    
>> 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
>>      
> i know it's about approximations. this is why i was asking if i'm
> missing an erlang function which takes degrees instead of radiants as
> input.
>
> thank you for your suggestion, i will use the EPS.
>
> cheers :)
>
> r.
>
>    


More information about the erlang-questions mailing list