[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding, subtracting, dividing and multipying Negative numbers

Posted on 2006-04-01
8
Medium Priority
?
429 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:ucla12
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16351222
uhm, what about

 Dim i As Integer = 1
        i *= -1

0
 

Author Comment

by:ucla12
ID: 16351252
was my question that dumb?   sorry about that ,  does this work for addiing and subtracting???
0
 
LVL 10

Expert Comment

by:bchoor
ID: 16351757
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


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:bchoor
ID: 16351820
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
0
 
LVL 14

Accepted Solution

by:
PockyMaster earned 2000 total points
ID: 16351866
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()
        Return Add(frac1, frac2)


    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!
0
 
LVL 10

Expert Comment

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

~BC
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16353673
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))
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16353676
Uhm...

 Public Function Subtract(ByVal frac As CFraction) As CFraction

        Return CFraction.Subtract(Me, frac)
    End Function

Like this ofcourse :D
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question