We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# Adding, subtracting, dividing and multipying Negative numbers

on
Medium Priority
442 Views
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
Comment
Watch Question

## View Solution Only

Commented:

Dim i As Integer = 1
i *= -1

Commented:
was my question that dumb?   sorry about that ,  does this work for addiing and subtracting???

Commented:
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

Commented:
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
Commented:
I created this class for you:

Public Class CFraction
Private _iNum As Integer
Private _iDenum As Integer
Private _iSign As Integer = 1

Public Sub New(ByVal iNum As Integer, ByVal iDenum As Integer)
_iNum = iNum
_iDenum = iDenum
End Sub

Public Sub ToggleSign()
_iSign *= -1
End Sub

Public ReadOnly Property Sign() As Integer
Get
Return _iSign
End Get
End Property

Public ReadOnly Property Denumerator() As Integer
Get
Return _iDenum
End Get
End Property

Public ReadOnly Property Numerator() As Integer
Get
Return _iNum
End Get
End Property

Public Shared Function Add(ByVal frac1 As CFraction, ByVal frac2 As CFraction) As CFraction

If frac1 Is Nothing OrElse frac2 Is Nothing Then Return Nothing

Dim iMultiply As Integer = frac1.Denumerator * frac2.Denumerator
Dim iNumerator As Integer = (frac1.Sign * frac1.Numerator * iMultiply / frac1.Denumerator) + (frac2.Sign * frac2.Numerator * iMultiply / frac2.Denumerator)

Return New CFraction(iNumerator, iMultiply)

End Function

Public Shared Function Subtract(ByVal frac1 As CFraction, ByVal frac2 As CFraction) As CFraction

frac2.ToggleSign()

End Function
End Class

try to use it like this:

Dim frac1 As New CFraction(1, 4)
Dim frac2 As New CFraction(1, 3)
Dim frac3 As CFraction = CFraction.Add(frac1, frac2)
Dim frac4 As CFraction = CFraction.Subtract(frac1, frac2)

You might add the HCF function  of bcHoor to  normalize your fraction result

Good luck!

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Nice job Pocky...I came across this one as well http://www.vb-helper.com/howto_fraction_class.html

~BC

Commented:
Actually after I posted I thought I forgot 2 methods:

Public Function Add(ByVal frac As CFraction) As CFraction

End Function

Public Function Subtract(ByVal frac As CFraction) As CFraction

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))

Commented:
Uhm...

Public Function Subtract(ByVal frac As CFraction) As CFraction

Return CFraction.Subtract(Me, frac)
End Function

Like this ofcourse :D
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile