I think we need to be careful here. This whole idea is predicated an assumption that Decimals are inherently ”better” or more accurate than binary floats.
They are not. The only real advantage is that they fit most people’s mental model better. But that’s actually a bit dangerous, as it makes the issues subtler. I’m not convinced it’s even better for money — if you operate in the lowest unit (cents in the US) then there isn’t anything decimal about it, and different accounting systems require different rounding rules, and even different fractional representations, such as the NY stock exchange: https://www.nytimes.com/1971/02/28/archives/stocks-why-fractions-eighths-a-plague-for-both-man-and-computer.html Which is actually better suited to binary math :-) In short, this assumption that making it easier to use Decimal is an obvious good should be questioned. In many cases on this list, it’s said that we’d do things differently if we were starting from scratch. I’m not sure that’s the case here. Where the Python Decimal type IS advantageous is with controlling the precision and the like. But in that case, users need to know what they are doing, so using the Decimal constructor explicitly is a good thing. That being said: maybe a decimal literal would help a bit: 1.1D Is currently a syntax error. It seems something like that is the obvious choice. Though I imagine it’s been rejected already for a reason. Final note: in a recent discussion about the JSON module, there was reluctance to have it fully represent Decimal. If it’s too much overhead for the JSON module, it’s surely too much overhead for the core interpreter! -CHB On Thu, Mar 5, 2020 at 8:05 AM André Roberge <andre.robe...@gmail.com> wrote: > > > 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/ > -- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
_______________________________________________ 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/RE62PP5DVIOAVGBRYBWHWX5NQJAVSMRH/ Code of Conduct: http://python.org/psf/codeofconduct/