how to find the fractional representation of a decimal number

No Language here - just algorithms!
Previous topicNext topic

Topic Author
enrico-sorichetti
Global Moderator
Global Moderator
Posts: 881
Joined: Wed Sep 11, 2013 3:57 pm

how to find the fractional representation of a decimal number

Post by enrico-sorichetti » Mon Oct 03, 2016 3:34 pm

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.


cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort 8-)

User avatar

Anuj Dhawan
Founder
Posts: 2612
Joined: Sun Apr 21, 2013 7:40 pm
Location: Mumbai, India
Zodiac: Sagittarius

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

Post by Anuj Dhawan » Tue Oct 04, 2016 12:38 pm

Thanks for the code Enrico. They are available under Download too now: downloadsystemcat?id=3


Thanks,
Anuj

Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.

Previous topicNext topic

Return to “Programming Algorithms.”