comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Way to use Ada Mod function on floats?
Date: Sun, 03 Dec 2006 10:14:59 -0500
Date: 2006-12-03T10:14:59-05:00	[thread overview]
Message-ID: <u3b7x2eq4.fsf@stephe-leake.org> (raw)
In-Reply-To: 4571ac78_6@news.bluewin.ch

Gautier <gautier@fakeaddress.nil> 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



  parent reply	other threads:[~2006-12-03 15:14 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-01 15:32 Way to use Ada Mod function on floats? AAFellow
2006-12-01 16:37 ` Jeffrey Creem
2006-12-01 17:21 ` Adam Beneschan
2006-12-01 21:13 ` Chris Moore
2006-12-02 11:11 ` Stephen Leake
2006-12-02 16:40   ` Gautier
2006-12-02 17:48     ` Simon Wright
2006-12-02 18:21       ` Dmitry A. Kazakov
2006-12-02 18:24         ` Dmitry A. Kazakov
2006-12-03  0:50       ` Jeffrey R. Carter
2006-12-03  1:05     ` Randy Brukardt
2006-12-03 15:14     ` Stephen Leake [this message]
2006-12-03 22:27       ` Jeffrey R. Carter
2006-12-04 11:31         ` Stephen Leake
replies disabled

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