Convert decimal number to rational: Difference between revisions
Content added Content deleted
(add fermat) |
m (→{{header|AppleScript}}: Tidied, updated output.) |
||
Line 183: | Line 183: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<lang applescript> |
<lang applescript>--------- RATIONAL APPROXIMATION TO DECIMAL NUMBER ------- |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
--> {"67/74", "14/27", "3/4"} |
|||
⚫ | |||
-- approxRatio :: Real -> Real -> Ratio |
-- approxRatio :: Real -> Real -> Ratio |
||
on approxRatio(epsilon, n) |
on approxRatio(epsilon, n) |
||
if {real, integer} contains (class of epsilon) and 0 < epsilon then |
if {real, integer} contains (class of epsilon) and 0 < epsilon then |
||
-- Given |
|||
set e to epsilon |
set e to epsilon |
||
else |
else |
||
-- Default |
|||
set e to 1 / 10000 |
set e to 1 / 10000 |
||
end if |
end if |
||
Line 239: | Line 227: | ||
-- |
--------------------------- TEST ------------------------- |
||
on run |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- 0.9054054 -> 67/74 |
|||
-- 0.518518 -> 14/27 |
|||
-- 0.75 -> 3/4 |
|||
⚫ | |||
-------------------- GENERIC FUNCTIONS ------------------- |
|||
-- abs :: Num -> Num |
-- abs :: Num -> Num |
||
Line 262: | Line 268: | ||
end if |
end if |
||
end mReturn |
end mReturn |
||
-- map :: (a -> b) -> [a] -> [b] |
-- map :: (a -> b) -> [a] -> [b] |
||
Line 273: | Line 280: | ||
return lst |
return lst |
||
end tell |
end tell |
||
end map |
end map |
||
-- 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 |
|||
⚫ | |||
end unlines</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre>0.9054054 -> 67/74 |
||
0.518518 -> 14/27 |
|||
0.75 -> 3/4</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |