@@ -447,116 +447,17 @@ constructor BigRational.Create(const Value: BigInteger);
447
447
end ;
448
448
449
449
(*
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
560
461
*)
561
462
562
463
constructor BigRational.Create(F: Double; MaxDenominator: Cardinal);
0 commit comments