Jump to content

Convert decimal number to rational: Difference between revisions

→‎{{header|Lua}}: added Lua solution
(Added Wren)
(→‎{{header|Lua}}: added Lua solution)
Line 1,410:
END.
</pre>
 
=={{header|Lua}}==
Brute force, and why not?
<lang lua>for _,v in ipairs({ 0.9054054, 0.518518, 0.75, math.pi }) do
local n, d, dmax, eps = 1, 1, 1e7, 1e-15
while math.abs(n/d-v)>eps and d<dmax do d=d+1 n=math.floor(v*d) end
print(string.format("%15.13f --> %d / %d", v, n, d))
end</lang>
{{out}}
<pre>0.9054054000000 --> 4527027 / 5000000
0.5185180000000 --> 259259 / 500000
0.7500000000000 --> 3 / 4
3.1415926535898 --> 31415926 / 10000000</pre>
 
=={{header|Maple}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.