Code style query: multiple assignments in if/elif tree

Ian Kelly ian.g.kelly at gmail.com
Tue Apr 1 04:18:26 EDT 2014


On Tue, Apr 1, 2014 at 1:35 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Tue, Apr 1, 2014 at 6:20 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> 177.211111333333
>>
>> compared to 177.26527800000002 calculated the rough way. That's not bad,
>> only about 5cm off! Effectively, your rough calculation was accurate to
>> one decimal place.
>
> 5cm when we're dealing with track distances measured in meters and
> usually hundreds of them? Sounds fine to me!

Erm, that's just the accumulated error over the first 2 seconds,
though.  A train moving at 88 m/s and decelerating at a maximum of
0.85 m/s^2 is not going to stop in just 177 meters.

After the first 2 seconds, the model is the same using either method,
but the velocity after 2 seconds is different.  Using the linear
deceleration integration, we get:

a(t) = {-0.425t, t <= 2;
        -0.85, t > 2}
v(t) = {v0 - 0.2125t^2, t <= 2;
        v0 - 0.85 - 0.85t, t > 2}

Giving v(2s) = v0 - 0.85 m/s

Using the averaging method, we get v(2s) = v0 - 0.2 - 0.425 = v0 - 0.625 m/s

For a difference of 0.225 m/s.  Using Steven's formula (4), the
additional difference in stopping distance is thus:

Δs = ((0 - (88.888889 - 0.625)^2) - (0 - (88.888889 - 0.85)^2)) / -1.7
     = 23.33 m

The reason the velocity is different after 2 seconds is because the
linear deceleration does not match the constraints of the problem. The
average deceleration for the first second is not 0.2 m/s, and the
average deceleration for the second second is not 0.425 m/s.



More information about the Python-list mailing list