comp.lang.ada
 help / color / mirror / Atom feed
* Way to use Ada Mod function on floats?
@ 2006-12-01 15:32 AAFellow
  2006-12-01 16:37 ` Jeffrey Creem
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: AAFellow @ 2006-12-01 15:32 UTC (permalink / raw)


Hi guys,

I need to perform the mod operation on floats.  Does anyone know of a
simple way to do this in Ada? It seems the mod function is only for
integers.  I can probably multiply out the decimal places, and then
divide my answer back, but if there is a more simple way that saves
computations, I'd rather do that.

Thank you for any help you can provide!

AA




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

* Re: Way to use Ada Mod function on floats?
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Jeffrey Creem @ 2006-12-01 16:37 UTC (permalink / raw)


AAFellow@hotmail.com wrote:
> Hi guys,
> 
> I need to perform the mod operation on floats.  Does anyone know of a
> simple way to do this in Ada? It seems the mod function is only for
> integers.  I can probably multiply out the decimal places, and then
> divide my answer back, but if there is a more simple way that saves
> computations, I'd rather do that.
> 
> Thank you for any help you can provide!
> 
> AA
> 

Hmm. I can't think of anything predefined that makes it all that easy.

perhaps you should just pragma import the one from libc



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

* Re: Way to use Ada Mod function on floats?
  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
  3 siblings, 0 replies; 14+ messages in thread
From: Adam Beneschan @ 2006-12-01 17:21 UTC (permalink / raw)


AAFellow@hotmail.com wrote:
> Hi guys,
>
> I need to perform the mod operation on floats.  Does anyone know of a
> simple way to do this in Ada? It seems the mod function is only for
> integers.  I can probably multiply out the decimal places, and then
> divide my answer back, but if there is a more simple way that saves
> computations, I'd rather do that.
>
> Thank you for any help you can provide!

The 'Remainder attribute may help---see A.5.3.  T'Remainder(X,Y) [where
T is a floating-point type] will return a value in the range [-Y/2,
Y/2], which isn't exactly what you want, but I think you can just add Y
to the result if it's negative.  I'm assuming that Y is positive here.

                        -- Adam




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

* Re: Way to use Ada Mod function on floats?
  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
  3 siblings, 0 replies; 14+ messages in thread
From: Chris Moore @ 2006-12-01 21:13 UTC (permalink / raw)


AAFellow@hotmail.com wrote:
> Hi guys,
> 
> I need to perform the mod operation on floats.  Does anyone know of a
> simple way to do this in Ada? It seems the mod function is only for
> integers.  I can probably multiply out the decimal places, and then
> divide my answer back, but if there is a more simple way that saves
> computations, I'd rather do that.
> 
> Thank you for any help you can provide!
> 
> AA

Something like this (I don't have a compiler near me atm) :

function Mod (N, M : Float) return Float is
   I : Integer := Integer (N / M - 0.5);
begin
   if I < 0 then I := I + 1; end if;
   return N - Float (I) * M;
end Mod;

Chris

-- 
Sig pending!



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

* Re: Way to use Ada Mod function on floats?
  2006-12-01 15:32 Way to use Ada Mod function on floats? AAFellow
                   ` (2 preceding siblings ...)
  2006-12-01 21:13 ` Chris Moore
@ 2006-12-02 11:11 ` Stephen Leake
  2006-12-02 16:40   ` Gautier
  3 siblings, 1 reply; 14+ messages in thread
From: Stephen Leake @ 2006-12-02 11:11 UTC (permalink / raw)


"AAFellow@hotmail.com" <AAFellow@hotmail.com> writes:

> Hi guys,
>
> I need to perform the mod operation on floats.  Does anyone know of a
> simple way to do this in Ada? It seems the mod function is only for
> integers.  I can probably multiply out the decimal places, and then
> divide my answer back, but if there is a more simple way that saves
> computations, I'd rather do that.

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;

-- 
-- Stephe



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

* Re: Way to use Ada Mod function on floats?
  2006-12-02 11:11 ` Stephen Leake
@ 2006-12-02 16:40   ` Gautier
  2006-12-02 17:48     ` Simon Wright
                       ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Gautier @ 2006-12-02 16:40 UTC (permalink / raw)


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 ?
______________________________________________________________
Gautier         -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

* Re: Way to use Ada Mod function on floats?
  2006-12-02 16:40   ` Gautier
@ 2006-12-02 17:48     ` Simon Wright
  2006-12-02 18:21       ` 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
  2 siblings, 2 replies; 14+ messages in thread
From: Simon Wright @ 2006-12-02 17:48 UTC (permalink / raw)


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 ?

Especially when it can overflow and give you a Constraint_Error.



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

* Re: Way to use Ada Mod function on floats?
  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
  1 sibling, 1 reply; 14+ messages in thread
From: Dmitry A. Kazakov @ 2006-12-02 18:21 UTC (permalink / raw)


On Sat, 02 Dec 2006 17:48:34 +0000, Simon Wright wrote:

> 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 ?
> 
> Especially when it can overflow and give you a Constraint_Error.

Hmm, why is it converted to integer? I would stay all the way in
Real_Type'Base:

 return Real_Type'Base
     (Dividend - Divisor * Real_Type'Base'Floor (Dividend / Divisor));

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Way to use Ada Mod function on floats?
  2006-12-02 18:21       ` Dmitry A. Kazakov
