|
-
Feb 20th, 2003, 10:53 AM
#1
Thread Starter
Fanatic Member
Expressing A Repeating Decimal Number As A Fraction
Hi there!
Recently, someone asked me to come up with a small app that could simply add 2 fractions (with sums being inferior to 1) and display the result as a fraction as well (not in decimal form).
I thought this was going to be easy at first but I got stumped early on with this. Actually adding the 2 fractions is nothing but getting the result as a fraction is another matter.
For the most part I got it working using the function below which works well on decimal numbers without long repeating patterns, but on a fractions such as 1/7 with a decimal equivalent of 0.142857142857142857142857142857143 the function gracefully fails.
I guess what I need is a formula (preferably in VB) that would reduce those long repeating patterns within the numerator and getting the denominator value right before calling the function.
Again, as this sample all I'm trying to achieve is to come up with and display the 17/21 in my MessageBox:
1 2 17
-+-=-
7 3 21
Math was never my forte in school and I wish now that I would have paid a little more attention during my math class 
Anyway thanks in advance!
Here's what I got so far:
VB Code:
Private Sub Command1_Click()
Dim lngNumerator As Long, lngDenominator As Long, lngCommonFactor As Long
lngNumerator = 5
lngDenominator = 10
If GetCommonFactor(lngNumerator, lngDenominator, lngCommonFactor) Then
MsgBox lngNumerator & " / " & lngDenominator
End If
End Sub
Public Function GetCommonFactor(Numerator As Long, Denominator As Long, CommonFactor As Long) As Boolean
Dim lngIdx As Long
CommonFactor = 1
For lngIdx = Numerator To 2 Step -1
If Numerator Mod lngIdx = 0 Then
If Denominator Mod lngIdx = 0 Then
CommonFactor = lngIdx
Numerator = Numerator / CommonFactor
Denominator = Denominator / CommonFactor
GetCommonFactor = True
Exit For
End If
End If
Next
End Function
-
Feb 20th, 2003, 03:01 PM
#2
transcendental analytic
a/b + c/d = e/f
e=a*d + c*b
f= b*d
but you would rather do away with common factors so h/i where
h=e/g
i=f/g
where g=gcd(e,f)
some pseudo for gcd(a,b):
d=b
r=a mod b
while (r>0){
n=d
d=r
r=n mod d
}
return d
Last edited by kedaman; Feb 20th, 2003 at 03:28 PM.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 20th, 2003, 05:39 PM
#3
Thread Starter
Fanatic Member
Originally posted by kedaman
a/b + c/d = e/f
e=a*d + c*b
f= b*d
but you would rather do away with common factors so h/i where
h=e/g
i=f/g
where g=gcd(e,f)
some pseudo for gcd(a,b):
d=b
r=a mod b
while (r>0){
n=d
d=r
r=n mod d
}
return d
Thanks kedaman going with what you gave me I used it with a function I also found on this forum.
I'm using it in the way as in the sample and seems to work great so far.
Thanks for the help.
VB Code:
Private Sub Command1_Click()
Dim lngNumerator As Integer, lngDenominator As Integer
Dim a As Long, b As Long, c As Long, d As Long
a = 1
b = 6
c = 2
d = 7
lngNumerator = a * d + c * b
lngDenominator = b * d
MsgBox Fraction(lngNumerator, lngDenominator)
End Sub
Function Fraction(Numerator As Integer, Denominator As Integer) As String
Dim i As Integer
For i = CInt(IIf(Denominator >= Numerator, Denominator, Numerator) / 2) To 1 Step -1
If Numerator Mod i = 0 And Denominator Mod i = 0 Then
Fraction = (Numerator / i) & "/" & (Denominator / i)
Exit For
End If
Next:
End Function
-
Feb 24th, 2003, 05:59 AM
#4
In case it may be useful to you, please notice the following example.
If you have a number with a repeating pattern, say, x = 4.820182018201... you can derive a fraction representing it in this fashion.
If the repeating pattern has n figures, then multiply x by 10n. Here:
104x = 10000x = 48201.82018201...
Substract:
10000x - x = 48197
9999x = 48201
So that x = 48197 / 9999
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|