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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.182.226.132 with SMTP id rs4mr22343792obc.34.1411818280343; Sat, 27 Sep 2014 04:44:40 -0700 (PDT) X-Received: by 10.140.27.166 with SMTP id 35mr228737qgx.5.1411818280315; Sat, 27 Sep 2014 04:44:40 -0700 (PDT) Path: buffer1.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!a13no4387441igq.0!news-out.google.com!i10ni38qaf.0!nntp.google.com!k15no100116qaq.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 27 Sep 2014 04:44:40 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2a02:120b:c3e3:9270:a567:790b:a162:2a6d; posting-account=gRqrnQkAAAAC_02ynnhqGk1VRQlve6ZG NNTP-Posting-Host: 2a02:120b:c3e3:9270:a567:790b:a162:2a6d References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <829fff52-ee63-4053-b962-0d8262ec4ade@googlegroups.com> Subject: Re: Integers and Mathematical Correctness From: gautier_niouzes@hotmail.com Injection-Date: Sat, 27 Sep 2014 11:44:40 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: number.nntp.dca.giganews.com comp.lang.ada:189185 Date: 2014-09-27T04:44:40-07:00 List-Id: Le vendredi 26 septembre 2014 18:38:10 UTC+2, Niklas Holsti a =E9crit=A0: > I beg to disagree. Rational numbers with Integer numerator and > denominator are not very useful, because after a relatively small number > of arithmetic operations, the numerator and denominator often become > very large and overflow the Integer range. There is always the possibility of reducing the fraction - see below. > IMO rational numbers are useful only when the numerator and denominator > are "bignums", effectively unbounded integer types. Bignums are probably more useful but prehaps slower. Anyway there is an uni= versal solution with generics, where you can choose what you want. For instance if you really want to use the type Integer, that's easy: package Rationals is new Frac_Euclid(Integer, 0,1, "-","+","-","*","/"); The package Frac_Euclid is defned below (works also with non-numerci types!= ): generic -- to provide: type ring_elt is private; -- ring element type zero, one: ring_elt; -- 0 and 1 elements with function "-" (a:ring_elt) return ring_elt; -- unary oper. with function "+" (a,b:ring_elt) return ring_elt; -- binary oper= . with function "-" (a,b:ring_elt) return ring_elt; with function "*" (a,b:ring_elt) return ring_elt; with function "/" (a,b:ring_elt) return ring_elt; -- returns the quotient: a=3D b*q + r -- q:quotient, r:rest package Frac_euclid is type frac_elt is record a,b:ring_elt; end record; -- define fraction frac_0: constant frac_elt:=3D (zero,one); frac_1: constant frac_elt:=3D (one,one); function Reduction(f: frac_elt) return frac_elt; pragma Inline(Reduction); function "+" (f: frac_elt) return frac_elt; -- unary oper= . function "-" (f: frac_elt) return frac_elt; function "+" (f1,f2: frac_elt) return frac_elt; -- binary ope= r. function "+" (a: ring_elt; f: frac_elt) return frac_elt; function "+" (f: frac_elt; a: ring_elt) return frac_elt; function "-" (f1,f2: frac_elt) return frac_elt; function "-" (a: ring_elt; f: frac_elt) return frac_elt; function "-" (f: frac_elt; a: ring_elt) return frac_elt; function "*" (f1,f2: frac_elt) return frac_elt; function "*" (a: ring_elt; f: frac_elt) return frac_elt; function "*" (f: frac_elt; a: ring_elt) return frac_elt; function "/" (f1,f2: frac_elt) return frac_elt; function "/" (a: ring_elt; f: frac_elt) return frac_elt; function "/" (f: frac_elt; a: ring_elt) return frac_elt; function "/" (a,b: ring_elt) return frac_elt; function Eq(f1,f2: frac_elt) return Boolean; -- returns f1= =3Df2 auto_reduce: Boolean:=3D True; reduce_in_add: Boolean:=3D True; Zero_denominator: exception; Division_by_null_fraction: exception; end Frac_Euclid; The implementation (as well as a bigint implementation) is in MathPaqs: http://sf.net/projects/mathpaqs/ Subdirectory : algebra and multi. Enjoy _________________________=20 Gautier's Ada programming=20 http://www.openhub.net/accounts/gautier_bd