[Python-ideas] Trigonometry in degrees
Jacco van Dorp
j.van.dorp at deonet.nl
Fri Jun 8 03:05:45 EDT 2018
Or when students get stuff e-17 out of a function, you teach them what
floating point numbers are and what gotcha's they can expect. The
simple version is "value is stored as a float, and a float gets
rounding errors below e-16", or for the more inquisitive minds you
give them nice places like
https://docs.python.org/3/tutorial/floatingpoint.html .
If they're really going to use what they learn, they're going to run
into it sooner or later. So having a bit of base knowledge about
floats is a lot more useful than having to google "why does sin()
return weird values python". At the very least, they'll isntead google
"float limitations", which is going to get them a lot closer to the
real information a lot faster.
That said, I wouldn't be that opposed to a dedicated type to remember
things about pi. Lets say....
class pi(Numeric):
"""Represents numbers that represent some function of pi"""
def __init__(self, mul=1):
self.multiplier = mul
def __mul__(self, other):
if isinstance(other, Numeric):
return self.__class__(self.multiplier*other)
(similar with the other special methods)
(Please consider the idea, not the exact code. I dont even know if
i spelled the numeric superclass right. Let alone making this type
hashable and immutable, which it should be.)
It's probably not a good idea to use that for performance-critical
parts, but for the more trivial applications, it could allow for more
clarity. Also, in a lot of common angles, it'd be far easier to
actually recognize special cases. you could also make it __repr__ like
f"Pi*{self.multiplier}", so you get a neat exact answer if you print
it..
More information about the Python-ideas
mailing list