comp.lang.ada
 help / color / mirror / Atom feed
* can Ada give run-time error or warning for integer division with non-zero remainder?
@ 2012-08-12  1:14 Nasser M. Abbasi
  2012-08-12  5:45 ` J-P. Rosen
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Nasser M. Abbasi @ 2012-08-12  1:14 UTC (permalink / raw)



In Ada when dividing 2 integers, the result is an integer
with the remainder ignored.  Hence 3/2 gives 1

----------------------------------
with Ada.Integer_Text_IO ; use Ada.Integer_Text_IO ;

procedure foo4 is
   N : constant integer := 3;
   M : integer;
   
   begin
     M := N/2;
     put(M);
        
end foo4;
--------------------------

>gnatmake foo4.adb
>./foo4
           1

Now, suppose I want to know that a division between 2 integers
has resulted in nonzero remainder that was thrown away. May be
because my algorithm is meant to work only for even values and
an odd value means there was a bug somewhere and I want to
know about it.

Is there any kind of run-time switch to tell it to
check for this? I know I can always add logic myself to
check for this in the code, using rem() for example (which
might be the better solution actually) but I was just
wondering if there is a run-time switch for this.

thanks,
-Nasser



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

* Re: can Ada give run-time error or warning for integer division with non-zero remainder?
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: J-P. Rosen @ 2012-08-12  5:45 UTC (permalink / raw)


Le 12/08/2012 03:14, Nasser M. Abbasi a �crit :
> 
> In Ada when dividing 2 integers, the result is an integer
> with the remainder ignored.  Hence 3/2 gives 1
> [...]
> Is there any kind of run-time switch to tell it to
> check for this? I know I can always add logic myself to
> check for this in the code, using rem() for example (which
> might be the better solution actually) but I was just
> wondering if there is a run-time switch for this.
> 
Fortunately not!

Dividing integers uses the integer division. If you want something else,
don't use integer division.

Having the outcome of an operation depend on a switch would imply that
you cannot predict the result of a program by reading it!

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr



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

* Re: can Ada give run-time error or warning for integer division with non-zero remainder?
  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:19 ` Robin Vowels
  3 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2012-08-12  6:45 UTC (permalink / raw)


On Sat, 11 Aug 2012 20:14:42 -0500, Nasser M. Abbasi wrote:

> In Ada when dividing 2 integers, the result is an integer
> with the remainder ignored.  Hence 3/2 gives 1
> 
> ----------------------------------
> with Ada.Integer_Text_IO ; use Ada.Integer_Text_IO ;
> 
> procedure foo4 is
>    N : constant integer := 3;
>    M : integer;
>    
>    begin
>      M := N/2;
>      put(M);
>         
> end foo4;
> --------------------------
> 
>>gnatmake foo4.adb
>>./foo4
>            1
> Now, suppose I want to know that a division between 2 integers
> has resulted in nonzero remainder that was thrown away. May be
> because my algorithm is meant to work only for even values and
> an odd value means there was a bug somewhere and I want to
> know about it.

It is not integer division then. You could use integer interval division
instead: [3, 3] / [2, 2] = [1, 2].

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: can Ada give run-time error or warning for integer division with non-zero remainder?
  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
  2012-08-12 14:19 ` Robin Vowels
  3 siblings, 1 reply; 6+ messages in thread
From: Per Sandberg @ 2012-08-12  7:16 UTC (permalink / raw)


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;
-------------------------------------------------------------------------
/P


On Sat, 11 Aug 2012 20:14:42 -0500 "Nasser M. Abbasi" <nma@12000.org>
wrote:

> 
> In Ada when dividing 2 integers, the result is an integer
> with the remainder ignored.  Hence 3/2 gives 1
> 
> ----------------------------------
> with Ada.Integer_Text_IO ; use Ada.Integer_Text_IO ;
> 
> procedure foo4 is
>    N : constant integer := 3;
>    M : integer;
>    
>    begin
>      M := N/2;
>      put(M);
>         
> end foo4;
> --------------------------
> 
> >gnatmake foo4.adb
> >./foo4
>            1
> 
> Now, suppose I want to know that a division between 2 integers
> has resulted in nonzero remainder that was thrown away. May be
> because my algorithm is meant to work only for even values and
> an odd value means there was a bug somewhere and I want to
> know about it.
> 
> Is there any kind of run-time switch to tell it to
> check for this? I know I can always add logic myself to
> check for this in the code, using rem() for example (which
> might be the better solution actually) but I was just
> wondering if there is a run-time switch for this.
> 
> thanks,
> -Nasser





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

* Re: can Ada give run-time error or warning for integer division with non-zero remainder?
  2012-08-12  7:16 ` Per Sandberg
@ 2012-08-12 14:03   ` Brad Moore
  0 siblings, 0 replies; 6+ messages in thread
From: Brad Moore @ 2012-08-12 14:03 UTC (permalink / raw)


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;




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

* Re: can Ada give run-time error or warning for integer division with non-zero remainder?
  2012-08-12  1:14 can Ada give run-time error or warning for integer division with non-zero remainder? Nasser M. Abbasi
                   ` (2 preceding siblings ...)
  2012-08-12  7:16 ` Per Sandberg
@ 2012-08-12 14:19 ` Robin Vowels
  3 siblings, 0 replies; 6+ messages in thread
From: Robin Vowels @ 2012-08-12 14:19 UTC (permalink / raw)


On Aug 12, 11:14 am, "Nasser M. Abbasi" <n...@12000.org> wrote:
> In Ada when dividing 2 integers, the result is an integer
> with the remainder ignored.  Hence 3/2 gives 1
>
> ----------------------------------
> with Ada.Integer_Text_IO ; use Ada.Integer_Text_IO ;
>
> procedure foo4 is
>    N : constant integer := 3;
>    M : integer;
>
>    begin
>      M := N/2;
>      put(M);
>
> end foo4;
> --------------------------
>
> >gnatmake foo4.adb
> >./foo4
>
>            1
>
> Now, suppose I want to know that a division between 2 integers
> has resulted in nonzero remainder that was thrown away. May be
> because my algorithm is meant to work only for even values and
> an odd value means there was a bug somewhere and I want to
> know about it.

Use MOD.




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

end of thread, other threads:[~2012-08-15  2:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2012-08-12 14:19 ` Robin Vowels

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