IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Need assistance to Convert Decimal to fraction in cobol


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
SENTHIL MURUGAAN
Warnings : 1

New User


Joined: 12 Jan 2013
Posts: 32
Location: India

PostPosted: Fri Oct 16, 2015 12:57 pm
Reply with quote

Hi,

Need help to convert decimal value to fraction in cobol.
Input File only holds only the below values file Record Length=80,FB
Input File:
1.3333
0.25

The output file should hold in below format
Output File:
4/3
1/4

Is this possible in cobol?

Thanks,
Senthil
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Fri Oct 16, 2015 4:31 pm
Reply with quote

If you have a method for defining the divisor then yes.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Fri Oct 16, 2015 4:54 pm
Reply with quote

You'd better post some more representative examples. Are you looking for things like 90/89? Or will they all be thirds, and quarters and fifths, and halves?
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Fri Oct 16, 2015 5:35 pm
Reply with quote

for non periodic numbers the algorithm is pretty simple ...

Code:
#! /usr/bin/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

/*  error handlers
*/

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")"' "
say "++"copies(" -",35)
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

Code:

#! /usr/bin/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

/*  error handlers
*/

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")"' "
say "++"copies(" -",35)
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.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Sat Oct 17, 2015 2:36 am
Reply with quote

a bit of explanation of the algorithms ...

for normal ( non periodic )

just find out how many decimal digits
1.2345 ==> 4 decimal digits
12345/10000
and simplify the fraction

for a periodic number things are a bit trickier
to find the period I wrote a the LRS ( longest repeated substring )subroutine

after that
the fraction for the period is
numerator the period,
denominator as may nines as the period length
0.33333333
3/9 ==> 1/3

if there is an integer part
period/(9....9) + integer_part
and after that simplify the fraction

1.333333
3/9 + 1 ==> (3+9)/9 =>> 4/3
Back to top
View user's profile Send private message
prino

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Sat Oct 17, 2015 5:43 am
Reply with quote

Is this a business requirement?

No, it's my homework...
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
Search our Forums:

Back to Top