comp.lang.ada
 help / color / mirror / Atom feed
From: David Shochat <shochat@chelmsford.com>
Subject: modular arithmetic bug in gnat?
Date: 2000/02/09
Date: 2000-02-09T00:00:00+00:00	[thread overview]
Message-ID: <38A144A0.141EA33F@chelmsford.com> (raw)

The following test is the result of distilling an issue that came up at
work:

with Ada.Text_Io;

procedure Mod_Test is

    type M_2_16 is mod 2 ** 16;

    K : M_2_16 := 11_111;

    A : constant Integer :=
       Integer(K * 1000 / 25);
begin
    Ada.Text_Io.Put ("A = " & Integer'Image(A));
end Mod_Test;

When I run this with gnat (3.12p, Linux) the answer is 51224. This is
evidently the result of delaying the reduction mod 2**16 until after the
division.

The same program on Rational Apex (3.2.0b, HP-UX 11.0) gives 1416 which
is what you get if you reduce mod 2**16 immediately after the
multiplication but before the division.

Now based on 4.5(13), the result should be as if the operations were
performed left to right, i.e. multiplication followed by division. But
each of those operations must be as defined for the modular type, and
4.5.5(10) implies that the first operation is defined as including a
reduction. So the correct result must be

((11111 * 1000) mod 2**16) / 25, i.e. 1416.

Do you agree?

-- David






             reply	other threads:[~2000-02-09  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-09  0:00 David Shochat [this message]
2000-02-09  0:00 ` modular arithmetic bug in gnat? reason67
2000-02-09  0:00 ` tmoran
2000-02-16  0:00 ` Nick Roberts
replies disabled

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