[Python-ideas] Settable defaulting to decimal instead of float

Chris Angelico rosuav at gmail.com
Thu Jan 12 08:25:54 EST 2017


On Thu, Jan 12, 2017 at 11:50 PM, Stephan Houben <stephanh42 at gmail.com> wrote:
> 2017-01-12 13:17 GMT+01:00 Chris Angelico <rosuav at gmail.com>:
>>
>> Most of the time one of my students talks to me about decimal vs
>> binary, they're thinking that a decimal literal (or converting the
>> default non-integer literal to be decimal) is a panacea to the "0.1 +
>> 0.2 != 0.3" problem.
>
>
> Indeed. Decimal also doesn't solve the
> 1/3
> issue.
>
> I don't understand why people always talk about Decimal,
> if you want math to work "right" you probably want fractions.
>
> (With the understanding that this is for still limited value of "right".)

My usual go-to response is that if you want perfect arithmetic with no
rounding errors, your *ONLY* option is symbolic math, where sqrt(8)
returns 2√2. It's pretty obvious that this gets unwieldy really fast,
and rounding errors are a fact of life :) Rationals have their own
problems (eg it's nearly impossible to eyeball them for size once they
get big), and still don't solve everything else.

>> Perhaps the real solution is a written-up
>> explanation of why binary floating point is actually a good thing, and
>> not just a backward-compatibility requirement?
>
>
> I have sometimes considered writing up "Why the aliens of Epsilon Eridani,
> whose computers use 13-valued logic, still use floating point numbers
> with base 2."
>
> (Short overview: analysis form first principles shows that the base should
> be:
> 1. an integral number > 1 and
> 2. as small as possible (to minmax the relative rounding error))
>
> The list of candidate bases satisfying these criteria is: 2.
>

That's exactly the sort of thing I'm talking about. Among other
things, only binary floating point guarantees that x <= (x+y)/2 <= y
for any x <= y. (At least, I think only binary - I know decimal can't
ensure that, and I haven't tested everything in between.) You're way
more an expert on this than I am - my skill consists of reading what
other people have written and echoing it to people :)

ChrisA


More information about the Python-ideas mailing list