comp.lang.ada
 help / color / mirror / Atom feed
* Ok to assume type Duration is for more than one day?
@ 2017-01-22 11:37 reinkor
  2017-01-22 12:28 ` Jeffrey R. Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: reinkor @ 2017-01-22 11:37 UTC (permalink / raw)


As I understand from my textbook, type Duration is guaranteed to cover the range -86_400 .. 86_400 (number of seconds for one day). Does this mean that I take a considerable risk if I assume "Duration" can represent more than one day?

The following program:

----------------------------------------------------------------
with Ada.Numerics.Generic_Elementary_Functions,Text_IO;
use Text_IO;
with Ada.Calendar; use Ada.Calendar;

procedure d1 is

   package Flt_Io is new Text_IO.Float_Io   (Float);
   package Int_Io is new Text_IO.Integer_Io (Integer);
   package E_F is new Ada.Numerics.Generic_Elementary_Functions (Float);
   use E_F,Flt_Io,Int_Io;

   d : Duration := Duration(3600*24*10);

begin

   Put(" Duration'Last : ");
   Put(Float(Duration'Last)/(3600.0*24.0*365.0),20,1,0);

end d1;
--------------------------------------------------------------

gives (gnat/debian and also Raspberry Pi (Raspbian)):

Duration'Last :                  292.5

(i.e. many years....).

reinert

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ok to assume type Duration is for more than one day?
  2017-01-22 11:37 Ok to assume type Duration is for more than one day? reinkor
@ 2017-01-22 12:28 ` Jeffrey R. Carter
  2017-01-23 20:47   ` Randy Brukardt
  2017-01-22 16:39 ` AdaMagica
  2017-01-23  8:35 ` G.B.
  2 siblings, 1 reply; 9+ messages in thread
From: Jeffrey R. Carter @ 2017-01-22 12:28 UTC (permalink / raw)


On 01/22/2017 12:37 PM, reinkor wrote:
> As I understand from my textbook, type Duration is guaranteed to cover the range -86_400 .. 86_400 (number of seconds for one day). Does this mean that I take a considerable risk if I assume "Duration" can represent more than one day?

Representing that range with an accuracy of 20 ms takes about 22 bits, so most 
implementations will use at least 32 bits. The additional bits can be used for a 
greater range, a smaller accuracy, or both. ±86,400 with an accuracy of 20 µs 
takes about 32 bits, so a range of ±86,400 seems likely for some 32-bit compilers.

> gives (gnat/debian and also Raspberry Pi (Raspbian)):
>
> Duration'Last :                  292.5
>
> (i.e. many years....).

GNAT clearly uses more than 32 bits (a look at the GNAT documentation indicates 
64 bits with an accuracy of 1 ns). If you're confident you'll never use another 
compiler, then go ahead (you might be surprised how many such systems I've seen 
ported to another compiler). If you want your code to be portable, then you'll 
limit yourself to ±86,400.

-- 
Jeff Carter
"I didn't squawk about the steak, dear. I
merely said I didn't see that old horse
that used to be tethered outside here."
Never Give a Sucker an Even Break
103


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ok to assume type Duration is for more than one day?
  2017-01-22 11:37 Ok to assume type Duration is for more than one day? reinkor
  2017-01-22 12:28 ` Jeffrey R. Carter
@ 2017-01-22 16:39 ` AdaMagica
  2017-01-23  8:35 ` G.B.
  2 siblings, 0 replies; 9+ messages in thread
From: AdaMagica @ 2017-01-22 16:39 UTC (permalink / raw)


A.1(43) Duration is delta implementation-defined range implementation-defined;

1.1.3(18) Certain aspects of the semantics are defined to be either implementation defined or unspecified. In such cases, the set of possible effects is specified, and the implementation may choose any effect in the set.
Implementations shall document their behavior in implementation-defined situations, but documentation is not required for unspecified situations. The implementation-defined characteristics are summarized in M.2.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ok to assume type Duration is for more than one day?
  2017-01-22 11:37 Ok to assume type Duration is for more than one day? reinkor
  2017-01-22 12:28 ` Jeffrey R. Carter
  2017-01-22 16:39 ` AdaMagica
@ 2017-01-23  8:35 ` G.B.
  2017-01-23  8:43   ` G.B.
  2017-01-23 13:32   ` Björn Lundin
  2 siblings, 2 replies; 9+ messages in thread
From: G.B. @ 2017-01-23  8:35 UTC (permalink / raw)


On 22.01.17 12:37, reinkor wrote:
> Does this mean that I take a considerable risk if I assume "Duration" can represent more than one day?

Assess the risk using a compile time check. For example,

     Seconds_in_Day : constant := 24*60*60;
     Minimum_Duration : constant := 100.0*Seconds_In_Day;

     procedure Check_Duration_More_Than_One_Day is
     begin
         case Standard.Boolean'(True) is
            when Duration'Last < Minimum_Duration => null;
            when True => null;
         end case;
     end Check_Duration_More_Than_One_Day;

Your implementation of Ada will reject the program at compile time
if
   Duration'Last < Minimum_Duration
is True because choice values must not occur more than once
in a case statement.

-- 
"HOTDOGS ARE NOT BOOKMARKS"
Springfield Elementary teaching staff

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ok to assume type Duration is for more than one day?
  2017-01-23  8:35 ` G.B.
@ 2017-01-23  8:43   ` G.B.
  2017-01-23 13:32   ` Björn Lundin
  1 sibling, 0 replies; 9+ messages in thread
From: G.B. @ 2017-01-23  8:43 UTC (permalink / raw)


