On Thu, Mar 5, 2020 at 5:55 AM André Roberge <andre.robe...@gmail.com> wrote:
> > > On Thu, Mar 5, 2020 at 5:29 AM Steve Barnes <gadgetst...@live.co.uk> > wrote: > >> One of the lovely things about Python is that we have the capability to >> avoid issues such as the vagaries of the floating point type with libraries >> such as decimal and fractions. This is wonderous to me but comes with an >> issue that I suspect is limiting its usage. That issue is that you have to >> litter your code with constructors of those types, e.g. >> >> > SNIP >> >> >> >> Wouldn’t it be possible to have something along the lines of: >> >> >> >> ``` >> >> from decimal import TreatFloatsAsDecimal >> >> @TreatFloatsAsDecimal >> >> a = 0.1 # These are all now decimals >> >> b = 0.2 >> >> c = 0.3 >> >> a + b == c # This now works >> >> ``` >> > > It is possible, and quite straightforward, to do this as an import hook or > a custom encoding. I will try to do so and document it later today, and > post it on this list. > > > Quick **first draft**: >>> from ideas.examples import decimal_math >>> hook = decimal_math.add_hook() >>> from ideas import console >>> console.start() Configuration values for the console: source_init from ideas.examples.decimal_math transform_source from ideas.examples.decimal_math -------------------------------------------------- Ideas Console version 0.0.15. [Python version: 3.7.3] ~>> 0.1 + 0.2 == 0.3 True ~>> 0.1 * 10 == 1 True ~>> 0.1 Decimal('0.1') ~>> 0.1 + 0.100 Decimal('0.200') Documentation at https://aroberge.github.io/ideas/docs/html/decimal_math.html (This took longer to write than the actual code.) André Roberge > > >> >> >> I do know that this goes against the explicit is better than implicit so >> an alternative might be to have a flexible base modifier, something like: >> >> ``` >> >> from decimal import DecimalBase as D >> >> >> >> a = 0D0.1 # These are all now decimals >> >> b = 0D0.2 >> >> c = 0D0.3 >> >> a + b == c # This now works >> >> ``` >> >> >> >> Since anything like this would require overriding at least part of the >> base interpreter I do not think that this is a suitable one for an external >> PyPi library or at least some hooks in the interpreter would be required to >> make it possible. >> >> >> >> Any thoughts, feedback, interest, expressions of horror? Or is this >> already there and I have missed it? >> >> >> >> Steve Barnes >> >> >> >> >> _______________________________________________ >> 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/7EF5MOJK5GOQZZEZXQ7DKM2N52JZ7VNB/ >> Code of Conduct: http://python.org/psf/codeofconduct/ >> >
_______________________________________________ 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/TQ7LUADZHKKOVRAHZTHM2BEETBBZH3JQ/ Code of Conduct: http://python.org/psf/codeofconduct/