comp.lang.ada
 help / color / mirror / Atom feed
From: stt@henning.camb.inmet.com (Tucker Taft)
Subject: Wraparound on modular conversion (was: DECAda/VMS - calling GETJPI)
Date: 1996/06/05
Date: 1996-06-05T00:00:00+00:00	[thread overview]
Message-ID: <DsIC17.C5y.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: dewar.833924742@schonberg


Robert is right, the letter and the intent of RM95
is that conversions to a modular type do *not* do a "modulo"
operation.  We discussed this particular issue at length during
one of the 9X language review meetings, and ultimately decided 
it this way.  This approach prevents unintended wraparound on conversion.

In retrospect, I am not positive we made the right decision, since 
I do find myself on occasion wishing there were some way to have 
the conversion do the modulo operation.

One way that will sometimes work is:

    X : Signed_Int;
    Y : Modular_Int := Modular_Int(Signed_Int'Pos(X) mod Modular_Int'Modulus);

This will do the modulo operation in "root_integer" which is fine so
long as Modular_Int'Modulus is <= System.Max_Int.  Otherwise, you
will get a compile-time error, and you may have to revert to
unchecked_conversion or to a sequence of statements that avoid
the overflow.  

For example, if Modular_Int'Modulus is > Signed_Int'Last,
the following sequence of statements will work without a
possibility of overflow:

    if X >= 0 then
        Y := Modular_Int(X);
    else
	-- X is < 0, make it positive, convert, and then subtract
	--  with wraparound.
        Y := Modular_Int(X - Signed_Int'First) - 
               (Modular_Int(-1 - Signed_Int'First) + 1);
    end if;

Probably the best long term solution would be to add an attribute
function, analogous to 'Val, that does the modulo operation as part
of the conversion.  E.g. Modular_Int'Modulo(X) would never overflow,
and would perform the modulo operation as part of the conversion.
Might be a good "Uniformity Rapporteur Group" (URG) idea...

-Tucker Taft   stt@inmet.com
Intermetrics, Inc.




  reply	other threads:[~1996-06-05  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-06-03  0:00 DECAda/VMS - calling GETJPI Alan Paterson
1996-06-03  0:00 ` Ken Garlington
1996-06-03  0:00 ` Stuart Palin
1996-06-03  0:00   ` Michael F Brenner
1996-06-03  0:00     ` Robert Dewar
1996-06-04  0:00       ` Ken Garlington
1996-06-06  0:00         ` Robert Dewar
1996-06-04  0:00       ` Michael F Brenner
1996-06-04  0:00         ` Robert Dewar
1996-06-04  0:00           ` Michael F Brenner
1996-06-04  0:00             ` Robert Dewar
1996-06-04  0:00             ` Robert Dewar
1996-06-05  0:00               ` Tucker Taft [this message]
1996-06-05  0:00                 ` Wraparound on modular conversion (was: DECAda/VMS - calling GETJPI) Robert Dewar
1996-06-05  0:00               ` DECAda/VMS - calling GETJPI Robert A Duff
1996-06-05  0:00                 ` Robert Dewar
1996-06-05  0:00               ` Fergus Henderson
1996-06-05  0:00                 ` Robert A Duff
1996-06-05  0:00             ` Adam Beneschan
1996-06-07  0:00               ` Norman H. Cohen
1996-06-11  0:00                 ` Adam Beneschan
1996-06-03  0:00 ` Mats Weber
replies disabled

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