Results 1 to 4 of 4

Thread: Expressing A Repeating Decimal Number As A Fraction

  1. #1

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Question 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:
    1. Private Sub Command1_Click()
    2. Dim lngNumerator As Long, lngDenominator As Long, lngCommonFactor As Long
    3.  
    4. lngNumerator = 5
    5. lngDenominator = 10
    6.  
    7. If GetCommonFactor(lngNumerator, lngDenominator, lngCommonFactor) Then
    8.      MsgBox lngNumerator & " / " & lngDenominator
    9. End If
    10.  
    11. End Sub
    12.  
    13. Public Function GetCommonFactor(Numerator As Long, Denominator As Long, CommonFactor As Long) As Boolean
    14.     Dim lngIdx As Long
    15.    
    16.     CommonFactor = 1
    17.    
    18.     For lngIdx = Numerator To 2 Step -1
    19.         If Numerator Mod lngIdx = 0 Then
    20.             If Denominator Mod lngIdx = 0 Then
    21.                 CommonFactor = lngIdx
    22.                 Numerator = Numerator / CommonFactor
    23.                 Denominator = Denominator / CommonFactor
    24.                 GetCommonFactor = True
    25.                 Exit For
    26.             End If
    27.         End If
    28.     Next
    29.  
    30. End Function
    Give your music collection a whole new life with PartyTime Jukebox

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3

    Thread Starter
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560
    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:
    1. Private Sub Command1_Click()
    2. Dim lngNumerator As Integer, lngDenominator As Integer
    3. Dim a As Long, b As Long, c As Long, d As Long
    4. a = 1
    5. b = 6
    6. c = 2
    7. d = 7
    8.  
    9. lngNumerator = a * d + c * b
    10. lngDenominator = b * d
    11.  
    12. MsgBox Fraction(lngNumerator, lngDenominator)
    13.  
    14. End Sub
    15.  
    16. Function Fraction(Numerator As Integer, Denominator As Integer) As String
    17. Dim i As Integer
    18.  
    19. For i = CInt(IIf(Denominator >= Numerator, Denominator, Numerator) / 2) To 1 Step -1
    20.     If Numerator Mod i = 0 And Denominator Mod i = 0 Then
    21.         Fraction = (Numerator / i) & "/" & (Denominator / i)
    22.         Exit For
    23.     End If
    24. Next:
    25.  
    26. End Function
    Give your music collection a whole new life with PartyTime Jukebox

  4. #4
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    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
  •  



Click Here to Expand Forum to Full Width