Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Need assistance to Convert Decimal to fraction in cobol

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
SENTHIL MURUGAAN
Warnings : 1

New User


Joined: 12 Jan 2013
Posts: 28
Location: India

PostPosted: Fri Oct 16, 2015 12:57 pm    Post subject: Need assistance to Convert Decimal to fraction in cobol
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: 1715
Location: UK

PostPosted: Fri Oct 16, 2015 4:31 pm    Post subject:
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

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7236

PostPosted: Fri Oct 16, 2015 4:54 pm    Post subject: Reply to: Need assistance to Convert Decimal to fraction in cobol
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Fri Oct 16, 2015 5:35 pm    Post subject: Reply to: Need assistance to Convert Decimal to fraction in cobol
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Sat Oct 17, 2015 2:36 am    Post subject: Reply to: Need assistance to Convert Decimal to fraction in cobol
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

Active Member


Joined: 07 Feb 2009
Posts: 984
Location: Oostende, Belgium

PostPosted: Sat Oct 17, 2015 5:43 am    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts How to convert the VBM file to VB or... Sulabh Agrawal JCL & VSAM 4 Fri Nov 18, 2016 1:04 pm
No new posts Convert +9999999999999.99 to S9(12)V9(3) vnktrrd DFSORT/ICETOOL 8 Thu Nov 17, 2016 8:15 pm
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm
This topic is locked: you cannot edit posts or make replies. RANDOM Function in COBOL swapnil781 COBOL Programming 2 Tue Nov 15, 2016 6:17 pm
No new posts convert PD to ZD ram_vizag DFSORT/ICETOOL 5 Sat Nov 05, 2016 3:06 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us