support for various angular units in std.math

Simen kjaeraas simen.kjaras at gmail.com
Sun Feb 27 22:04:15 PST 2011


Colin Wallace <wallacoloo at gmail.com> wrote:

> I've been doing a lot of opengl work in D. Opengl deals primarily
> (maybe even completely?) with degrees.
>
> When using trigonometric functions, they all deal with radians. So
> I've been having to do lots of converting. Converting is very
> straightforward, but I still think it would be nice if there were some
> built in functions for dealing with other units than radians. I think
> a system that looked like what is used for the Duration struct in
> core.time would make things more readable. The code for taking the
> sine of a number measured in degrees could look like:
> sin!"degrees"(45)
> rather than
> sin(45*PI/180)
>
> It doesn't make a huge difference to me, but neither does the dur!()
> function, yet somebody decided it would be helpful and it made its way
> into the standard library. So I figured I would at least share this
> idea to see what other people thought of it.

I think a better solution would be a proper units struct:

unit!"degrees" a;

sin(a); // Automagically behaves correctly

bool foo(unit!"radians" bar) {
    return bar < PI;
}

foo(a); // Compile-time error: degrees not equal to radians.

-- 
Simen


More information about the Digitalmars-d mailing list