support for various angular units in std.math

Don nospam at nospam.com
Tue Mar 1 00:12:11 PST 2011


Colin Wallace wrote:
> Don Wrote:
> 
>> Colin Wallace wrote:
>>> When using trigonometric functions, they all deal with radians. So
>>> I've been having to do lots of converting. Converting is very
>>> straightforward,
>> Actually, it isn't!
>> assert(sin(360 * PI / 180 ) == 0.0); // fails!
> 
> That's a very marginal error (1e-19). For graphics, I never need that much precision. 

Yes, but someone doing high school trigonometry with degrees will be 
confused when it fails.

> Besides, it has nothing to do with the conversion in this case. sin(2*PI) results in the same value, causing the assertion to fail.

No, it's the conversion. It's because the mathematical number pi is not 
precisely representable in floating point. But 360 is precisely 
representable, and sin(360 degrees) is also precisely representable.
assert(sinPi(360/180) == 0) will pass.
So multiplying by PI is *not* the correct way to calculate trignometric 
functions in degrees.  2*PI is not 360 degrees.



More information about the Digitalmars-d mailing list