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.
>
>
>
> ```
>
> a = 0.1
>
> b = 0.2
>
> c = 0.3
>
> a + b == c # This doesn’t work but it is easy to forget why
>
> ```
>
> vs:
>
> ```
>
> from decimal import *  # I know why this is bad but it comes straight from
> the examples
>
> a = Decimal("0.1")  # Needs to be a string to avoid being temporarily a
> float
>
> b = Decimal("0.2")  # Ditto
>
> c = Decimal("0.3")  # Ditto
>
> a + b == c  # Magic this works!
>
> ```
>
>
>
> While this works it is not very friendly to users and there will often be
> cases where people forget to quote the values, etc. and there are similar
> issues with using the fractions library and I am sure with others. I
> suspect that this is why the decimals library, and some others, is not used
> as widely as it possibly should be.
>
>
>
> 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.



> Less obviously this sort of approach could also apply to making all
> integers into Fractions or other base types into something else.
>
Also a bit tricky since you don't want to end up with

for i in range(Fraction(4)) ...

However, have a look at
https://aroberge.github.io/ideas/docs/html/fractional_math_tok.html

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/NYSEKOXRZVASYXG6G64OHPJKWWNZESWK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to