Convert decimal number to rational: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Sidef}}: updated code + rational approximations)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 105:
0.000000000
</pre>
 
=={{header|AppleScript}}==
<lang applescript>on run
Line 564 ⟶ 565:
3.14159265358979 → 104348 / 33215
2.71828182845905 → 49171 / 18089</pre>
 
 
 
=={{header|Clojure}}==
Line 696 ⟶ 695:
3/4
</pre>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Convert_decimal_number_to_rational this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Forth}}==
Line 743 ⟶ 734:
0.9054054e 100 RealToRational swap . . 67 74
</lang>
 
 
=={{header|Fortran}}==
Line 1,040 ⟶ 1,030:
print n & ":", parserational(n)
loop</lang>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Convert_decimal_number_to_rational this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Go}}==
Line 1,797 ⟶ 1,795:
approx below 1e+09: -1881244168 / 598818617
approx below 1e+10: -9978066541 / 3176117225</pre>
 
=={{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>
{{out}}
<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;
 
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;
}
 
my $rat = Rat.new( $numer.Int, $denom.Int ).nude.join('/');
return $int > 0 ?? "$int $rat" !! $rat;
 
my @a = ['0.9054', 3], ['0.518', 3], ['0.75', 0], | (^4).map({['12.34567', $_]});
for @a -> [ $n, $d ] {
say "$n with $d repeating digits = ", decimal_to_fraction( $n, $d );
}</lang>
{{out}}
<pre>0.9054 with 3 repeating digits = 67/74
0.518 with 3 repeating digits = 14/27
0.75 with 0 repeating digits = 3/4
12.34567 with 0 repeating digits = 12 34567/100000
12.34567 with 1 repeating digits = 12 31111/90000
12.34567 with 2 repeating digits = 12 17111/49500
12.34567 with 3 repeating digits = 12 1279/3700</pre>
 
=={{header|Phix}}==
Line 2,179 ⟶ 2,142:
(exact->inexact 67/74) ; -> 0.9054054054054054
(inexact->exact 0.9054054054054054) ;-> 8155166892806033/9007199254740992</lang>
 
=={{header|Perl 6Raku}}==
(formerly 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>
{{out}}
<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;
 
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;
}
 
my $rat = Rat.new( $numer.Int, $denom.Int ).nude.join('/');
return $int > 0 ?? "$int $rat" !! $rat;
 
my @a = ['0.9054', 3], ['0.518', 3], ['0.75', 0], | (^4).map({['12.34567', $_]});
for @a -> [ $n, $d ] {
say "$n with $d repeating digits = ", decimal_to_fraction( $n, $d );
}</lang>
{{out}}
<pre>0.9054 with 3 repeating digits = 67/74
0.518 with 3 repeating digits = 14/27
0.75 with 0 repeating digits = 3/4
12.34567 with 0 repeating digits = 12 34567/100000
12.34567 with 1 repeating digits = 12 31111/90000
12.34567 with 2 repeating digits = 12 17111/49500
12.34567 with 3 repeating digits = 12 1279/3700</pre>
 
=={{header|REXX}}==
10,327

edits