On 23.01.17 09:35, G.B. wrote:
>         case Standard.Boolean'(True) is
>            when Duration'Last < Minimum_Duration => null;
>            when True => null;
>         end case;

Standard.True instead when meaning true...

-- 
"HOTDOGS ARE NOT BOOKMARKS"
Springfield Elementary teaching staff


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ok to assume type Duration is for more than one day?
  2017-01-23  8:35 ` G.B.
  2017-01-23  8:43   ` G.B.
@ 2017-01-23 13:32   ` Björn Lundin
  2017-01-23 13:53     ` G.B.
  1 sibling, 1 reply; 9+ messages in thread
From: Björn Lundin @ 2017-01-23 13:32 UTC (permalink / raw)


On 2017-01-23 09:35, G.B. wrote:
> On 22.01.17 12:37, reinkor wrote:
>> Does this mean that I take a considerable risk if I assume "Duration"
>> can represent more than one day?
> 
> Assess the risk using a compile time check. For example,
> 
>     Seconds_in_Day : constant := 24*60*60;
>     Minimum_Duration : constant := 100.0*Seconds_In_Day;
> 
>     procedure Check_Duration_More_Than_One_Day is
>     begin
>         case Standard.Boolean'(True) is
>            when Duration'Last < Minimum_Duration => null;
>            when True => null;
>         end case;
>     end Check_Duration_More_Than_One_Day;
> 
> Your implementation of Ada will reject the program at compile time
> if
>   Duration'Last < Minimum_Duration
> is True because choice values must not occur more than once
> in a case statement.
> 

or something like
 pragma Compile_Time_Error(Duration'Last > 100 * 86_400.0, "To large");

if Duration'Last > 100 * 86_400.0 you get a compile error
with the string "To large"
-- 
--
Björn


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ok to assume type Duration is for more than one day?
  2017-01-23 13:32   ` Björn Lundin
@ 2017-01-23 13:53     ` G.B.
  2017-01-23 14:32       ` Björn Lundin
  0 siblings, 1 reply; 9+ messages in thread
From: G.B. @ 2017-01-23 13:53 UTC (permalink / raw)


On 23.01.17 14:32, Björn Lundin wrote:
> or something like
>  pragma Compile_Time_Error(Duration'Last > 100 * 86_400.0, "To large");
>
> if Duration'Last > 100 * 86_400.0 you get a compile error
> with the string "To large"

Is this  available with compilers other than GNAT?

Could it be made standard?
Given that Ada requires certain expressions to be
compile time expressions, they could be referred to
when defining minimum support for a standard pragma.

-- 
"HOTDOGS ARE NOT BOOKMARKS"
Springfield Elementary teaching staff

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ok to assume type Duration is for more than one day?
  2017-01-23 13:53     ` G.B.
@ 2017-01-23 14:32       ` Björn Lundin
  0 siblings, 0 replies; 9+ messages in thread
From: Björn Lundin @ 2017-01-23 14:32 UTC (permalink / raw)


On 2017-01-23 14:53, G.B. wrote:
> On 23.01.17 14:32, Björn Lundin wrote:
>> or something like
>>  pragma Compile_Time_Error(Duration'Last > 100 * 86_400.0, "To large");
>>
>> if Duration'Last > 100 * 86_400.0 you get a compile error
>> with the string "To large"
> 
> Is this  available with compilers other than GNAT?

hmm, I see that it is listed under
<https://gcc.gnu.org/onlinedocs/gnat_rm/Implementation-Defined-Pragmas.html#Implementation-Defined-Pragmas>
so I guess not.

> Could it be made standard?

That is something for compiler implementors to answer.
I don't use this one a lot, but I do use its sibling
Compile_Time_Warning quite a bit.



-- 
--
Björn

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Ok to assume type Duration is for more than one day?
  2017-01-22 12:28 ` Jeffrey R. Carter
@ 2017-01-23 20:47   ` Randy Brukardt
  0 siblings, 0 replies; 9+ messages in thread
From: Randy Brukardt @ 2017-01-23 20:47 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1113 bytes --]

Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message 
news:o628eh$2vi$1@dont-email.me...
> On 01/22/2017 12:37 PM, reinkor wrote:
>> As I understand from my textbook, type Duration is guaranteed to cover 
>> the range -86_400 .. 86_400 (number of seconds for one day). Does this 
>> mean that I take a considerable risk if I assume "Duration" can represent 
>> more than one day?
>
> Representing that range with an accuracy of 20 ms takes about 22 bits, so 
> most implementations will use at least 32 bits. The additional bits can be 
> used for a greater range, a smaller accuracy, or both. ±86,400 with an 
> accuracy of 20 µs takes about 32 bits, so a range of ±86,400 seems likely 
> for some 32-bit compilers.

I believe that in Janus/Ada, we allowed +/- 2 days to make math a bit 
easier, but the limit is not wildly different than the textbook version. (I 
think we ignored the recommendation for 20 µs). It would be odd for a 
non-64-bit compiler to use a 64-bit Duration, as 64-bit operations are 
relatively expensive on smaller machines.

                                 Randy.


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-01-23 20:47 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-22 11:37 Ok to assume type Duration is for more than one day? reinkor
2017-01-22 12:28 ` Jeffrey R. Carter
2017-01-23 20:47   ` Randy Brukardt
2017-01-22 16:39 ` AdaMagica
2017-01-23  8:35 ` G.B.
2017-01-23  8:43   ` G.B.
2017-01-23 13:32   ` Björn Lundin
2017-01-23 13:53     ` G.B.
2017-01-23 14:32       ` Björn Lundin

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