From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7a434e753b9dd044 X-Google-Attributes: gid103376,public From: Stephen Leake Subject: Re: Time_Span divide question Date: 1997/05/16 Message-ID: <337C5D12.6230@gsfc.nasa.gov>#1/1 X-Deja-AN: 241905751 References: <5lg555$rba3@castor.cca.rockwell.com> <337BD41B.2FE1@mail.us.net> Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA Reply-To: Stephen.Leake@gsfc.nasa.gov Newsgroups: comp.lang.ada Date: 1997-05-16T00:00:00+00:00 List-Id: Mark D. McKinney wrote: > > > D1 := 0.495; > > D2 := 0.500; > > Put_Line ("Duration divide result: " & Integer'Image(Integer(D1/D2))); > D1 / D2 = 0.990 > Integer(D1/D2) = 1 > > > > TS1 := To_Time_Span (D1); > > TS2 := To_Time_Span (D2); > > Put_Line ("Time_Span divide result: " & Integer'Image(TS1/TS2)); > > To_Time_Span(D1) = Integer(D1) = 0 > To_Time_Span(D2) = Integer(D2) = 1 Um, no. Time_Span is an integer type, but it is scaled, so that 1 is one Time_Unit, which is a small fraction of seconds. In GNAT, Time_Unit is one nanosecond. So To_Time_Span (foo) is NOT the same as Integer (foo). > TS1 / TS2 = 0 > > Yes they should be different. > > Careful where rounding occurs. Here's a compilable test: with Text_IO; use Text_IO; with Ada.Real_Time; use Ada.Real_Time; with Unchecked_Conversion; with Interfaces; procedure Test is D1, D2 : Duration; TS1, TS2 : Time_Span; function To_Int_64 is new Unchecked_Conversion (Source => Time_Span, Target => Interfaces.Integer_64); begin D1 := 0.495; D2 := 0.500; Put_Line ("Duration divide result: " & Integer'Image(Integer(D1/D2))); TS1 := To_Time_Span (D1); TS2 := To_Time_Span (D2); Put_Line (" Int_64 (TS1) => " & Interfaces.Integer_64'Image (To_Int_64 (TS1))); Put_Line (" Int_64 (TS2) => " & Interfaces.Integer_64'Image (To_Int_64 (TS2))); Put_Line ("Time_Span divide result: " & Integer'Image(TS1/TS2)); end Test; Note that your compiler may have a different representation for Time_Span. Running the code on GNAT 3.09, under Windows 95 on a Pentium, I get: Duration divide result: 0 Int_64 (TS1) => 495000000 Int_64 (TS2) => 500000000 Time_Span divide result: 0 Integer division truncates, in order to satisfy the rules in RM 4.5.5 (1 .. 5). Fixed point division goes up or down; see RM 4.5.5(21) -- - Stephe