comp.lang.ada
 help / color / mirror / Atom feed
* Wraparound on multiply overflow
@ 2001-08-08 21:16 Paul Graham
  2001-08-09 16:23 ` Jacob Sparre Andersen
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Paul Graham @ 2001-08-08 21:16 UTC (permalink / 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




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

end of thread, other threads:[~2001-08-16  5:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-08 21:16 Wraparound on multiply overflow Paul Graham
2001-08-09 16:23 ` Jacob Sparre Andersen
2001-08-10  8:25 ` Florian Weimer
2001-08-16  5:17 ` David Thompson

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