[Issue 5900] New: std.math.radians(), std.math.degrees()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 27 11:23:56 PDT 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 at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at 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: -------


More information about the Digitalmars-d-bugs mailing list