Page 1 of 1

how to find the fractional representation of a decimal number

Posted: Mon Oct 03, 2016 3:34 pm
by enrico-sorichetti
for non periodic numbers the algorithm is pretty simple ...

Code: Select all

#! /usr/bin/env rexx
/* REXX
*/

Trace "O"
numeric digits 32
signal on novalue

f.1 = 1.25
f.2 = 0.25
f.0 = 2

do f = 1 to f.0

    parse var f.f i "." d
    p = length(d)
    n = i || d
    d = 10 ** p

    rn = n / gcd(n,d)
    rd = d / gcd(n,d)

    say "numerator="rn", denominator="rd
end

exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
logic_error:
say "++"copies(" -",35)
say "++ Logic error at line '"sigl"' "
say "++"copies(" -",35)
exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
novalue:
say "++"copies(" -",35)
say "++ Novalue trapped, line '"sigl"' var '"condition("D")"' "
exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
gcd: procedure
    parse arg x, y
    if  y > x then do
        r = x; x = y; y = r
    end

    do  until r = 0
        r = x // y; x = y; y = r
    end

    return x

for a periodic number ( OOREXX ONLY )

Code: Select all

#! /usr/bin/env rexx
/* REXX
*/

Trace "O"
numeric digits 32
signal on novalue

n = 1.333333333
parse var n i "."  f
p = lrs(f)

say n i f p

say length(f)
say length(p)

d   = copies("9", length(p) )

n   = i*d + p

rn = n / gcd(n,d)
rd = d / gcd(n,d)

say "numerator="rn", denominator="rd

exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
logic_error:
say "++"copies(" -",35)
say "++ Logic error at line '"sigl"' "
exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
novalue:
say "++"copies(" -",35)
say "++ Novalue trapped, line '"sigl"' var '"condition("D")"' "
exit

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
lrs:procedure
    use strict arg s
    m = s~length()
    k   = 0
    L   = .array~new
    r   = ""
    do  i = 1 to m
        do  j = i + 1 to m
            if substr(s,i,1) = substr(s,j,1) then do
                if  i = 1 then ,
                    L[i,j] = 1
                else do
                    if  j-i > L[i-1,j-1] then do
                        L[i,j]  = L[i-1,j-1] + 1
                        if  L[i,j] > k then do
                            k   = L[i,j]
                            r   = substr(s,i-k+1,k)
                        end
                    end
                    else do
                        L[i,j]  = 0
                    end
                end
            end
            else do
                L[i,j]  = 0
            end

        end /* do  j = i + 1 to m */
    end /* do  i = 1 to m */
    return r

/*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
gcd: procedure
    parse arg x, y
    if  y > x then do
        r = x; x = y; y = r
    end

    do  until r = 0
        r = x // y
        x = y
        y = r
    end

    say x y r

    return x

quick and dirty just to show the algorithms.

Re: how to find the fractional representation of a decimal number

Posted: Tue Oct 04, 2016 12:38 pm
by Anuj Dhawan
Thanks for the code Enrico. They are available under Download too now: downloadsystemcat?id=3