Skip to content

Commit 7f8ad21

Browse files
committed
Added Create(NumBits, TRandom32Proc). No need for an IRandom, or Velthuis.RandomNumbers.
1 parent 3ca54aa commit 7f8ad21

8 files changed

+41
-118
lines changed

Source/Velthuis.BigRationals.pas

+11-110
Original file line numberDiff line numberDiff line change
@@ -447,116 +447,17 @@ constructor BigRational.Create(const Value: BigInteger);
447447
end;
448448

449449
(*
450-
https://rosettacode.org/wiki/Convert_decimal_number_to_rational#Ada
451-
452-
procedure Real_To_Rational (R: Real;
453-
Bound: Positive;
454-
Numerator: out Integer;
455-
Denominator: out Positive) is
456-
Error: Real;
457-
Best: Positive := 1;
458-
Best_Error: Real := Real'Last;
459-
begin
460-
if R = 0.0 then
461-
Numerator := 0;
462-
Denominator := 1;
463-
return;
464-
elsif R < 0.0 then
465-
Real_To_Rational(-R, Bound, Numerator, Denominator);
466-
Numerator := - Numerator;
467-
return;
468-
else
469-
for I in 1 .. Bound loop
470-
Error := abs(Real(I) * R - Real'Rounding(Real(I) * R));
471-
if Error < Best_Error then
472-
Best := I;
473-
Best_Error := Error;
474-
end if;
475-
end loop;
476-
end if;
477-
Denominator := Best;
478-
Numerator := Integer(Real'Rounding(Real(Denominator) * R));
479-
480-
end Real_To_Rational;
481-
482-
procedure RealToRational(R: Extended; Bound: Cardinal; out Numerator: Integer; out Denominator: Cardinal);
483-
var
484-
Error: Extended;
485-
Best: Cardinal;
486-
BestError: Extended;
487-
I: Integer;
488-
begin
489-
Best := 1;
490-
BestError := Math.MaxExtended;
491-
492-
if R = 0.0 then
493-
begin
494-
Numerator := 0;
495-
Denominator := 1;
496-
end
497-
else if R < 0.0 then
498-
begin
499-
RealToRational(-R, Bound, Numerator, Denominator);
500-
Numerator := -Numerator;
501-
end
502-
else
503-
begin
504-
for I := 1 to Bound do
505-
begin
506-
Error := Abs(I * R - Round(I * R)); // Abs(Frac(I * R));
507-
if Error < BestError then
508-
begin
509-
Best := I;
510-
BestError := Error;
511-
end; // if
512-
end; // for
513-
end; // if
514-
Denominator := Best;
515-
Numerator := Round(Denominator * R);
516-
end;
517-
518-
// --------------------------------------
519-
520-
with Ada.Text_IO; With Real_To_Rational;
521-
522-
procedure Convert_Decimal_To_Rational is
523-
524-
type My_Real is new Long_Float; -- change this for another "Real" type
525-
526-
package FIO is new Ada.Text_IO.Float_IO(My_Real);
527-
procedure R2R is new Real_To_Rational(My_Real);
528-
529-
Nom, Denom: Integer;
530-
R: My_Real;
531-
532-
begin
533-
loop
534-
Ada.Text_IO.New_Line;
535-
FIO.Get(R);
536-
FIO.Put(R, Fore => 2, Aft => 9, Exp => 0);
537-
exit when R = 0.0;
538-
for I in 0 .. 4 loop
539-
R2R(R, 10**I, Nom, Denom);
540-
Ada.Text_IO.Put(" " & Integer'Image(Nom) &
541-
" /" & Integer'Image(Denom));
542-
end loop;
543-
end loop;
544-
end Convert_Decimal_To_Rational;
545-
546-
// Output: -----------------------------
547-
548-
> ./convert_decimal_to_rational < input.txt
549-
550-
0.750000000 1 / 1 3 / 4 3 / 4 3 / 4 3 / 4
551-
0.518518000 1 / 1 1 / 2 14 / 27 14 / 27 14 / 27
552-
0.905405400 1 / 1 9 / 10 67 / 74 67 / 74 67 / 74
553-
0.142857143 0 / 1 1 / 7 1 / 7 1 / 7 1 / 7
554-
3.141592654 3 / 1 22 / 7 22 / 7 355 / 113 355 / 113
555-
2.718281828 3 / 1 19 / 7 193 / 71 1457 / 536 25946 / 9545
556-
-0.423310825 0 / 1 -3 / 7 -11 / 26 -69 / 163 -1253 / 2960
557-
31.415926536 31 / 1 157 / 5 377 / 12 3550 / 113 208696 / 6643
558-
0.000000000
559-
450+
Test with e.g.
451+
452+
0.750000000 1 / 1 3 / 4 3 / 4 3 / 4 3 / 4
453+
0.518518000 1 / 1 1 / 2 14 / 27 14 / 27 14 / 27
454+
0.905405400 1 / 1 9 / 10 67 / 74 67 / 74 67 / 74
455+
0.142857143 0 / 1 1 / 7 1 / 7 1 / 7 1 / 7
456+
3.141592654 3 / 1 22 / 7 22 / 7 355 / 113 355 / 113
457+
2.718281828 3 / 1 19 / 7 193 / 71 1457 / 536 25946 / 9545
458+
-0.423310825 0 / 1 -3 / 7 -11 / 26 -69 / 163 -1253 / 2960
459+
31.415926536 31 / 1 157 / 5 377 / 12 3550 / 113 208696 / 6643
460+
0.000000000
560461
*)
561462

562463
constructor BigRational.Create(F: Double; MaxDenominator: Cardinal);

Tests/BigDecimals/BigDecimalDevelopmentTests.dproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
33
<ProjectGuid>{DCFEB574-48AF-4E4B-B811-FB7EC60879F8}</ProjectGuid>
4-
<ProjectVersion>18.5</ProjectVersion>
4+
<ProjectVersion>18.6</ProjectVersion>
55
<FrameworkType>None</FrameworkType>
66
<Base>True</Base>
77
<Config Condition="'$(Config)'==''">Debug</Config>

Tests/BigIntegers/BigIntegerDevelopmentTests.dproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
33
<ProjectGuid>{DA578521-538C-4077-A0D0-7EE7F73A19C6}</ProjectGuid>
4-
<ProjectVersion>18.5</ProjectVersion>
4+
<ProjectVersion>18.6</ProjectVersion>
55
<FrameworkType>VCL</FrameworkType>
66
<Base>True</Base>
77
<Config Condition="'$(Config)'==''">Debug</Config>

Tests/BigIntegers/TestBigIntegers.pas

+24-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ TTestBigInteger = class(TTestCase)
4040
procedure TestParse;
4141
procedure TestTryParse;
4242
procedure TestCreateBytes;
43-
procedure TestCreateRandom;
43+
procedure TestCreateIRandom;
44+
procedure TestCreateRandom32;
4445
procedure TestCreateDouble;
4546
procedure TestIsZero;
4647
procedure TestIsPositive;
@@ -901,7 +902,7 @@ procedure TTestBigInteger.TestCreateDouble;
901902
end;
902903
end;
903904

904-
procedure TTestBigInteger.TestCreateRandom;
905+
procedure TTestBigInteger.TestCreateIRandom;
905906
var
906907
I, NumBits: Integer;
907908
ARandom: IRandom;
@@ -916,6 +917,27 @@ procedure TTestBigInteger.TestCreateRandom;
916917
end;
917918
end;
918919

920+
function Random32: UInt32;
921+
begin
922+
Result := UInt32(RandSeed);
923+
end;
924+
925+
procedure TTestBigInteger.TestCreateRandom32;
926+
var
927+
I, NumBits: Integer;
928+
Value: BigInteger;
929+
Dummy: Integer;
930+
begin
931+
Randomize;
932+
for I := 0 to 1000 do
933+
begin
934+
NumBits := I;
935+
Value := BigInteger.Create(NumBits, Random32);
936+
Dummy := Random(MaxInt); // next random value
937+
Check(Value.BitLength <= NumBits, Format('%s (bits = %d), Numbits = %d', [Value.ToString(16), Value.BitLength, NumBits]));
938+
end;
939+
end;
940+
919941
procedure TTestBigInteger.TestLogicalNot;
920942
var
921943
I: Integer;

Tests/BigRationals/BigRationalDevelopmentTests.dproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
33
<ProjectGuid>{BCA851BB-A137-43FC-BCB8-1A8B89013254}</ProjectGuid>
4-
<ProjectVersion>18.5</ProjectVersion>
4+
<ProjectVersion>18.6</ProjectVersion>
55
<FrameworkType>None</FrameworkType>
66
<Base>True</Base>
77
<Config Condition="'$(Config)'==''">Debug</Config>

Visualizers/BigNumVisualizers.dproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<TargetedPlatforms>1</TargetedPlatforms>
88
<AppType>Package</AppType>
99
<FrameworkType>None</FrameworkType>
10-
<ProjectVersion>18.5</ProjectVersion>
10+
<ProjectVersion>18.6</ProjectVersion>
1111
<Platform Condition="'$(Platform)'==''">Win32</Platform>
1212
</PropertyGroup>
1313
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">

Visualizers/BigNumberVisualizers.dproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
33
<ProjectGuid>{82F7D428-1B82-4DC7-A2A3-E6FC570D13D4}</ProjectGuid>
4-
<ProjectVersion>18.5</ProjectVersion>
4+
<ProjectVersion>18.6</ProjectVersion>
55
<FrameworkType>None</FrameworkType>
66
<MainSource>BigNumberVisualizers.dpr</MainSource>
77
<Base>True</Base>

Visualizers/BigNumbers.dproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<TargetedPlatforms>1</TargetedPlatforms>
88
<AppType>Package</AppType>
99
<FrameworkType>None</FrameworkType>
10-
<ProjectVersion>18.5</ProjectVersion>
10+
<ProjectVersion>18.6</ProjectVersion>
1111
<Platform Condition="'$(Platform)'==''">Win32</Platform>
1212
</PropertyGroup>
1313
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">

0 commit comments

Comments
 (0)