comp.lang.ada
 help / color / mirror / Atom feed
* Language Revision : "/" and "div"
@ 2010-05-01  6:52 vincent.diemunsch
  2010-05-01  9:25 ` Dmitry A. Kazakov
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: vincent.diemunsch @ 2010-05-01  6:52 UTC (permalink / raw)


Hi everybody,

I wonder why Ada uses "/" to express the integer division and not
"div".
This leads to the fact that an expression like 4/3*6 that any decent
calculator
would evaluate as 8 is in Ada evaluated as 6 !!! That is a shame :-).

Therefore, I propose the following, for a new revision of the
language :
- discard the function "/" (left, right : Integer) return Integer and
replace it by the operator "div",
that sounds quite logical with the existing "rem" and "mod".
- add a fraction package with a function "/" (left, right :
Integer'base) return Fraction; as constructor.

Then when compiling "n : integer := 4/3*6;" an error will occur,
because the result will be the fraction
8/1 and not an integer. The sentence could then be changed in
EITHER "n : integer := (4 div 3)*6" that explicitly states that the
result will be 6.
OR        "n : integer := Rounding (4/3*6);

I think this solution is safe and will add simplicity and precision to
integer operations.
Moreover, this correct handling of integer computation will allow
compilers to make optimizations
like for instance simplifying expressions like "2/3 * n * 6 * i" ->
"4*n*i"

Do you think I could summit this to the next revision of the
language ?
If yes, how ?

Vincent

Here is a example of a fraction package :

--------------------------------------------------------------------------------
-- This package is a simple implementation of common fractions.
--
-- Copyright (C) 2009 Vincent DIEMUNSCH.
-- GNU General Public License.
--
-- This program is free software: you can redistribute it and/or
modify it under
-- the terms of the GNU General Public License as published by the
Free Software
-- Foundation, either version 3 of the License, or (at your option)
any later
-- version.
--
-- This file is distributed in the hope that it will be useful, but
WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR
-- A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
--
-- You should have received a copy of the GNU General Public License
along with
-- this program.  If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------
--
-- REVISION HISTORY :
-- Version 1.0, Author : V. DIEMUNSCH, date : 12/01/2009, Creation
-- Version 1.1, Author : V. DIEMUNSCH, date : 18/05/2009, Created 2
GCD functions in body
-- Version 1.2, Author : V. DIEMUNSCH, date : 27/06/2009, made type
Fraction private
-- Version 1.3, Author : V. DIEMUNSCH, date : 30/06/2009, fixed a
major bug in Complex_GCD !

package Fractions is

   Max : constant := Integer'Last;
   subtype Numerator   is Integer;  -- range - Max .. + Max;
   subtype Denominator is Positive; -- range     1 .. + Max;

   type Fraction is private;

   -- The type Fraction is private, to ensure that a fraction is
always in canonical form.
   -- The canonical form means that the denominator and the numerator
have no common factors,
   -- and that the denominator is positive. Zero has the unique
representation 0/1.
   -- The best way to create a new fraction is to use the following
function "/",
   -- that canonicalizes the fraction using a binary GCD algorithm.
   -- After each operation the fraction is canonicalized, sometimes
using a smart
   -- algorithm to reduce computation speed. Setting directly the
value of the
   -- Numerator and Denominator can avoid these repetitive
computations.
   -- But then it is the responsibility of the user that the assigned
values have
   -- no common factors.

   function "/" (Left : Numerator; Right : Denominator) return
Fraction;
   -- The Fraction returned is in canonical form.

   function Num (F : Fraction) return Numerator;
   function Den (F : Fraction) return Denominator;

   function Set_Canonical_Fraction (Left : Numerator; Right :
Denominator) return Fraction;
   -- Bypass canonicalization ... So it is the caller responsability
   -- to make sure the fraction is in canonical form !
   pragma Inline (Num, Den, Set_Canonical_Fraction);

      -- GCD :
   function GCD (n, m : Positive) return Positive;

   function Image  (F : Fraction) return String;

   ----------------------------
   -- "Standard" description --
   ----------------------------

   -- (Mimics package Standard)
   -- The predefined operators for this type are as follows:

   function "="   (Left, Right : Fraction) return Boolean;
   function "<"   (Left, Right : Fraction) return Boolean;
   function "<="  (Left, Right : Fraction) return Boolean;
   function ">"   (Left, Right : Fraction) return Boolean;
   function ">="  (Left, Right : Fraction) return Boolean;

   function "+"   (Right : Fraction) return Fraction;
   function "-"   (Right : Fraction) return Fraction;
   function "abs" (Right : Fraction) return Fraction;

   function "+"   (Left, Right : Fraction) return Fraction;
   function "-"   (Left, Right : Fraction) return Fraction;
   function "*"   (Left, Right : Fraction) return Fraction;
   function "/"   (Left, Right : Fraction) return Fraction;

   function inv   (Right : Fraction) return Fraction;

   function "**"  (Left : Fraction; Right : Integer'Base) return
Fraction;

   -- with standard integers:
   function "*" (Left : Integer'Base; Right : Fraction) return
Fraction;
   function "*" (Left : Fraction; Right : Integer'Base) return
Fraction;
   function "/" (Left : Integer'Base; Right : Fraction) return
Fraction;
   function "/" (Left : Fraction; Right : Integer'Base) return
Fraction;

   function div (left, right : Integer) return Integer;

   function Floor             (F : Fraction) return Integer;
   function Ceiling           (F : Fraction) return Integer;
   function Rounding          (F : Fraction) return Integer;
   function Unbiased_Rounding (F : Fraction) return Integer;


   -- with real types:

   generic
      type F is digits <>;
   function To_Float (Q : Fraction) return F;

   generic
      type F is delta <>;
   function To_Fixed_Point (Q : Fraction) return F;

private

   type Fraction is record
      Num : Numerator;
      Den : Denominator;
   end record;

end Fractions;




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

end of thread, other threads:[~2010-05-05  2:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-01  6:52 Language Revision : "/" and "div" vincent.diemunsch
2010-05-01  9:25 ` Dmitry A. Kazakov
2010-05-01 10:46 ` Georg Bauhaus
2010-05-01 16:26 ` Jeffrey R. Carter
2010-05-02 21:33 ` Gene
2010-05-05  2:31 ` Yannick Duchêne (Hibou57)

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