Results 1 to 4 of 4

Thread: how can I simplify these fractions?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281
    Hi there,

    thanks to a bit of help and some adjustments, my simplify function is getting better - but not quite perfect.

    I think it's a problem with that stupid Mod operator - tell me if I'm wrong.

    Anyway, it works in the sense that things like:

    6/4 will be given as 3/2
    4/1 will be given as 4
    10/2 will be given as 5
    -3/9 will be given as -1/3
    0/5 will be given as 0

    But for fractions like -9/1, -1/-5, -3/-3, 1/-8....nothing is returned, no answer is given. Could someone please take a look at it. I've been at it for days.

    Thanks for any help!

    ------------

    Dim a As Integer
    Dim b As Integer

    Private Sub Command1_Click()
    a = Int(Rnd * 20) - 10
    b = Int(Rnd * 20) - 10
    If b = 0 Then
    b = 1
    End If
    Picture1.Cls
    Picture1.Print a & "/" & b
    Picture2.Cls
    Picture2.Print Fraction(a, b)
    End Sub

    ''this function reduces fractions
    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)
    'the number is one
    If numerator = denominator Then
    Fraction = 1
    End If
    'whole number answer
    If numerator Mod denominator = 0 And numerator <> denominator Then
    Fraction = numerator / denominator
    End If
    Exit For
    End If
    Next:
    End Function




  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    The best war to get around that is to remove your -ves before you call the fraction function

    so do something like this


    Code:
    Public Sub LowestTerms(ByRef Numerator As Long, ByRef Denomiator As Long)
    
    
    Dim MySgn As Integer '(the sign of the fraction)
    
    'Work out the Sign of the Fraction
    MySgn = Sgn(Numerator) * Sgn(Denominator)
    
    'Make the Fraction Strictly +ve
    Numerator = Abs(Numerator)
    Denominator = Abs(Denominator)
    
    
    
    'Put Your Code to make it in the lowest terms Here
    
    
    'Multiply the Numerator by the sign of the Fraction
    Numerator = Numerator * MySgn
    
    End Sub
    And you'll be laughing.

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    I didn't test this and it was a late posting..
    did you give it a shot.

    http://209.207.250.147/showthread.php?threadid=31454

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Lively Member
    Join Date
    Feb 1999
    Location
    France
    Posts
    90
    HeSaidJoe,

    Yeah I tried the code you posted but I ran into the same problem - with those negative numbers. Thanks anyway.

    Sam, I'll try what you suggested now.

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