Convert decimal number to rational: Difference between revisions

Added Delphi example
(→‎{{header|Lua}}: added Lua solution)
(Added Delphi example)
Line 652:
-0.423310825 0/1 -3/7 -11/26 -69/163 -1253/2960 -10093/23843
31.415926536 31/1 157/5 377/12 3550/113 208696/6643 2918194/92889</pre>
=={{header|Delphi}}==
{{libheader| Velthuis.BigRationals}}
{{libheader| Velthuis.BigDecimals}}
Thanks Rudy Velthuis for Velthuis.BigRationals and Velthuis.BigDecimals library[https://github.com/rvelthuis/DelphiBigNumbers].
{{Trans|Go}}
<lang Delphi>
program Convert_decimal_number_to_rational;
 
{$APPTYPE CONSOLE}
 
uses
Velthuis.BigRationals,
Velthuis.BigDecimals;
 
const
Tests: TArray<string> = ['0.9054054', '0.518518', '0.75'];
 
var
Rational: BigRational;
Decimal: BigDecimal;
 
begin
for var test in Tests do
begin
Decimal := test;
Rational := Decimal;
Writeln(test, ' = ', Rational.ToString);
end;
Readln;
end.</lang>
=={{header|EchoLisp}}==
The '''rationalize''' function uses a Stern-Brocot tree [http://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree] to find the best rational approximation of an inexact (floating point) number, for a given precision. The '''inexact->exact''' function returns a rational approximation for the default precision 0.0001 .
478

edits