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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,4b26cf620424a3ff,start X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!k41g2000yqb.googlegroups.com!not-for-mail From: "vincent.diemunsch@gmail.com" Newsgroups: comp.lang.ada Subject: Language Revision : "/" and "div" Date: Fri, 30 Apr 2010 23:52:18 -0700 (PDT) Organization: http://groups.google.com Message-ID: <8aa58be8-a9b2-4653-8234-e5b6a70f310f@k41g2000yqb.googlegroups.com> NNTP-Posting-Host: 87.91.37.131 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1272696738 31803 127.0.0.1 (1 May 2010 06:52:18 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 1 May 2010 06:52:18 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: k41g2000yqb.googlegroups.com; posting-host=87.91.37.131; posting-account=hya6vwoAAADTA0O27Aq3u6Su3lQKpSMz User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; fr-fr) AppleWebKit/531.22.7 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:11268 Date: 2010-04-30T23:52:18-07:00 List-Id: 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 :=3D 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=A0"n : integer :=3D (4 div 3)*6" that explicitly states that the result will be 6. OR "n : integer :=3D 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 . ---------------------------------------------------------------------------= ----- -- -- 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 :=3D 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 "=3D" (Left, Right : Fraction) return Boolean; function "<" (Left, Right : Fraction) return Boolean; function "<=3D" (Left, Right : Fraction) return Boolean; function ">" (Left, Right : Fraction) return Boolean; function ">=3D" (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;