## digitalmars.D.bugs - [Issue 5900] New: std.math.radians(), std.math.degrees()

d-bugmail puremagic.com writes:
```http://d.puremagic.com/issues/show_bug.cgi?id=5900

Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody puremagic.com
ReportedBy: bearophile_hugs eml.cc

--- Comment #0 from bearophile_hugs eml.cc 2011-04-27 11:20:10 PDT ---
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;

double d = 25.2;

float f = 25.2;

int i = 25;

int c = 'f';

creal cr = 25.2 + 0i;

cdouble cd = 25.2 + 0i;

cfloat cf = 25.2 + 0i;

// more runtime tests needed
}

--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
```
Apr 27 2011
d-bugmail puremagic.com writes:
```http://d.puremagic.com/issues/show_bug.cgi?id=5900

Walter Bright <bugzilla digitalmars.com> changed:

----------------------------------------------------------------------------
Status|NEW                         |RESOLVED
CC|                            |bugzilla digitalmars.com
Resolution|                            |WONTFIX

--- Comment #1 from Walter Bright <bugzilla digitalmars.com> 2011-04-27
16:33:44 PDT ---

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

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.

--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
```
Apr 27 2011
d-bugmail puremagic.com writes:
```http://d.puremagic.com/issues/show_bug.cgi?id=5900

Don <clugdbug yahoo.com.au> changed:

----------------------------------------------------------------------------
CC|                            |clugdbug yahoo.com.au

--- Comment #2 from Don <clugdbug yahoo.com.au> 2011-04-28 00:31:53 PDT ---
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:
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.

--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
```
Apr 28 2011
d-bugmail puremagic.com writes:
```http://d.puremagic.com/issues/show_bug.cgi?id=5900

kennytm gmail.com changed:

----------------------------------------------------------------------------
CC|                            |kennytm gmail.com

--- Comment #3 from kennytm gmail.com 2011-04-28 01:47:32 PDT ---
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:
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 );
}

--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
```
Apr 28 2011
d-bugmail puremagic.com writes:
```http://d.puremagic.com/issues/show_bug.cgi?id=5900

Walter Bright <bugzilla digitalmars.com> changed: