O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Work with Fractions Without Using Decimals

Problem

You want to perform mathematical operations with fractions without converting to decimal notation (and possibly introducing rounding errors).

Solution

Create your own simple fraction class.

Discussion

The .NET Framework does not include any built-in support for fraction calculations. However, it’s quite easy to create a class to represent vectors. This class will include methods such as Add, Subtract, Multiply, and DivideBy. It will also include a Normalize function that will reduce a fraction to lowest terms using Euclid’s algorithm and adjust its sign. In addition, the class will support cloning and comparing (useful for sorting arrays of fractions), and it will override the Equals method to perform ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required