tags:

views:

192

answers:

3

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.

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);
+1  A: 

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.

Tim Goodman
Also note that I've assumed `time_taken` isn't going to be negative.
Tim Goodman
See also http://www.adaic.com/standards/95aarm/html/AA-A-5-3.html
trashgod
Ada has the floor Attribute; i.e Float'Floor.e.g. Float'Floor(10.5) = 10.0
mat_geek
+3  A: 

Use the floating point 'Remainder attribute.

Elapsed, Time_Taken : Float;

...

Elapsed := Float'Remainder(Time_Taken, 10.348);
Marc C
Be warned that Remainder will return negative numbers.if Elapsed < 0 then Elapsed := 10.384 - Elapsed;end if;
mat_geek
+2  A: 

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;
Simon Wright