Convert decimal number to rational: Difference between revisions

Use the function fraction
m (→‎{{header|TI SR-56}}: Terminology)
(Use the function fraction)
Line 2,807:
=={{header|Seed7}}==
The library [http://seed7.sourceforge.net/libraries/bigrat.htm bigrat.s7i]
defines the operatorfunction [httphttps://seed7.sourceforge.net/libraries/bigrat.htm#%28attr_bigRational%29parse%28in_var_string%29bigRational(in_var_string) parsebigRational],
which creates a [https://seed7.sourceforge.net/manual/types.htm#bigRational bigRational] number from a string.
whichThis function accepts, besides fractions, also a decimal number with repeating decimals.
Internally a bigRational uses numerator and denominator to represent a rational number.
Writing a bigRational does not create a fraction with numerator and denominator, but a decimal number with possibly repeating decimals.
The function [https://seed7.sourceforge.net/libraries/bigrat.htm#fraction(in_bigRational) fraction]
uses numerator and denominator from the bigRational number to get a string with the fraction.
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigrat.s7i";
Line 2,814 ⟶ 2,819:
const proc: main is func
begin
writeln(fraction(bigRational parse ("0.9(054)")));
writeln(fraction(bigRational parse ("0.(518)")));
writeln(fraction(bigRational parse ("0.75")));
writeln(fraction(bigRational parse ("3.(142857)")));
writeln(fraction(bigRational parse ("0.(8867924528301)")));
writeln(fraction(bigRational parse ("0.(846153)")));
writeln(fraction(bigRational parse ("0.9054054")));
writeln(fraction(bigRational parse ("0.518518")));
writeln(fraction(bigRational parse ("0.14285714285714")));
writeln(fraction(bigRational parse ("3.14159265358979")));
writeln(fraction(bigRational parse ("2.718281828")));
writeln(fraction(bigRational parse ("31.415926536")));
writeln(fraction(bigRational parse ("0.000000000")));
end func;</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>67/74
29

edits