Bug 5900 – std.math.radians(), std.math.degrees()

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-04-27T11:20:00Z
Last change time
2011-10-29T13:34:29Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-04-27T11:20:10Z
I suggest to add to std.math two simple functions for radians <-> degrees conversion. Similar functions are present in the Python math library too: http://docs.python.org/library/math.html#angular-conversion A possible implementation: import std.math: PI; import std.traits: Select, isFloatingPoint; /// Returns true if a type T is a cfloat, cdouble or creal. // It returns false on ireal, ifloat and idouble. template isComplex(T) { enum bool isComplex = is(T == cfloat) || is(T == cdouble) || is(T == creal); } /// Converts from degrees to radians. @safe pure nothrow Select!(isFloatingPoint!T || isComplex!T, T, double) radians(T)(in T x) { return x * (PI / 180); } /// Converts from radians to degrees. @safe pure nothrow Select!(isFloatingPoint!T || isComplex!T, T, double) degrees(T)(in T x) { return x / (PI / 180); } unittest { real r = 25.2; static assert (is(typeof(radians(r)) == real)); double d = 25.2; static assert (is(typeof(radians(d)) == double)); float f = 25.2; static assert (is(typeof(radians(f)) == float)); int i = 25; static assert (is(typeof(radians(i)) == double)); int c = 'f'; static assert (is(typeof(radians(c)) == double)); creal cr = 25.2 + 0i; static assert (is(typeof(radians(cr)) == creal)); cdouble cd = 25.2 + 0i; static assert (is(typeof(radians(cd)) == cdouble)); cfloat cf = 25.2 + 0i; static assert (is(typeof(radians(cf)) == cfloat)); // more runtime tests needed }
Comment #1 by bugzilla — 2011-04-27T16:33:44Z
No, please no. 1. Phobos should not be filled up with trivia. 2. Multiplying by a constant is trivia. 3. Documenting and adding unit tests for trivia is a waste of our very limited resources. 4. Any numerics programmer who cannot figure out what constant to multiply with to convert degrees <=> radians has no business using trig functions. 5. Python being bloated with trivia does not mean Phobos should be. 6. Degrees and radians are not distinct types, leading to potentially ugly bugs if one writes code that mixes the two up.
Comment #2 by clugdbug — 2011-04-28T00:31:53Z
It's worse than that. A bigger issue is that it encourages the wrong approach. These functions would encourage people to write wrong code like this: sin(degreesToRadians(360)); Which gives the wrong answer -- sin(360degrees) should be EXACTLY zero, not a small nonsense value like 1.4534e-17. I don't think it's fair to trick people like that. The correct way to do trig with degrees is: sin( ((x%360.0)/180)*PI ); I'll put this in the docs for std.math, since it's not obvious.
Comment #3 by kennytm — 2011-04-28T01:47:32Z
(In reply to comment #2) > It's worse than that. A bigger issue is that it encourages the wrong approach. > These functions would encourage people to write wrong code like this: > sin(degreesToRadians(360)); > Which gives the wrong answer -- sin(360degrees) should be EXACTLY zero, not a > small nonsense value like 1.4534e-17. > I don't think it's fair to trick people like that. > > The correct way to do trig with degrees is: sin( ((x%360.0)/180)*PI ); > I'll put this in the docs for std.math, since it's not obvious. Well maybe *this* is the function that should be added instead of degrees(). T degrees(alias f)(T theta) if (isFloatingPoint!T && f is sin ...) { return f( (theta % 360.0) / 180 * PI ); }
Comment #4 by bugzilla — 2011-10-29T13:34:29Z
*** Issue 6862 has been marked as a duplicate of this issue. ***