Convert decimal number to rational: Difference between revisions

→‎{{header|Perl 6}}: Update to more closely match the task requirements
(→‎{{header|Factor}}: edit whitespace, show all imports, and simplify a bit)
(→‎{{header|Perl 6}}: Update to more closely match the task requirements)
Line 1,799:
 
=={{header|Perl 6}}==
Decimals are natively represented as rationals in Perl 6, so if the task does not need to handle repeating decimals, it is trivially handled by the <tt>.nude</tt> method, which returns the numerator and denominator:.
 
<lang perl6>say .nude.join('/') for 0.9054054, 0.518518, 0.75;</lang>
Cyclical repeating fractions require a bit more work; it's all built in, but the formatting is manual.
<lang perl6>for 3/4, 9/32, 67/74, 14/27, 7777/23456 -> $rational {
put "\n==== Fraction: {$rational.nude.join: '/'} ", '=' x (100 - $rational.perl.chars) ;
put "\nStandard stringification conversion of fraction to decimal: {$rational}";
put "\nHigh (configurable) precision stringification conversion of fraction to decimal " ~
"(configured for 130 significant places):\n{$rational.base(10, 130).subst(/0+$/, '')}";
my ($static, $cycle) = $rational.base-repeating; # defaults to base 10, pass in a different base if desired
$cycle = $cycle ?? "({$cycle})" !! '';
put "\nRepeating fraction stringification conversion of fraction to decimal " ~
"(repeating cycle enclosed in parenthesis):\n{$static}{$cycle}";
}</lang>
{{out}}
<pre>==== Fraction: 3/4 ================================================================================================
<pre>4527027/5000000
259259/500000
3/4</pre>
However, if we want to take repeating decimals into account, then we can get a bit fancier.
<lang perl6>sub decimal_to_fraction ( Str $n, Int $rep_digits = 0 ) returns Str {
my ( $int, $dec ) = ( $n ~~ /^ (\d+) \. (\d+) $/ )».Str or die;
 
Standard stringification conversion of fraction to decimal: 0.75
my ( $numer, $denom ) = ( $dec, 10 ** $dec.chars );
if $rep_digits {
my $to_move = $dec.chars - $rep_digits;
$numer -= $dec.substr(0, $to_move);
$denom -= 10 ** $to_move;
}
 
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
my $rat = Rat.new( $numer.Int, $denom.Int ).nude.join('/');
0.75
return $int > 0 ?? "$int $rat" !! $rat;
}
 
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
my @a = ['0.9054', 3], ['0.518', 3], ['0.75', 0], | (^4).map({['12.34567', $_]});
0.75
for @a -> [ $n, $d ] {
 
say "$n with $d repeating digits = ", decimal_to_fraction( $n, $d );
==== Fraction: 9/32 =============================================================================================
}</lang>
 
{{out}}
Standard stringification conversion of fraction to decimal: 0.28125
<pre>0.9054 with 3 repeating digits = 67/74
 
0.518 with 3 repeating digits = 14/27
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
0.75 with 0 repeating digits = 3/4
0.28125
12.34567 with 0 repeating digits = 12 34567/100000
 
12.34567 with 1 repeating digits = 12 31111/90000
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
12.34567 with 2 repeating digits = 12 17111/49500
0.28125
12.34567 with 3 repeating digits = 12 1279/3700</pre>
 
==== Fraction: 67/74 =============================================================================================
 
Standard stringification conversion of fraction to decimal: 0.905405
 
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
0.9054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054054
 
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
0.9(054)
 
==== Fraction: 14/27 =============================================================================================
 
Standard stringification conversion of fraction to decimal: 0.518519
 
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
0.5185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185185
 
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
0.(518)
 
==== Fraction: 7777/23456 ========================================================================================
 
Standard stringification conversion of fraction to decimal: 0.331557
 
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
0.3315569577080491132332878581173260572987721691678035470668485675306957708049113233287858117326057298772169167803547066848567530696
 
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
0.33155(6957708049113233287858117326057298772169167803547066848567530)</pre>
 
=={{header|Phix}}==
10,327

edits