Short-circuit Logic

Ian Kelly ian.g.kelly at gmail.com
Thu May 30 15:49:43 EDT 2013


On Thu, May 30, 2013 at 1:30 PM, Neil Cerutti <neilc at norwich.edu> wrote:
> On 2013-05-30, Chris Angelico <rosuav at gmail.com> wrote:
>> On Thu, May 30, 2013 at 3:10 PM, Steven D'Aprano
>><steve+comp.lang.python at pearwood.info> wrote:
>>> # Wrong, don't do this!
>>> x = 0.1
>>> while x != 17.3:
>>>     print(x)
>>>     x += 0.1
>>
>> Actually, I wouldn't do that with integers either.
>
> I propose borrowing the concept of significant digits from the
> world of Physics.
>
> The above has at least three significant digits. With that scheme
> x would approximately equal 17.3 when 17.25 <= x < 17.35.
>
> But I don't see immediately how to calculate 17.25 and 17.35 from
> 17.3, 00.1 and 3 significant digits.

How about this:

while round(x, 1) != round(17.3, 1):
    pass

The second round call may be unnecessary.  I would expect the parser
to ensure that round(17.3, 1) == 17.3, but I'm not certain that is the
case.



More information about the Python-list mailing list