Convert decimal number to rational: Difference between revisions
Convert decimal number to rational (view source)
Revision as of 18:58, 27 January 2020
, 4 years ago→{{header|Perl 6}}: Update to more closely match the task requirements
(→{{header|Factor}}: edit whitespace, show all imports, and simplify a bit) |
Thundergnat (talk | contribs) (→{{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
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 ================================================================================================
Standard stringification conversion of fraction to decimal: 0.75
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
0.75
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
0.75
==== Fraction: 9/32 =============================================================================================
▲}</lang>
Standard stringification conversion of fraction to decimal: 0.28125
High (configurable) precision stringification conversion of fraction to decimal (configured for 130 significant places):
0.28125
Repeating fraction stringification conversion of fraction to decimal (repeating cycle enclosed in parenthesis):
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}}==
|