Convert decimal number to rational: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: Update to more closely match the task requirements)
m (Reverted edits by Thundergnat (talk) to last revision by Chunes)
Line 1,799: Line 1,799:


=={{header|Perl 6}}==
=={{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.
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}}
{{out}}
<pre>4527027/5000000
<pre>==== Fraction: 3/4 ================================================================================================
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;


my ( $numer, $denom ) = ( $dec, 10 ** $dec.chars );
Standard stringification conversion of fraction to decimal: 0.75
if $rep_digits {
my $to_move = $dec.chars - $rep_digits;
$numer -= $dec.substr(0, $to_move);
$denom -= 10 ** $to_move;
}


my $rat = Rat.new( $numer.Int, $denom.Int ).nude.join('/');
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
return $int > 0 ?? "$int $rat" !! $rat;
0.75
}


my @a = ['0.9054', 3], ['0.518', 3], ['0.75', 0], | (^4).map({['12.34567', $_]});
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
for @a -> [ $n, $d ] {
0.75
say "$n with $d repeating digits = ", decimal_to_fraction( $n, $d );

}</lang>
==== Fraction: 9/32 =============================================================================================
{{out}}

<pre>0.9054 with 3 repeating digits = 67/74
Standard stringification conversion of fraction to decimal: 0.28125
0.518 with 3 repeating digits = 14/27

0.75 with 0 repeating digits = 3/4
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
12.34567 with 0 repeating digits = 12 34567/100000
0.28125
12.34567 with 1 repeating digits = 12 31111/90000

12.34567 with 2 repeating digits = 12 17111/49500
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
12.34567 with 3 repeating digits = 12 1279/3700</pre>
0.28125

==== 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}}==
=={{header|Phix}}==