comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Ada.Real_Time.Time_First
Date: Wed, 9 Dec 2020 14:16:10 +0100	[thread overview]
Message-ID: <rqqimn$1fre$1@gioia.aioe.org> (raw)
In-Reply-To: lyv9dbduvf.fsf@pushface.org

On 2020-12-09 13:30, Simon Wright wrote:
> I opened an issue[1] on Cortex GNAT RTS, saying
> 
>     You’d expect Ada.Real_Time.Time_First to be quite a long time before
>     any possible value of Ada.Real_Time.Clock; but in fact the system
>     starts with Clock equal to Time_First.
> 
> On the other hand,
> 
> I had written
> 
>     Last_Flight_Command_Time : Ada.Real_Time.Time
>       := Ada.Real_Time.Time_First;
> 
>     ...
>     
>     Quad_Is_Flying :=
>       Ada.Real_Time.To_Duration (Now - Last_Flight_Command_Time)
>         < In_Flight_Time_Threshold;
> 
> but Now - Last_Flight_Command_Time is going to be quite small, to start
> with, so Quad_Is_Flying is going to be True when it shouldn't be.
> 
> The workround I used was
> 
>     Quad_Is_Flying :=
>       Last_Flight_Command_Time /= Ada.Real_Time.Time_First
>         and then
>       Ada.Real_Time.To_Duration (Now - Last_Flight_Command_Time)
>         < In_Flight_Time_Threshold;
> 
> In other words, I was using Time_First as a flag to indicate that
> Last_Flight_Command_Time was invalid.
> 
> What would your standard pattern for this sort of problem be?
> Esepecially considering that if I make Time_First a large negative
> number I'll get the opposite problem, e.g. predicting ahead for a very
> large interval, possibly even leading to numeric overflows.

I would use Next_Time instead of Last_Time:

    Next_Flight_Command_Time : Time := Time_First;
begin
    loop
       Now := Clock;
       if Now >= Next_Flight_Command_Time then
          Fire_All_Rockets;
          Next_Flight_Command_Time :=
             Next_Flight_Command_Time + In_Flight_Time_Threshold;
       end if;
    end loop;
exception
    when Constraint_Error => -- the End of Times!
       Put_Line ("Thank you for you cooperation!");
       Fire_Death_Star;
       Self_Destroy;
end;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2020-12-09 13:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-09 12:30 Ada.Real_Time.Time_First Simon Wright
2020-12-09 13:16 ` Dmitry A. Kazakov [this message]
2020-12-09 20:07   ` Ada.Real_Time.Time_First Simon Wright
2020-12-09 14:21 ` Ada.Real_Time.Time_First Niklas Holsti
2020-12-09 20:16   ` Ada.Real_Time.Time_First Simon Wright
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox