From: Robert A Duff on
tmoran(a)acm.org writes:

>> And it would be desirable, because in most cases the explicit
>> "mod" (or "%") is more readable.
>
> I := Ring_Indices'succ(I);
> vs
> I := (I + 1) mod Ring_Size;
> or
> Bearing := Bearing + Turn_Angle;
> vs
> Bearing := (Bearing + Turn_Angle) mod 360;

The explicit "mod"s are more readable, I think.

For angles, you probably want a fixed-point type, and there's no such
thing as modular fixed-point types in Ada. There was a proposal to add
that to Ada 2005, but I think ARG decided not to do that, IIRC.

Note that you can always write functions that do a "mod",
if that's what you want -- Next_Ring_Index, or "+" on fixed-point
angles.

- Bob
From: tmoran on
>> I := Ring_Indices'succ(I);
>> vs
>> I := (I + 1) mod Ring_Size;
>> or
>> Bearing := Bearing + Turn_Angle;
>> vs
>> Bearing := (Bearing + Turn_Angle) mod 360;
>
>The explicit "mod"s are more readable, I think.

Interesting. I think the opposite. The explicit mod versions take a
computer-centric, rather than problem-centric, view, which is the opposite
of the usual "Ada approach". They are also subject to the possible error
of failing to write the "mod" part, whereas with modular types the
compiler has the responsibility to remember to do the mod operation.
From: Jeffrey Carter on
On 08/09/2010 06:28 AM, Robert A Duff wrote:
>
> Signed integers work fine for that. Except when you need
> that one extra bit -- that's the problem.

We've been able to do

type T is range 0 .. 2 ** N - 1;
for T'Size use N;

since Ada 83. So the only problem is when you want the equivalent of

type T is mod System.Max_Binary_Modulus;

since System.Max_Binary_Modulus - 1 > System.Max_Integer.

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

--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Robert A Duff on
Jeffrey Carter <spam.jrcarter.not(a)spam.not.acm.org> writes:

> On 08/09/2010 06:28 AM, Robert A Duff wrote:
>>
>> Signed integers work fine for that. Except when you need
>> that one extra bit -- that's the problem.
>
> We've been able to do
>
> type T is range 0 .. 2 ** N - 1;
> for T'Size use N;
>
> since Ada 83.

Yes. And T'Size = N by default in Ada >= 95, so I would write:

pragma Assert(T'Size = N);

instead of the Size clause.

>...So the only problem is when you want the equivalent of
>
> type T is mod System.Max_Binary_Modulus;
>
> since System.Max_Binary_Modulus - 1 > System.Max_Integer.

Exactly. (Well, I don't think that's required by the RM,
but in all Ada implementations it's probably the case
that Max_Binary_Modulus = 2*(Max_Integer+1).)

- Bob
From: Yannick DuchĂȘne (Hibou57) on
Le Mon, 09 Aug 2010 15:28:02 +0200, Robert A Duff
<bobduff(a)shell01.theworld.com> a Ă©crit:
>> Hmmm... Hash coded tables?
>
> Signed integers work fine for that. Except when you need
> that one extra bit -- that's the problem.

All of the hash functions I have seen so far use modular types. Do you
known ones computing on integers ?