Home  |  Site Map
  ConceptDevelopment.NET
Skip Navigation Links
Home
SearchExpand Search
SilverlightExpand Silverlight
DatabaseExpand Database
ValidationExpand Validation
LocalizationExpand Localization
Fun stuffExpand Fun stuff
  Download now

ZIP file (20k) This ZIP file (20k) contains the C# source code for the Fraction type. All the standard operators (+. -. /, ==, !=) are overridden to allow manipulation of Fractional data, and IComparable is implemented.


About the code
The code was built using the Microsoft VisualStudio.NET Release Candidate on Windows 2000 and Internet Explorer 6.0. It will soon be updated to the released version - any comments on compatibility would be appreciated.
 

Why a Fraction type?
Recipes.
While working on a database of recipes, I encountered the problem of 'multiplying' a recipe's quantities to make a recipe for four serve eight or twelve. Similarly, generating a shopping list from mulitple recipes requires adding 1 1/2 cups flour to 3/4 cup flour and being able to represent the result as a fraction.

The Fraction type is easy to use:

Fraction f1, f2, f3;
f1 = new Fraction(1,3,5);
f2 = new Fraction(-2,1,4);
f3 = new Fraction(1,2);
and facilitates the following operations:
(1/2 == 2/4) = True
1/2 * 1/2 = 1/4
1/4 / 2 = 1/8
(-5/8) - 2= -2 5/8
2 + (-5/8) = 1 3/8

The Fraction code is available to view and download. You can see the results of testing here.

Useful links

View the code!
C# code demonstrating various concepts

GotDotNet.com QuickStart
Read the .NET sample code for operator override.

Complex Numbers Class
Similar sample, modelling complex (real+imaginary) numbers.