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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,fc647120984c9ad2 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newscon06.news.prodigy.net!prodigy.net!uns-out.usenetserver.com!news.usenetserver.com!pc02.usenetserver.com!news.flashnewsgroups.com-b7.4zTQh5tI3A!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Way to use Ada Mod function on floats? References: <1164987168.477589.240140@j44g2000cwa.googlegroups.com> <4571ac78_6@news.bluewin.ch> From: Stephen Leake Date: Sun, 03 Dec 2006 10:14:59 -0500 Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (windows-nt) Cancel-Lock: sha1:Ksyq4L6aBy1wUQlG2uxobAciMlM= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@flashnewsgroups.com Organization: FlashNewsgroups.com X-Trace: 815b84572ea2363d2952e03907 Xref: g2news2.google.com comp.lang.ada:7795 Date: 2006-12-03T10:14:59-05:00 List-Id: Gautier writes: > Stephen Leake: > >> SAL (http://stephe-leake.org/ada/sal.html) has: >> function Modulo (Dividend, Divisor : in Real_Type) return >> Real_Type >> is >> -- match names in Ada LRM 4.5.5 >> A : Real_Type renames Dividend; >> B : Real_Type renames Divisor; >> N : constant Integer := Integer (Real_Type'Floor (A / B)); >> begin >> return A - B * Real_Type (N); >> end Modulo; > > Out of curiosity, what is the need of converting Real_Type'Floor (A / > B) to an Integer and back to a Real_Type ? Since so many others claim to be able to read my mind, I almost feel it is unnecessary to answer! It is a mistake to convert to Integer; my tests for this pass when I delete that. This code was first written for Ada 83. Hmm; the Ada 95 LRM says 'Floor _did_ exist then (it is not listed as an extension to Ada 83), but apparently I was unaware of it, or it didn't exist in the DEC Ada compiler, or something. So I used 'round to Integer' instead: function Modulo (Dividend, Divisor : in FLOAT_TYPE) return FLOAT_TYPE is -- match names in LRM 4.5.5 A : FLOAT_TYPE renames Dividend; B : FLOAT_TYPE renames Divisor; Quotient : FLOAT_TYPE := Dividend / Divisor; N : INT_32_TYPE := INT_32_TYPE (Quotient - 0.5); begin return A - B * FLOAT_TYPE (N); end Modulo; Apparently when I converted to using 'Float, I did not fully think it thru. Thanks for pointing this out. It is always useful to expose code to review by experts :). Using 'Truncation gives different sign results; 'Floor matches the integer definition of "mod": Mod (10.0, 5.0) : 0.00000 Mod (11.0, 5.0) : 1.00000 Mod (14.0, 5.0) : 4.00000 Mod (10.0, -5.0) : 0.00000 Mod (11.0, -5.0) : -4.00000 Mod (14.0, -5.0) : -1.00000 Mod (-10.0, 5.0) : 0.00000 Mod (-11.0, 5.0) : 4.00000 Mod (-14.0, 5.0) : 1.00000 Mod (-10.0, -5.0) : 0.00000 Mod (-11.0, -5.0) : -1.00000 Mod (-14.0, -5.0) : -4.00000 Mod (0.0, 2.0) : 0.00000 Perhaps 'Truncation would match "rem"? I haven't looked at it in detail. -- -- Stephe