From ea99441e0da927e5a40cf21311265c7e22974f12 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Mon, 13 Feb 2017 13:21:17 +1100 Subject: Preference dedupe removed, bignum library obtained from internet (will be replaced later) --- src/rationals.adb | 78 +++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 40 deletions(-) (limited to 'src/rationals.adb') diff --git a/src/rationals.adb b/src/rationals.adb index 7ae321c..62fe9b9 100644 --- a/src/rationals.adb +++ b/src/rationals.adb @@ -7,12 +7,12 @@ package body Rationals is function Reduce - (Numerator, Denominator : in Integer) + (Numerator, Denominator : in Big_Unsigned) return Fraction is - A : Integer := Numerator; - B : Integer := Denominator; - Temp : Integer; + A : Big_Unsigned := Numerator; + B : Big_Unsigned := Denominator; + Temp : Big_Unsigned; begin -- Euclid's algorithm loop @@ -21,7 +21,8 @@ package body Rationals is B := Temp mod B; exit when B = 0; end loop; - return (Num => Numerator / A, Den => Denominator / A); + return (Num => Numerator / A, + Den => Denominator / A); end Reduce; @@ -42,7 +43,7 @@ package body Rationals is return Fraction is begin return Reduce - (Left.Num + Left.Den * Right, + (Left.Num + Left.Den * Utils.To_Big_Unsigned (Word (Right)), Left.Den); end "+"; @@ -52,7 +53,7 @@ package body Rationals is return Fraction is begin return Reduce - (Left * Right.Den + Right.Num, + (Utils.To_Big_Unsigned (Word (Left)) * Right.Den + Right.Num, Right.Den); end "+"; @@ -74,7 +75,7 @@ package body Rationals is return Fraction is begin return Reduce - (Left.Num - Left.Den * Right, + (Left.Num - Left.Den * Utils.To_Big_Unsigned (Word (Right)), Left.Den); end "-"; @@ -84,7 +85,7 @@ package body Rationals is return Fraction is begin return Reduce - (Left * Right.Den - Right.Num, + (Utils.To_Big_Unsigned (Word (Left)) * Right.Den - Right.Num, Right.Den); end "-"; @@ -116,8 +117,8 @@ package body Rationals is return Fraction is begin return Reduce - (Left.Num * Right, - Right); + (Left.Num * Utils.To_Big_Unsigned (Word (Right)), + Left.Den); end "*"; function "*" @@ -126,7 +127,7 @@ package body Rationals is return Fraction is begin return Reduce - (Left * Right.Num, + (Utils.To_Big_Unsigned (Word (Left)) * Right.Num, Right.Den); end "*"; @@ -149,7 +150,7 @@ package body Rationals is begin return Reduce (Left.Num, - Left.Den * Right); + Left.Den * Utils.To_Big_Unsigned (Word (Right))); end "/"; function "/" @@ -159,14 +160,14 @@ package body Rationals is begin return Reduce (Right.Num, - Left * Right.Den); + Utils.To_Big_Unsigned (Word (Left)) * Right.Den); end "/"; function "/" (Left, Right : in Integer) return Fraction is begin - return Reduce (Left, Right); + return Reduce (Utils.To_Big_Unsigned (Word (Left)), Utils.To_Big_Unsigned (Word (Right))); end "/"; @@ -185,7 +186,7 @@ package body Rationals is Right : in Integer) return Boolean is begin - return Left.Num = Right and Left.Den = 1; + return Left.Num = Utils.To_Big_Unsigned (Word (Right)) and Left.Den = 1; end "="; function "=" @@ -193,7 +194,7 @@ package body Rationals is Right : in Fraction) return Boolean is begin - return Left = Right.Num and Right.Den = 1; + return Utils.To_Big_Unsigned (Word (Left)) = Right.Num and Right.Den = 1; end "="; @@ -211,7 +212,7 @@ package body Rationals is Right : in Integer) return Boolean is begin - return Left.Num <= Left.Den * Right; + return Left.Num <= Left.Den * Utils.To_Big_Unsigned (Word (Right)); end "<="; function "<=" @@ -219,7 +220,7 @@ package body Rationals is Right : in Fraction) return Boolean is begin - return Left * Right.Den <= Right.Num; + return Utils.To_Big_Unsigned (Word (Left)) * Right.Den <= Right.Num; end "<="; @@ -237,7 +238,7 @@ package body Rationals is Right : in Integer) return Boolean is begin - return Left.Num < Left.Den * Right; + return Left.Num < Left.Den * Utils.To_Big_Unsigned (Word (Right)); end "<"; function "<" @@ -245,7 +246,7 @@ package body Rationals is Right : in Fraction) return Boolean is begin - return Left * Right.Den < Right.Num; + return Utils.To_Big_Unsigned (Word (Left)) * Right.Den < Right.Num; end "<"; @@ -263,7 +264,7 @@ package body Rationals is Right : in Integer) return Boolean is begin - return Left.Num >= Left.Den * Right; + return Left.Num >= Left.Den * Utils.To_Big_Unsigned (Word (Right)); end ">="; function ">=" @@ -271,7 +272,7 @@ package body Rationals is Right : in Fraction) return Boolean is begin - return Left * Right.Den >= Right.Num; + return Utils.To_Big_Unsigned (Word (Left)) * Right.Den >= Right.Num; end ">="; @@ -289,7 +290,7 @@ package body Rationals is Right : in Integer) return Boolean is begin - return Left.Num > Left.Den * Right; + return Left.Num > Left.Den * Utils.To_Big_Unsigned (Word (Right)); end ">"; function ">" @@ -297,7 +298,7 @@ package body Rationals is Right : in Fraction) return Boolean is begin - return Left * Right.Den > Right.Num; + return Utils.To_Big_Unsigned (Word (Left)) * Right.Den > Right.Num; end ">"; @@ -307,21 +308,21 @@ package body Rationals is (Item : in Fraction) return Integer is begin - return Item.Num; + return Integer (Utils.To_Words (Item.Num)(1)); end Numerator; function Denominator (Item : in Fraction) return Integer is begin - return Item.Den; + return Integer (Utils.To_Words (Item.Den)(1)); end Denominator; function Floor (Item : in Fraction) return Integer is begin - return Item.Num / Item.Den; + return Integer (Utils.To_Words (Item.Num / Item.Den)(0)); end Floor; function Ceiling @@ -329,9 +330,9 @@ package body Rationals is return Integer is begin if Item.Num mod Item.Den = 0 then - return Item.Num / Item.Den; + return Integer (Utils.To_Words (Item.Num / Item.Den)(1)); else - return 1 + Item.Num / Item.Den; + return 1 + Integer (Utils.To_Words (Item.Num / Item.Den)(1)); end if; end Ceiling; @@ -339,10 +340,10 @@ package body Rationals is (Item : in Fraction) return Integer is begin - if Item.Num mod Item.Den >= Standard."/" (Item.Den, 2) then - return 1 + Item.Num / Item.Den; + if Item.Num mod Item.Den >= Item.Den / 2 then + return 1 + Integer (Utils.To_Words (Item.Num / Item.Den)(1)); else - return Item.Num / Item.Den; + return Integer (Utils.To_Words (Item.Num / Item.Den)(1)); end if; end Round; @@ -351,13 +352,10 @@ package body Rationals is function Image (Item : in Fraction) - return String - is - use Ada.Strings; - use Ada.Strings.Fixed; + return String is begin - return Trim (Integer'Image (Item.Num), Left) & '/' & - Trim (Integer'Image (Item.Den), Left); + return Utils.To_String (Item.Num) & '/' & + Utils.To_String (Item.Den); end Image; function Value @@ -371,7 +369,7 @@ package body Rationals is S := Index (Item, "/"); A := Integer'Value (Item (Item'First .. S - 1)); B := Integer'Value (Item (S + 1 .. Item'Last)); - return Reduce (A, B); + return Reduce (Utils.To_Big_Unsigned (Word (A)), Utils.To_Big_Unsigned (Word (B))); end Value; -- cgit