@ 2006-12-02 18:24         ` Dmitry A. Kazakov
  0 siblings, 0 replies; 14+ messages in thread
From: Dmitry A. Kazakov @ 2006-12-02 18:24 UTC (permalink / raw)


On Sat, 2 Dec 2006 19:21:43 +0100, Dmitry A. Kazakov wrote:

> On Sat, 02 Dec 2006 17:48:34 +0000, Simon Wright wrote:
> 
>> 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 ?
>> 
>> Especially when it can overflow and give you a Constraint_Error.
> 
> Hmm, why is it converted to integer? I would stay all the way in
> Real_Type'Base:
> 
>  return Real_Type'Base
>      (Dividend - Divisor * Real_Type'Base'Floor (Dividend / Divisor));

Of course, a qualified expression meant, so Real_Type'Base' in the first
line.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Way to use Ada Mod function on floats?
  2006-12-02 17:48     ` Simon Wright
  2006-12-02 18:21       ` Dmitry A. Kazakov
@ 2006-12-03  0:50       ` Jeffrey R. Carter
  1 sibling, 0 replies; 14+ messages in thread
From: Jeffrey R. Carter @ 2006-12-03  0:50 UTC (permalink / raw)


Simon Wright wrote:
> Gautier <gautier@fakeaddress.nil> writes:
> 
>> Stephen Leake:
>>
>>>       N : constant Integer := Integer (Real_Type'Floor (A / B));
 >>
>> Out of curiosity, what is the need of converting Real_Type'Floor (A /
>> B) to an Integer and back to a Real_Type ?
> 
> Especially when it can overflow and give you a Constraint_Error.

And why 'Floor? Wouldn't 'Truncation be better?

-- 
Jeff Carter
"Run away! Run away!"
Monty Python and the Holy Grail
58



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

* Re: Way to use Ada Mod function on floats?
  2006-12-02 16:40   ` Gautier
  2006-12-02 17:48     ` Simon Wright
@ 2006-12-03  1:05     ` Randy Brukardt
  2006-12-03 15:14     ` Stephen Leake
  2 siblings, 0 replies; 14+ messages in thread
From: Randy Brukardt @ 2006-12-03  1:05 UTC (permalink / raw)


"Gautier" <gautier@fakeaddress.nil> wrote in message
news:4571ac78_6@news.bluewin.ch...
> 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 ?

He just wanted to get Constraint_Error sometimes (esp. on Janus/Ada, where
Integer is 16 bits!). ;-)

                             Randy.





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

* Re: Way to use Ada Mod function on floats?
  2006-12-02 16:40   ` Gautier
  2006-12-02 17:48     ` Simon Wright
  2006-12-03  1:05     ` Randy Brukardt
@ 2006-12-03 15:14     ` Stephen Leake
  2006-12-03 22:27       ` Jeffrey R. Carter
  2 siblings, 1 reply; 14+ messages in thread
From: Stephen Leake @ 2006-12-03 15:14 UTC (permalink / raw)


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



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

* Re: Way to use Ada Mod function on floats?
  2006-12-03 15:14     ` Stephen Leake
@ 2006-12-03 22:27       ` Jeffrey R. Carter
  2006-12-04 11:31         ` Stephen Leake
  0 siblings, 1 reply; 14+ messages in thread
From: Jeffrey R. Carter @ 2006-12-03 22:27 UTC (permalink / raw)


Stephen Leake wrote:
> 
> Perhaps 'Truncation would match "rem"? I haven't looked at it in
> detail.

OK. Some of the suggestions have actually been for "rem" equivalents, 
which may be why I was thinking along those lines. Ada is about the only 
language that provides both, and many use a name like "mod" for 
remainder functions.

So, why don't you call your function "mod"?

-- 
Jeff Carter
"No one is to stone anyone until I blow this whistle,
do you understand? Even--and I want to make this
absolutely clear--even if they do say, 'Jehovah.'"
Monty Python's Life of Brian
74



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

* Re: Way to use Ada Mod function on floats?
  2006-12-03 22:27       ` Jeffrey R. Carter
@ 2006-12-04 11:31         ` Stephen Leake
  0 siblings, 0 replies; 14+ messages in thread
From: Stephen Leake @ 2006-12-04 11:31 UTC (permalink / raw)


"Jeffrey R. Carter" <spam.not.jrcarter@acm.not.spam.org> writes:

> Stephen Leake wrote:
>> Perhaps 'Truncation would match "rem"? I haven't looked at it in
>> detail.
>
> OK. Some of the suggestions have actually been for "rem" equivalents,
> which may be why I was thinking along those lines. Ada is about the
> only language that provides both, and many use a name like "mod" for
> remainder functions.
>
> So, why don't you call your function "mod"?

It is renamed to that in the spec.

In the original implementation, the body was separate, to allow for
different implementations (in assembler) on different processors.
Separate bodies cannot have operator names. That turned out to be
unnecessary, but I kept the names.

I prefer non-operator names in bodies; it makes searching for things
easier.

-- 
-- Stephe



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

end of thread, other threads:[~2006-12-04 11:31 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2006-12-03 22:27       ` Jeffrey R. Carter
2006-12-04 11:31         ` Stephen Leake

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