Jean Abou Samra <[email protected]> added the comment:
I would argue that given a function,
from math import *
def naive_calc_pi(n=100):
u = sqrt(8)
v = 4
for _ in range(n):
v = 2*u*v/(u + v)
u = sqrt(u*v)
return u
when you realize that floats have limited precision (happened to me several
times while being taught and teaching Python), you could just replace
from math import *
with
from decimal import *
and use it, instead of the current
from decimal import Decimal as D
def sqrt(x):
return D(x).sqrt()
Of course, you can define these, but as I repeatedly ended up doing this, I
just thought I'd bring the idea upstream.
Another, perhaps more important argument is readability. We all think in terms
of functions: log(x), not x.log(). I find it a significant fact that NumPy has
both numpy.dot(A, B) and numpy.ndarray.dot(self, B), but the former is
generally preferred (the method's documentation points to the function and the
first dozen Google search results for "numpy dot product" yield the function).
It makes expressions resemble what we are used to: compare
(a + b).tan().log() = (a.tan() + b.tan()).sqrt()/(1 - a.tan()*b.tan()).sqrt()
with
sqrt(tan(a + b)) = sqrt(tan(a) + tan(b))/sqrt(1 - tan(a)*tan(b))
> Also, the APIs aren't completely harmonious because the Decimal methods
> accept an optional context object.
I don't see a problem with the functions also accepting this parameter.
(np.sin() has many parameters after all.)
Overall, I think this modest addition would bring an improvement in the
usability of the decimal module. I can make a PR.
----------
nosy: +Jean_Abou_Samra
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue41315>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com