From: tmoran@acm.org
Subject: Re: Puzzling small piece of code - exception block essentially used as goto
Date: Mon, 04 Jun 2007 23:10:59 -0500
Date: 2007-06-04T23:10:59-05:00 [thread overview]
Message-ID: <KsadnSZO7rXOf_nbnZ2dnUVZ_t6qnZ2d@comcast.com> (raw)
In-Reply-To: Q049i.5409$nj4.313@trndny06
> > Without knowing the details of the types of the variables used, I
> > can't say.
>
> Here are the ranges for the variables (all are integers):
>
> Hour_Delta: -9999*60..9999*60
> New_Hour: 0..2^32
> Hour: 0..2^32
>
> Relative_Time : constant := 0;
And what is the value of Max_Hour_Delta?
Another way to look at that code is to observe that the only way out of
the loop is to not raise Constraint_Error, ie, New_Hour is within
+-9999*60 of Hour (that *60 rather suggests the units are minutes, not
hours as one might assume fron the names). If that's not true at first,
the code adjusts Hour by steps of Max_Delta_Hour, hopefully in the
direction of New_Hour. If it doesn't overshoot, Hour will eventually be
in range, at which time it will be set to the value of New_Hour. So the
code essentially says:
if Max_Delta_Hour < 0 then
burn some CPU time; -- move Hour ever farther from New_Hour
raise Constraint_Error; -- till it hits a limit
elsif Max_Delta_Hour = 0 then
sit forever in infinite loop, never changing Hour;
elsif Max_Delta_Hour in > 2 * 9999*60
+ 1
+ ( abs(New_Hour-Hour) mod (9999*60+1) ) then
oscillate forever between Hour too positive and Hour too negative;
else
burn some CPU time;
end if;
Hour := New_Hour;
(if I did my algebra correctly).
So replacing the whole thing by "Hour := New_Hour;" probably does
what the original programmer wanted.
next prev parent reply other threads:[~2007-06-05 4:10 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-06-02 5:01 Puzzling small piece of code - exception block essentially used as goto Anonymous Coward
2007-06-02 5:48 ` Jeffrey R. Carter
2007-06-05 2:38 ` Anonymous Coward
2007-06-05 4:10 ` tmoran [this message]
2007-06-05 4:53 ` tmoran
2007-06-02 6:16 ` tmoran
2007-06-05 0:08 ` Adam Beneschan
2007-06-05 7:58 ` anon
2007-06-05 10:40 ` Markus E Leypold
2007-06-05 23:43 ` Anonymous Coward
2007-06-06 10:39 ` Markus E Leypold
2007-06-08 0:55 ` Anonymous Coward
2007-06-08 2:22 ` Markus E Leypold
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox