comp.lang.ada
 help / color / mirror / Atom feed
From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: can Ada give run-time error or warning for integer division with non-zero remainder?
Date: Sun, 12 Aug 2012 08:03:35 -0600
Date: 2012-08-12T08:03:35-06:00	[thread overview]
Message-ID: <5027B7B7.7070705@shaw.ca> (raw)
In-Reply-To: <20120812091658.0ad5d91d@lufsen.sandat.dyndns.org>

Or you could simplify this further using some new Ada 2012 features, and 
eliminate the body.

package Integers is
    type Integer is new Standard.Integer;

    overriding function "/" (L : Integer; R  : Integer) return Integer is
      (Integer (Standard.Integer (L) / Standard.Integer (R)))
    with Post => "/"'Result * R = L;

    function "/" (L : Standard.Integer; R  : Integer) return Integer is
       (Integer (L / Standard.Integer (R)))
    with Post => "/"'Result * R = Integer (L);

    function "/" (L : Integer; R  : Standard.Integer) return Integer is
       (Integer (Standard.Integer (L) / R))
    with Post => "/"'Result * Integer (R) = L;

end Integers;

Brad


On 12/08/2012 1:16 AM, Per Sandberg wrote:
> If you want an new behaviour for "/" on an integer type you could always
> make a new integer type with the required properties:
> -------------------------------------------------------------------------
> package Integers is
>     type Integer is new Standard.Integer;
>     overriding function "/" (L : Integer; R  : Integer) return Integer;
>     function "/" (L : Standard.Integer; R  : Integer) return Integer;
>     function "/" (L : Integer; R  : Standard.Integer) return Integer;
> end Integers
> -------------------------------------------------------------------------
> package body Integers is
>
>     overriding function "/" (L : Integer; R  : Integer) return Integer is
>     begin
>        return Ret : Integer do
>           Ret := Integer (Standard.Integer (Standard.Integer (L) /
>     Standard.Integer (R))); if Ret * R /= L then
>              raise Constraint_Error with L'Img & "/" & R'Img & " gives
>     reminder."; end if;
>        end return;
>     end "/";
>
>     function "/" (L : Standard.Integer; R  : Integer) return Integer is
>     begin
>        return Ret : Integer do
>           Ret := Integer (L) / R;
>        end return;
>     end "/";
>
>     function "/" (L : Integer; R  : Standard.Integer) return Integer is
>     begin
>        return Ret : Integer do
>           Ret := L / Integer (R);
>        end return;
>     end "/";
>
> end Integers;




  reply	other threads:[~2012-08-12 14:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-12  1:14 can Ada give run-time error or warning for integer division with non-zero remainder? Nasser M. Abbasi
2012-08-12  5:45 ` J-P. Rosen
2012-08-12  6:45 ` Dmitry A. Kazakov
2012-08-12  7:16 ` Per Sandberg
2012-08-12 14:03   ` Brad Moore [this message]
2012-08-12 14:19 ` Robin Vowels
replies disabled

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