Skip to content

Commit

Permalink
Added Create(NumBits, TRandom32Proc). No need for an IRandom, or Velt…
Browse files Browse the repository at this point in the history
…huis.RandomNumbers.
  • Loading branch information
rvelthuis committed Mar 23, 2019
1 parent 3ca54aa commit 7f8ad21
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 118 deletions.
121 changes: 11 additions & 110 deletions Source/Velthuis.BigRationals.pas
Original file line number Diff line number Diff line change
Expand Up @@ -447,116 +447,17 @@ constructor BigRational.Create(const Value: BigInteger);
end;

(*
https://rosettacode.org/wiki/Convert_decimal_number_to_rational#Ada
procedure Real_To_Rational (R: Real;
Bound: Positive;
Numerator: out Integer;
Denominator: out Positive) is
Error: Real;
Best: Positive := 1;
Best_Error: Real := Real'Last;
begin
if R = 0.0 then
Numerator := 0;
Denominator := 1;
return;
elsif R < 0.0 then
Real_To_Rational(-R, Bound, Numerator, Denominator);
Numerator := - Numerator;
return;
else
for I in 1 .. Bound loop
Error := abs(Real(I) * R - Real'Rounding(Real(I) * R));
if Error < Best_Error then
Best := I;
Best_Error := Error;
end if;
end loop;
end if;
Denominator := Best;
Numerator := Integer(Real'Rounding(Real(Denominator) * R));
end Real_To_Rational;
procedure RealToRational(R: Extended; Bound: Cardinal; out Numerator: Integer; out Denominator: Cardinal);
var
Error: Extended;
Best: Cardinal;
BestError: Extended;
I: Integer;
begin
Best := 1;
BestError := Math.MaxExtended;
if R = 0.0 then
begin
Numerator := 0;
Denominator := 1;
end
else if R < 0.0 then
begin
RealToRational(-R, Bound, Numerator, Denominator);
Numerator := -Numerator;
end
else
begin
for I := 1 to Bound do
begin
Error := Abs(I * R - Round(I * R)); // Abs(Frac(I * R));
if Error < BestError then
begin
Best := I;
BestError := Error;
end; // if
end; // for
end; // if
Denominator := Best;
Numerator := Round(Denominator * R);
end;
// --------------------------------------
with Ada.Text_IO; With Real_To_Rational;
procedure Convert_Decimal_To_Rational is
type My_Real is new Long_Float; -- change this for another "Real" type
package FIO is new Ada.Text_IO.Float_IO(My_Real);
procedure R2R is new Real_To_Rational(My_Real);
Nom, Denom: Integer;
R: My_Real;
begin
loop
Ada.Text_IO.New_Line;
FIO.Get(R);
FIO.Put(R, Fore => 2, Aft => 9, Exp => 0);
exit when R = 0.0;
for I in 0 .. 4 loop
R2R(R, 10**I, Nom, Denom);
Ada.Text_IO.Put(" " & Integer'Image(Nom) &
" /" & Integer'Image(Denom));
end loop;
end loop;
end Convert_Decimal_To_Rational;
// Output: -----------------------------
> ./convert_decimal_to_rational < input.txt
0.750000000 1 / 1 3 / 4 3 / 4 3 / 4 3 / 4
0.518518000 1 / 1 1 / 2 14 / 27 14 / 27 14 / 27
0.905405400 1 / 1 9 / 10 67 / 74 67 / 74 67 / 74
0.142857143 0 / 1 1 / 7 1 / 7 1 / 7 1 / 7
3.141592654 3 / 1 22 / 7 22 / 7 355 / 113 355 / 113
2.718281828 3 / 1 19 / 7 193 / 71 1457 / 536 25946 / 9545
-0.423310825 0 / 1 -3 / 7 -11 / 26 -69 / 163 -1253 / 2960
31.415926536 31 / 1 157 / 5 377 / 12 3550 / 113 208696 / 6643
0.000000000
Test with e.g.
0.750000000 1 / 1 3 / 4 3 / 4 3 / 4 3 / 4
0.518518000 1 / 1 1 / 2 14 / 27 14 / 27 14 / 27
0.905405400 1 / 1 9 / 10 67 / 74 67 / 74 67 / 74
0.142857143 0 / 1 1 / 7 1 / 7 1 / 7 1 / 7
3.141592654 3 / 1 22 / 7 22 / 7 355 / 113 355 / 113
2.718281828 3 / 1 19 / 7 193 / 71 1457 / 536 25946 / 9545
-0.423310825 0 / 1 -3 / 7 -11 / 26 -69 / 163 -1253 / 2960
31.415926536 31 / 1 157 / 5 377 / 12 3550 / 113 208696 / 6643
0.000000000
*)

constructor BigRational.Create(F: Double; MaxDenominator: Cardinal);
Expand Down
2 changes: 1 addition & 1 deletion Tests/BigDecimals/BigDecimalDevelopmentTests.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{DCFEB574-48AF-4E4B-B811-FB7EC60879F8}</ProjectGuid>
<ProjectVersion>18.5</ProjectVersion>
<ProjectVersion>18.6</ProjectVersion>
<FrameworkType>None</FrameworkType>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
Expand Down
2 changes: 1 addition & 1 deletion Tests/BigIntegers/BigIntegerDevelopmentTests.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{DA578521-538C-4077-A0D0-7EE7F73A19C6}</ProjectGuid>
<ProjectVersion>18.5</ProjectVersion>
<ProjectVersion>18.6</ProjectVersion>
<FrameworkType>VCL</FrameworkType>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
Expand Down
26 changes: 24 additions & 2 deletions Tests/BigIntegers/TestBigIntegers.pas
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ TTestBigInteger = class(TTestCase)
procedure TestParse;
procedure TestTryParse;
procedure TestCreateBytes;
procedure TestCreateRandom;
procedure TestCreateIRandom;
procedure TestCreateRandom32;
procedure TestCreateDouble;
procedure TestIsZero;
procedure TestIsPositive;
Expand Down Expand Up @@ -901,7 +902,7 @@ procedure TTestBigInteger.TestCreateDouble;
end;
end;

procedure TTestBigInteger.TestCreateRandom;
procedure TTestBigInteger.TestCreateIRandom;
var
I, NumBits: Integer;
ARandom: IRandom;
Expand All @@ -916,6 +917,27 @@ procedure TTestBigInteger.TestCreateRandom;
end;
end;

function Random32: UInt32;
begin
Result := UInt32(RandSeed);
end;

procedure TTestBigInteger.TestCreateRandom32;
var
I, NumBits: Integer;
Value: BigInteger;
Dummy: Integer;
begin
Randomize;
for I := 0 to 1000 do
begin
NumBits := I;
Value := BigInteger.Create(NumBits, Random32);
Dummy := Random(MaxInt); // next random value
Check(Value.BitLength <= NumBits, Format('%s (bits = %d), Numbits = %d', [Value.ToString(16), Value.BitLength, NumBits]));
end;
end;

procedure TTestBigInteger.TestLogicalNot;
var
I: Integer;
Expand Down
2 changes: 1 addition & 1 deletion Tests/BigRationals/BigRationalDevelopmentTests.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{BCA851BB-A137-43FC-BCB8-1A8B89013254}</ProjectGuid>
<ProjectVersion>18.5</ProjectVersion>
<ProjectVersion>18.6</ProjectVersion>
<FrameworkType>None</FrameworkType>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
Expand Down
2 changes: 1 addition & 1 deletion Visualizers/BigNumVisualizers.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Package</AppType>
<FrameworkType>None</FrameworkType>
<ProjectVersion>18.5</ProjectVersion>
<ProjectVersion>18.6</ProjectVersion>
<Platform Condition="'$(Platform)'==''">Win32</Platform>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
Expand Down
2 changes: 1 addition & 1 deletion Visualizers/BigNumberVisualizers.dproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{82F7D428-1B82-4DC7-A2A3-E6FC570D13D4}</ProjectGuid>
<ProjectVersion>18.5</ProjectVersion>
<ProjectVersion>18.6</ProjectVersion>
<FrameworkType>None</FrameworkType>
<MainSource>BigNumberVisualizers.dpr</MainSource>
<Base>True</Base>
Expand Down
2 changes: 1 addition & 1 deletion Visualizers/BigNumbers.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<TargetedPlatforms>1</TargetedPlatforms>
<AppType>Package</AppType>
<FrameworkType>None</FrameworkType>
<ProjectVersion>18.5</ProjectVersion>
<ProjectVersion>18.6</ProjectVersion>
<Platform Condition="'$(Platform)'==''">Win32</Platform>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
Expand Down

0 comments on commit 7f8ad21

Please sign in to comment.