开发者

How to determine the modulus of a Float in Ada 95

I need to determine the amount left of a time cycle. To do that in C I would use fmod. But in ada I can find no reference to a similar function. It needs to be accurate and it needs to return a float for precision.

开发者_如何学JAVA

So how do I determine the modulus of a Float in Ada 95?

 elapsed := time_taken mod 10.348;
 left := 10.348 - elapsed;
 delay Duration(left);


Use the floating point 'Remainder attribute.

Elapsed, Time_Taken : Float;

...

Elapsed := Float'Remainder(Time_Taken, 10.348);


Not an answer to your actual question; but, to achieve the intention of that piece of code, consider using delay until.

   Next_Time : Ada.Calendar.Time;
   use type Ada.Calendar.Time;
   Period : constant Duration := 10.348;
begin
   ...
   Next_Time := Ada.Calendar.Clock;
   loop
      -- do stuff
      Next_Time := Next_Time + Period;
      delay until Next_Time;
   end loop;


I don't know Ada, but assuming it has a Floor function you could use elapsed := time_taken - Floor(time_taken / 10.348) * 10.348).

Edit: I also just found this discussion on using the Remainder attribute for this purpose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