On Thu, 7 Apr 2022 at 07:22, Oscar Benjamin <oscar.j.benja...@gmail.com> wrote: > > 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?
Teaching IS a use-case with Python. Does it actually need binary rather than decimal floating point? Why should I fetch a third-party library rather than use what exists? > > 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 That's fair, and always a good place to start, but if you want more precision than that, where do you go? > 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. Exactly. It's very easy to do demos and exploration that require nothing more than the core language and standard library. No need to walk people through the use of pip, running into platform differences, etc. The Decimal module is great for that. What do the specialized libraries offer that Decimal doesn't? Is it really worth all that hassle for the sake of teaching something that, in production, would just be done with floats and the math module anyway? ChrisA _______________________________________________ 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/DOPZ2RZJJHHJKEBKZQG6UJPUYD2FSGEI/ Code of Conduct: http://python.org/psf/codeofconduct/