Unexpected results comparing float to Fraction
Ian Kelly
ian.g.kelly at gmail.com
Mon Jul 29 19:35:18 CEST 2013
On Mon, Jul 29, 2013 at 11:04 AM, MRAB <python at mrabarnett.plus.com> wrote:
> On 29/07/2013 17:40, Ian Kelly wrote:
>> At the point where the float is exactly equal to the value you get
>> from the floating-point division 1/3. If it's some other float then
>> the user didn't get there by entering 1/3, so it's not worth trying to
>> pretend that they did.
>>
> I thought that you're not meant to check for equality when using floats.
Equality checking is useful for floats when there is exactly one value
that you want to test for as in this case, as opposed to a range of
approximate values that are considered to be equal within rounding
error. There is exactly one float that the expression 0.1 evaluates
to, and although it's not equal to the decimal 0.1, it is the only
float that we want to format as "0.1". The immediately preceding and
following floats are 0.09999999999999999 and 0.10000000000000002, and
they are formatted as such because they're not equal to the float that
you get from 0.1.
More information about the Python-list
mailing list