comp.lang.ada
 help / color / mirror / Atom feed
From: magnesium.club.cc.cmu.edu!news.sei.cmu.edu!ae@uunet.uu.net  (Arthur Evans
Subject: Re: Can you help with fixed-point arithmetic?
Date: 21 Jul 93 12:29:25 GMT	[thread overview]
Message-ID: <1993Jul21.082925.3193@sei.cmu.edu> (raw)

mfeldman@seas.gwu.edu (Michael Feldman) asked about why his compiler
stated that Constraint_Error would be raised by this statement:
       27    TimeThen := TimeNow + 10 * Duration'(86400.0);

In an earlier message I explained, and I then proposed another way to
accomplish his goal of adding (in effect) days to a value of type
Calendar.Time.  Here's another approach to the latter problem.

Declare a function like this:
	function "+"(Left: in Calendar.Time; Days: in integer)
	    return Calendar.Time;
In the body, call Calendar.Split on Left, and add Days to the DAY
result.  While the result exceeds 365 (or 366 in leap years), subtract
365 (or 366) and add one to the year.  This needs some care; details are
left as an exercise for the student (or, in this case, the professor).
Then put it all back together with Calendar.Time_Of.

If you want to add both days and partial days, give this "+" a third
parameter of type Duration.  Again, detect the possible overflow and
propagate a carry into DAY.  Be careful -- there are subtleties in
detecting the overflow.  Consider
	if SECONDS + Increment > 86400.0 then
Your compiler is not incorrect if it lets Constraint_Error be raised in
evaluating the sum.  It's also not incorrect if it does the comparison
correctly, dealing with an intermediate result (the sum) outside the
range of type Duration.  In the worst case you may have to just do the
sum and detect the Constraint_Error.

If you put this function in a package My_Calendar, it can re-export the
needed parts of package Calendar, so your application would mention only
My_Calendar in a 'with' clause.

Cheers!

Art Evans
----------------------------------------------
Arthur Evans, Jr, PhD           Ada Consultant
461 Fairview Road
Pittsburgh PA  15238-1933
412-963-0839
ae@sei.cmu.edu

             reply	other threads:[~1993-07-21 12:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-07-21 12:29 magnesium.club.cc.cmu.edu!news.sei.cmu.edu!ae [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-07-21 22:19 Can you help with fixed-point arithmetic? Michael Feldman
1993-07-21 18:13 Mark A Biggar
1993-07-21 16:38 Robert I. Eachus
1993-07-21  2:03 magnesium.club.cc.cmu.edu!news.sei.cmu.edu!ae
replies disabled

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