Convert decimal number to rational: Difference between revisions

m
→‎{{header|AppleScript}}: Tidied, updated output.
(add fermat)
m (→‎{{header|AppleScript}}: Tidied, updated output.)
Line 183:
 
=={{header|AppleScript}}==
<lang applescript>on--------- runRATIONAL APPROXIMATION TO DECIMAL NUMBER -------
script ratioString
-- Using a tolerance epsilon of 1/10000
on |λ|(x)
showRatio(approxRatio(1.0E-4, x))
end |λ|
end script
map(ratioString, ¬
{0.9054054, 0.518518, 0.75})
--> {"67/74", "14/27", "3/4"}
end run
 
 
-- approxRatio :: Real -> Real -> Ratio
on approxRatio(epsilon, n)
if {real, integer} contains (class of epsilon) and 0 < epsilon then
-- Given
set e to epsilon
else
-- Default
set e to 1 / 10000
end if
Line 239 ⟶ 227:
 
 
-- GENERIC FUNCTIONS ------------------------- TEST -------------------------
on run
script ratioString
-- Using a tolerance epsilon of 1/10000
on |λ|(x)
(x as string) & " -> " & showRatio(approxRatio(1.0E-4, x))
end |λ|
end script
unlines(map(ratioString, ¬
{0.9054054, 0.518518, 0.75}))
-- 0.9054054 -> 67/74
-- 0.518518 -> 14/27
-- 0.75 -> 3/4
end run
 
 
-------------------- GENERIC FUNCTIONS -------------------
 
-- abs :: Num -> Num
Line 262 ⟶ 268:
end if
end mReturn
 
 
-- map :: (a -> b) -> [a] -> [b]
Line 273 ⟶ 280:
return lst
end tell
end map</lang>
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines</lang>
{{Out}}
<pre>{"0.9054054 -> 67/74", "14/27", "3/4"}</pre>
0.518518 -> 14/27
0.75 -> 3/4</pre>
 
=={{header|AutoHotkey}}==
9,655

edits