From: Paul Graham <pgraham@cadence.com>
Subject: Wraparound on multiply overflow
Date: Wed, 08 Aug 2001 14:16:03 -0700
Date: 2001-08-08T14:16:03-07:00 [thread overview]
Message-ID: <3B71AC13.F67BDC2@cadence.com> (raw)
In C it is defined that the result of multiplying two unsigned integers
is the mathematical result modulo the value (int_max + 1), where int_max
is the maximum unsigned integer. I'm interested in porting to Ada a
random number generator which relies on this rule. The code looks like:
unsigned int X[10];
...
X[0] = seed;
for (i = 1; i <= 10; i++)
X[i] = X[i-1] * 12345;
The idea is that a table of random looking numbers is created by
repeated multiplication of a seed by some constant. This sequence is
guaranteed to be the same across conforming C implementations, because
of the modulo rule I mentioned above.
Can this code be written in Ada? A few iterations of the loop would
result in a range error or overflow. I could suppress checks, but would
the result of an overflowing multiply be defined in that case? Is there
any other way of emulating C's behavior for this program? I know that
Ada has modular types, but these don't work with multiply, do they?
Paul
next reply other threads:[~2001-08-08 21:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-08-08 21:16 Paul Graham [this message]
2001-08-09 16:23 ` Wraparound on multiply overflow Jacob Sparre Andersen
2001-08-10 8:25 ` Florian Weimer
2001-08-16 5:17 ` David Thompson
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox