Adding, Subtracting, Multiplying, and Dividing Fractions in Visual Basic.Net


   Hello world! I love Math. So I'm going to share with you today a simple DLL that will allow you to declare a fraction and perform basic operations on it. I created a class which can be found under Arnex.dll. It's Arnex.Fraction. With Arnex.Fraction you can perform the following:

  • Add (+)
  • Subtract (-)
  • Multiply (*)
  • Divide (/)


You can also compare fraction using the following:

  • Greater Than (>)
  • Less Than (<)
  • Is Equal To (=)
  • Is Not Equal To (<>)

Other useful functions are:


.ToString()
.IsProper()
.Reciprocal()
.AdditiveInverse()
.ToMixedForm()

When displaying fractions, you should convert it to string using .ToString function. 

See sample code below to see how it works. Simply add Arnex.dll on your code and import it to your code.


Imports Arnex

Module Module1

    Sub Main()
        Dim a As New Fraction(4, 5)
        Dim b As New Fraction(2)
        Dim c As New Fraction
        Dim d As New Fraction With {
            .Numerator = 3,
            .Denominator = 7}
        Dim e As New Fraction(6, 9)


        Console.WriteLine("Denominator of " & b.ToString & " is " & b.Denominator)
        Console.WriteLine("The equivalent of " & a.ToString & " in decimal is " & a.ToDecimalForm)
        Console.WriteLine(e.ToString & " in simplest form is " & e.ToSimplestForm.ToString)

        c = a + d + e

        Console.WriteLine("The sum of a + d + e is " & c.ToString & " equivalent to " & c.ToMixedForm)

        Dim f As Fraction = a

        Console.WriteLine("F is " & a.ToString)
        Console.WriteLine("The product of 4/5 and 1/2 is " & (New Fraction(4, 5) * New Fraction(1, 2)).ToString)
        Console.WriteLine("The quotient of 4/5 and 1/2 is " & (New Fraction(4, 5) / New Fraction(1, 2)).ToMixedForm.ToString)

        If d > e Then
            Console.WriteLine(d.ToString & "is greater than " & e.ToString)
        Else
            Console.WriteLine(d.ToString & "is less than " & e.ToString)
        End If

        If d > e Then
            Console.WriteLine(d.ToString & "is greater than " & e.ToString)
        Else
            Console.WriteLine(d.ToString & "is less than " & e.ToString)
        End If

        Dim g As New Fraction(8, 12)

        If e = g Then
            Console.WriteLine(e.ToString & " is equal to " & g.ToString)
        End If

        Console.WriteLine("The difference between e and g is " & (e - g).ToString)
        Console.WriteLine(New Fraction(4, -7).ToString)

        Console.WriteLine("Is 3/5 a proper fraction? " & New Fraction(3, 5).IsProper)
        Console.WriteLine("Is 7/6 a proper fraction? " & New Fraction(7, 6).IsProper)
        Console.WriteLine("The reciprocal of " & a.ToString & " is " & a.Reciprocal.ToString)
        Console.WriteLine("The additive inverse of " & d.ToString & " is " & d.AdditiveInverse.ToString)

        Console.ReadKey()

    End Sub

End Module




You may download Arnex.dll and a sample project here. The sample project was created using VS2013 Community Edition.

P.S.
If ever you found some bugs, please inform me by commenting below so that I can correct it.

You're Awesome.
Arnel Isiderio Robles Software Developer

I am passionate about doing something good in the community and sharing my thoughts on different areas ranging from programming to community development. I am interested in developing my skills and use it for the greater good.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.