|
View previous topic :: View next topic
|
| Author |
Message |
SENTHIL MURUGAAN Warnings : 1 New User
Joined: 12 Jan 2013 Posts: 32 Location: India
|
|
|
|
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 |
|
 |
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2454 Location: Hampshire, UK
|
|
|
|
| If you have a method for defining the divisor then yes. |
|
| Back to top |
|
 |
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
| 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 |
|
 |
enrico-sorichetti
Superior Member

Joined: 14 Mar 2007 Posts: 10903 Location: italy
|
|
|
|
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 |
|
 |
enrico-sorichetti
Superior Member

Joined: 14 Mar 2007 Posts: 10903 Location: italy
|
|
|
|
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 |
|
 |
prino
Senior Member

Joined: 07 Feb 2009 Posts: 1325 Location: Vilnius, Lithuania
|
|
|
|
Is this a business requirement?
No, it's my homework... |
|
| Back to top |
|
 |
|
|
 |
All times are GMT + 6 Hours |
|