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: 1767
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: 7313

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: 10240
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: 10240
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

Senior Member


Joined: 07 Feb 2009
Posts: 1009
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 COBOL Programming Sandpit? jodrisco COBOL Programming 6 Wed Apr 12, 2017 3:47 am
No new posts RULES(NOEVENPACK) in cobol jackzhang75 COBOL Programming 5 Wed Mar 29, 2017 12:47 am
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm
No new posts Execute TSO ACF commands from COBOL-I... supreethi.srid TSO/ISPF 0 Tue Feb 21, 2017 5:19 pm
No new posts Convert current to hex value gopurs DFSORT/ICETOOL 4 Wed Feb 15, 2017 5:40 am


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