From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,83d7c4caadb45100 X-Google-Attributes: gid103376,public From: stt@henning.camb.inmet.com (Tucker Taft) Subject: Wraparound on modular conversion (was: DECAda/VMS - calling GETJPI) Date: 1996/06/05 Message-ID: #1/1 X-Deja-AN: 158523835 sender: news@inmet.camb.inmet.com (USENET news) x-nntp-posting-host: henning.camb.inmet.com references: organization: Intermetrics, Inc. newsgroups: comp.lang.ada Date: 1996-06-05T00:00:00+00:00 List-Id: 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.