summaryrefslogtreecommitdiff
path: root/src/rationals.adb
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2017-02-13 18:27:13 +1100
committerJed Barber <jjbarber@y7mail.com>2017-02-13 18:27:13 +1100
commit2b8b55de4a18757e8d6769e458c84f7c1df1e261 (patch)
treecbd62219babccc04e57fa7708f88385a7f6413d3 /src/rationals.adb
parent2b842cb65ce29071d5786bdecc834c026d1f2db2 (diff)
Swapped out crypto package for something smaller, revised other code and readme/notes slightly
Diffstat (limited to 'src/rationals.adb')
-rw-r--r--src/rationals.adb85
1 files changed, 45 insertions, 40 deletions
diff --git a/src/rationals.adb b/src/rationals.adb
index 62fe9b9..bc54106 100644
--- a/src/rationals.adb
+++ b/src/rationals.adb
@@ -1,28 +1,34 @@
with Ada.Strings.Fixed;
+with Multi_Precision_Integers.IO;
+use Multi_Precision_Integers.IO;
package body Rationals is
function Reduce
- (Numerator, Denominator : in Big_Unsigned)
+ (Numerator, Denominator : in Multi_Int)
return Fraction
is
- A : Big_Unsigned := Numerator;
- B : Big_Unsigned := Denominator;
- Temp : Big_Unsigned;
+ A, B, Temp : Multi_Int (M_Size);
+ Ret_N, Ret_D : Multi_Int (M_Size);
begin
+ Fill (A, Numerator);
+ Fill (B, Denominator);
+
-- Euclid's algorithm
loop
Temp := A;
A := B;
- B := Temp mod B;
- exit when B = 0;
+ Fill (B, Temp mod B);
+ exit when Equal (B, 0);
end loop;
- return (Num => Numerator / A,
- Den => Denominator / A);
+
+ Fill (Ret_N, Numerator / A);
+ Fill (Ret_D, Denominator / A);
+ return (Num => Ret_N, Den => Ret_D);
end Reduce;
@@ -43,7 +49,7 @@ package body Rationals is
return Fraction is
begin
return Reduce
- (Left.Num + Left.Den * Utils.To_Big_Unsigned (Word (Right)),
+ (Left.Num + Left.Den * Multi (Right),
Left.Den);
end "+";
@@ -53,7 +59,7 @@ package body Rationals is
return Fraction is
begin
return Reduce
- (Utils.To_Big_Unsigned (Word (Left)) * Right.Den + Right.Num,
+ (Multi (Left) * Right.Den + Right.Num,
Right.Den);
end "+";
@@ -75,7 +81,7 @@ package body Rationals is
return Fraction is
begin
return Reduce
- (Left.Num - Left.Den * Utils.To_Big_Unsigned (Word (Right)),
+ (Left.Num - Left.Den * Multi (Right),
Left.Den);
end "-";
@@ -85,7 +91,7 @@ package body Rationals is
return Fraction is
begin
return Reduce
- (Utils.To_Big_Unsigned (Word (Left)) * Right.Den - Right.Num,
+ (Multi (Left) * Right.Den - Right.Num,
Right.Den);
end "-";
@@ -117,7 +123,7 @@ package body Rationals is
return Fraction is
begin
return Reduce
- (Left.Num * Utils.To_Big_Unsigned (Word (Right)),
+ (Left.Num * Multi (Right),
Left.Den);
end "*";
@@ -127,7 +133,7 @@ package body Rationals is
return Fraction is
begin
return Reduce
- (Utils.To_Big_Unsigned (Word (Left)) * Right.Num,
+ (Multi (Left) * Right.Num,
Right.Den);
end "*";
@@ -150,7 +156,7 @@ package body Rationals is
begin
return Reduce
(Left.Num,
- Left.Den * Utils.To_Big_Unsigned (Word (Right)));
+ Left.Den * Multi (Right));
end "/";
function "/"
@@ -160,14 +166,14 @@ package body Rationals is
begin
return Reduce
(Right.Num,
- Utils.To_Big_Unsigned (Word (Left)) * Right.Den);
+ Multi (Left) * Right.Den);
end "/";
function "/"
(Left, Right : in Integer)
return Fraction is
begin
- return Reduce (Utils.To_Big_Unsigned (Word (Left)), Utils.To_Big_Unsigned (Word (Right)));
+ return Reduce (Multi (Left), Multi (Right));
end "/";
@@ -177,8 +183,8 @@ package body Rationals is
(Left, Right : in Fraction)
return Boolean is
begin
- return Left.Num = Right.Num and
- Left.Den = Right.Den;
+ return Equal (Left.Num, Right.Num) and
+ Equal (Left.Den, Right.Den);
end "=";
function "="
@@ -186,7 +192,7 @@ package body Rationals is
Right : in Integer)
return Boolean is
begin
- return Left.Num = Utils.To_Big_Unsigned (Word (Right)) and Left.Den = 1;
+ return Equal (Left.Num, Right) and Equal (Left.Den, 1);
end "=";
function "="
@@ -194,7 +200,7 @@ package body Rationals is
Right : in Fraction)
return Boolean is
begin
- return Utils.To_Big_Unsigned (Word (Left)) = Right.Num and Right.Den = 1;
+ return Equal (Right.Num, Left) and Equal (Right.Den, 1);
end "=";
@@ -212,7 +218,7 @@ package body Rationals is
Right : in Integer)
return Boolean is
begin
- return Left.Num <= Left.Den * Utils.To_Big_Unsigned (Word (Right));
+ return Left.Num <= Left.Den * Multi (Right);
end "<=";
function "<="
@@ -220,7 +226,7 @@ package body Rationals is
Right : in Fraction)
return Boolean is
begin
- return Utils.To_Big_Unsigned (Word (Left)) * Right.Den <= Right.Num;
+ return Multi (Left) * Right.Den <= Right.Num;
end "<=";
@@ -238,7 +244,7 @@ package body Rationals is
Right : in Integer)
return Boolean is
begin
- return Left.Num < Left.Den * Utils.To_Big_Unsigned (Word (Right));
+ return Left.Num < Left.Den * Multi (Right);
end "<";
function "<"
@@ -246,7 +252,7 @@ package body Rationals is
Right : in Fraction)
return Boolean is
begin
- return Utils.To_Big_Unsigned (Word (Left)) * Right.Den < Right.Num;
+ return Multi (Left) * Right.Den < Right.Num;
end "<";
@@ -264,7 +270,7 @@ package body Rationals is
Right : in Integer)
return Boolean is
begin
- return Left.Num >= Left.Den * Utils.To_Big_Unsigned (Word (Right));
+ return Left.Num >= Left.Den * Multi (Right);
end ">=";
function ">="
@@ -272,7 +278,7 @@ package body Rationals is
Right : in Fraction)
return Boolean is
begin
- return Utils.To_Big_Unsigned (Word (Left)) * Right.Den >= Right.Num;
+ return Multi (Left) * Right.Den >= Right.Num;
end ">=";
@@ -290,7 +296,7 @@ package body Rationals is
Right : in Integer)
return Boolean is
begin
- return Left.Num > Left.Den * Utils.To_Big_Unsigned (Word (Right));
+ return Left.Num > Left.Den * Multi (Right);
end ">";
function ">"
@@ -298,7 +304,7 @@ package body Rationals is
Right : in Fraction)
return Boolean is
begin
- return Utils.To_Big_Unsigned (Word (Left)) * Right.Den > Right.Num;
+ return Multi (Left) * Right.Den > Right.Num;
end ">";
@@ -308,31 +314,31 @@ package body Rationals is
(Item : in Fraction)
return Integer is
begin
- return Integer (Utils.To_Words (Item.Num)(1));
+ return Basic (Item.Num);
end Numerator;
function Denominator
(Item : in Fraction)
return Integer is
begin
- return Integer (Utils.To_Words (Item.Den)(1));
+ return Basic (Item.Den);
end Denominator;
function Floor
(Item : in Fraction)
return Integer is
begin
- return Integer (Utils.To_Words (Item.Num / Item.Den)(0));
+ return Basic (Item.Num / Item.Den);
end Floor;
function Ceiling
(Item : in Fraction)
return Integer is
begin
- if Item.Num mod Item.Den = 0 then
- return Integer (Utils.To_Words (Item.Num / Item.Den)(1));
+ if Equal (Item.Num mod Item.Den, 0) then
+ return Basic (Item.Num / Item.Den);
else
- return 1 + Integer (Utils.To_Words (Item.Num / Item.Den)(1));
+ return 1 + Basic (Item.Num / Item.Den);
end if;
end Ceiling;
@@ -341,9 +347,9 @@ package body Rationals is
return Integer is
begin
if Item.Num mod Item.Den >= Item.Den / 2 then
- return 1 + Integer (Utils.To_Words (Item.Num / Item.Den)(1));
+ return 1 + Basic (Item.Num / Item.Den);
else
- return Integer (Utils.To_Words (Item.Num / Item.Den)(1));
+ return Basic (Item.Num / Item.Den);
end if;
end Round;
@@ -354,8 +360,7 @@ package body Rationals is
(Item : in Fraction)
return String is
begin
- return Utils.To_String (Item.Num) & '/' &
- Utils.To_String (Item.Den);
+ return Str (Item.Num) & '/' & Str (Item.Den);
end Image;
function Value
@@ -369,7 +374,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 (Utils.To_Big_Unsigned (Word (A)), Utils.To_Big_Unsigned (Word (B)));
+ return Reduce (Multi (A), Multi (B));
end Value;