Link to home
Start Free TrialLog in
Avatar of ucla12
ucla12

asked on

Adding, subtracting, dividing and multipying Negative numbers

I have a Fraction Calculator I pieced together in Visual basic 2005 express.  My kids use to check homework.  It works just fine But my oldest child has been getting some problems that have negative numbers  example:  -1/2 X 1/4  = -1/8 The rules for multipying and division are simple...If both fractions (numbers) have the same sign the anwser is positive  But with adding or subtracting it is a diffrerent story. Is there a built in math function or a way to assign a number to be negative? if so will it follow the negative / position rukles of math.  On a calculator you would use the +/- button. Or do I have to create my own code?  any help would be appreicated.  I have my code I am using now if you need to see it.....Thanks
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

uhm, what about

 Dim i As Integer = 1
        i *= -1

Avatar of ucla12
ucla12

ASKER

was my question that dumb?   sorry about that ,  does this work for addiing and subtracting???
for adding and subtracting fractions - say 2/8 + 1/2
1. you would need to get the denominators and multiply them
denominators are 8 and 2 ==> 8 * 2 = 16
2. then cross multiply
so 2 * 2 = 4 (where the first 2 is from the numerator in 2/8 and the second 2 is from the denominator in 1/2)
also 1 * 8 = 8
3. Now both fractions have the same denominator so you can add and subtract easily
4/16 + 8/16 ==> (4+8)/16 = 12/16

Let's say you have 3/8 - 1/4
1. 8*4 = 32
2. 3*4 = 12; 1*8 = 8
3. 12/32 - 8/32 ==> (12-8)/32 = 4/32

The only thing missing is making the result into a proper fraction which I'm not sure how to achieve
i.e 4/32 = 1/8 or 12/16 = 3/4

Usually you would try to find the HCF (Highest Common Factor of both numbers),
in the case of 4 and 32 - the HCF is 4 ==> (4/4) / (32/4) = 1/8
in the case of 12 and 16 - the HCF is 4 ==> (12/4) / (16/4) = 3/4

Hope this makes some sense
~BC


I found this to calculate HCF - it was in C++, I changed it to VB.NET

    Public Function GetHCF(ByVal A As Integer, ByVal B As Integer) As Integer
        ' Make sure B > A, if not switch it around
        If A = B Then
            Return A
        ElseIf A > B Then
            Dim temp As Integer = B
            B = A
            A = temp
        End If

        Dim C As Integer
        Do
            C = B Mod A
            If C = 0 Then
                Return A
            Else
                B = A
                A = C
            End If
        Loop
    End Function


HTH
~BC
ASKER CERTIFIED SOLUTION
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice job Pocky...I came across this one as well http://www.vb-helper.com/howto_fraction_class.html

~BC
Actually after I posted I thought I forgot 2 methods:

  Public Function Add(ByVal frac As CFraction) As CFraction

        Return CFraction.Add(Me, frac)

    End Function

    Public Function Subtract(ByVal frac As CFraction) As CFraction

        Return CFraction.Add(Me, frac)
    End Function


That will allow you to do :
Dim frac1 As New CFraction(1, 4)
Dim frac2 As New CFraction(1, 3)
Dim frac3 As CFraction = frac1.Add(frac2)

or to put it on a single line:

        Dim fracResult As CFraction = New CFraction(1, 4).Add(New CFraction(1, 3))
Uhm...

 Public Function Subtract(ByVal frac As CFraction) As CFraction

        Return CFraction.Subtract(Me, frac)
    End Function

Like this ofcourse :D