Convert decimal number to rational: Difference between revisions
Convert decimal number to rational (view source)
Revision as of 22:55, 27 January 2020
, 4 years ago→{{header|Sidef}}: updated code + rational approximations
Thundergnat (talk | contribs) m (Reverted edits by Thundergnat (talk) to last revision by Chunes) |
(→{{header|Sidef}}: updated code + rational approximations) |
||
Line 2,600:
=={{header|Sidef}}==
By default, literal numbers are represented in rational form:
<lang ruby>
say 0.518518.as_frac #=> 259259/500000
say 0.9054054.as_frac #=> 4527027/5000000</lang>
Additionally, '''Num(str)''' can be used for parsing a decimal expansion into rational form:
<lang ruby>'0.9054054 0.518518 0.75'.split.each { |str|
say Num(str).as_frac
}</lang>
{{out}}
Line 2,616:
3/4
</pre>
For rational approximations, the Number '''.rat_approx''' method can be used:
<lang ruby>say 0.518518.rat_approx.as_frac #=> 14/27
say 0.9054054.rat_approx.as_frac #=> 67/74</lang>
=={{header|Tcl}}==
|