On Wed, 6 Apr 2022 at 21:47, Chris Angelico <ros...@gmail.com> wrote: > > On Thu, 7 Apr 2022 at 05:37, Oscar Benjamin <oscar.j.benja...@gmail.com> > wrote: > > > > > > Not so obvious to me, as it would require inordinate amounts of > > > fiddling around when you want to dynamically adjust your precision. > > > You'd have to reassign every Decimal instance to have the new > > > settings. > > > > Why do you need to "dynamically adjust your precision"? > > Some algorithms work just fine when you start with X digits of > precision and then increase that to X+Y digits later on (simple > example: Newton's method for calculating square roots).
This is why I followed up by saying that the decimal module should not really be used in place of a scientific multiprecision library. Calculating irrational square roots is precisely the kind of thing that decimal floating point is not needed for. Do you know of a real example where this pattern is used in a situation that actually needs decimal (rather than binary) floating point? > > If you're otherwise using the decimal module in place of a scientific > > multiprecision library then I think it's not really the right tool for > > the job. It's unfortunate that the docs suggest making functions like > > sin and cos: there is no good reason to use decimal over binary for > > transcendental or irrational numbers or functions. > > Suppose you want to teach people how sin and cos are calculated. What > would YOU recommend? Python already comes with an arbitrary-precision > numeric data type. Do we need to use something else? I would teach this using ordinary floats in the first instance since that's how cos is calculated most of the time e.g. that's what the math module does (and numpy etc): In [31]: x = 0.5 In [32]: sum((-1)**(n//2)*x**n/factorial(n) for n in range(0, 20, 2)) Out[32]: 0.8775825618903728 In [33]: cos(x) Out[33]: 0.8775825618903728 Certainly I have used the decimal module to demonstrate precisely the example you gave above (computing sqrt(2) with Newton's method) to show the idea that we can compute arbitrarily accurate results. That's not really what the decimal module is for though and I could have just as easily used something else (gmpy2/mpmath/sympy etc). The only advantage of the decimal module in that situation is just that it happens to be in the stdlib so I can demonstrate the code and hope that others can easily reproduce it. -- Oscar _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/SE7A2BFGSGU5BR43VPJQVS26NVPMLJRZ/ Code of Conduct: http://python.org/psf/codeofconduct/