[Python-ideas] Trigonometry in degrees

Robert Vanden Eynde robertvandeneynde at hotmail.com
Thu Jun 7 08:33:29 EDT 2018


I suggest adding degrees version of the trigonometric functions in the math module.

- Useful in Teaching and replacing calculators by python, importing something is seen by the young students much more easy than to define a function.

- Special values could be treated, aka when the angle is a multiple of 90, young students are often surprise to see that cos(pi/2) != 0

Testing for a special value Isn't very costly (x % 90 == 0) but it could be pointed out that there is a small overhead using the "degrees" equivalent of trig function because of the radians to degrees conversion And the special values testing.

- Standard names will be chosen so that everyone will use the same name convention. I suggest adding a "d" like sind, cosd, tand, acosd, asind, atand, atan2d.

Another option would be to add "deg" or prepend "d" or "deg" however the name should be short.

sind, dsin, sindeg or degsin ?

We can look in other languages what they chose.

Creating a new package like 'from math.degrees import cos' however I would not recommend that because "cos" in the source code would mean to lookup the import to know if it's in degrees or radians (and that leads to very filthy bugs). Also "degrees" is already so the name would have to change the name of the package.

- Also in the cmath module. Even though the radians make more sense in the complex plane. The same functions sin cos tan, asin acos atan, alongside with phase and polar.

Here's my current implementation :

def cosd(x):
    if x % 90 == 0:
        return (1, 0, -1, 0)[int(x // 90) % 4]
    else:
        return cos(radians(x))

def sind(x):
    if x % 90 == 0:
        return (0, 1, 0, -1)[int(x // 90) % 4]
    else:
        return sin(radians(x))

def tand(x):
    if x % 90 == 0:
        return (0, float('inf'), 0, float('-inf'))[int(x // 90) % 4]
    else:
        return tan(radians(x))

The infinity being positive of negative is debatable however, here I've chosen the convention lim tan(x) as x approaches ±90° from 0

def acosd(x):
    if x == 1: return 0
    if x == 0: return 90
    if x == -1: return 180
    return degrees(acos(x))

def asind(x):
    if x == 1: return 90
    if x == 0: return 0
    if x == -1: return -90
    return degrees(asin(x))

However, currently [degrees(acos(x)) for x in (1,0,-1)] == [0, 90, 180] on my machine so maybe the test isn't necessary.

Testing for Special values of abs(x) == 0.5 could be an idea but I don't think the overhead is worth the effort.

Probably this has already been discussed but I don't know how to check that.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180607/946dce6d/attachment.html>


More information about the Python-ideas mailing list