How to resolve the algorithm Convert decimal number to rational step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Convert decimal number to rational step by step in the AppleScript programming language

Table of Contents

Problem Statement

The task is to write a program to transform a decimal number into a fraction in lowest terms. It is not always possible to do this exactly. For instance, while rational numbers can be converted to decimal representation, some of them need an infinite number of digits to be represented exactly in decimal form. Namely, repeating decimals such as 1/3 = 0.333... Because of this, the following fractions cannot be obtained (reliably) unless the language has some way of representing repeating decimals: Acceptable output: Finite decimals are of course no problem:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Convert decimal number to rational step by step in the AppleScript programming language

Source code in the applescript programming language

--------- RATIONAL APPROXIMATION TO DECIMAL NUMBER -------

-- 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
    
    script gcde
        on |λ|(e, x, y)
            script _gcd
                on |λ|(a, b)
                    if b < e then
                        a
                    else
                        |λ|(b, a mod b)
                    end if
                end |λ|
            end script
            |λ|(abs(x), abs(y)) of _gcd
        end |λ|
    end script
    
    set c to |λ|(e, 1, n) of gcde
    Ratio((n div c), (1 div c))
end approxRatio


-- Ratio :: Int -> Int -> Ratio
on Ratio(n, d)
    {type:"Ratio", n:n, d:d}
end Ratio


-- showRatio :: Ratio -> String
on showRatio(r)
    (n of r as string) & "/" & (d of r as string)
end showRatio


--------------------------- 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
on abs(x)
    if 0 > x then
        -x
    else
        x
    end if
end abs


-- Lift 2nd class handler function into 1st class script wrapper 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
    if class of f is script then
        f
    else
        script
            property |λ| : f
        end script
    end if
end mReturn


-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
    tell mReturn(f)
        set lng to length of xs
        set lst to {}
        repeat with i from 1 to lng
            set end of lst to |λ|(item i of xs, i, xs)
        end repeat
        return lst
    end tell
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
    s
end unlines


  

You may also check:How to resolve the algorithm Formal power series step by step in the Lua programming language
You may also check:How to resolve the algorithm Identity matrix step by step in the ERRE programming language
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the Factor programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Leap year step by step in the Smalltalk programming language