Thread overview
[Issue 5900] New: std.math.radians(), std.math.degrees()
Apr 27, 2011
Walter Bright
Apr 28, 2011
Don
Apr 28, 2011
kennytm@gmail.com
Oct 29, 2011
Walter Bright
April 27, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5900

           Summary: std.math.radians(), std.math.degrees()
           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;
    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
}

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


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |WONTFIX


--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> 2011-04-27 16:33:44 PDT ---
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.

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


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 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:
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.

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


kennytm@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm@gmail.com


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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5900


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2011-10-29 13:34:29 PDT ---
*** Issue 6862 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------