Skip to main content
Email Password   helpLost your password?

Introduction

This article demonstrates how to remove the problem of limited precision when numbers such as 1/10, 1/7, 1/3, etc. are represented in a floating point variable type in C#. Yes, you guessed it right, this problem can be solved by storing such numbers in a fraction format. As a fraction object stores its numerator and denominator as integer variables, there is no loss of accuracy. For this purpose, I have written a simple C# class representing fractions. It can be used in various applications, e.g., equation solving, matrix transformations, etc.

Features of class

The class contains a variety of overloaded constructors and operators for certain situations. It also throws certain exceptions, e.g., when denominator is tried to assign a 0 value, when the result of an arithmetic exceeds the limit (range), etc. One more feature is the automatic normalization (simplification) of fractions. The class uses data type 'long integer' for storing numerator and denominator, hence its range is upper bounded by the range of Int64 in .NET framework.

Using the code

The class includes a variety of constructors, e.g., one that takes an integer like 123, one that takes a double like 156.25, one that takes a string that has all the above qualities (e.g., string can be "32/77" or "213" or "321.44"), one that takes values of numerator and denominator like 231 and 101, and, of course, a parameter-less constructor for a "zero" fraction stored as 0/1.

Fraction frac=new Fraction(); // we'll get 0/1

frac=new Fraction(1,5);       // we'll get 1/5

frac=new Fraction(25);        // we'll get 25/1

frac=new Fraction(9.25);      // we'll get 37/4

frac=new Fraction("6.25");    // we'll get 25/4


frac=new Fraction( System.Console.ReadLine() );
     // we can enter anything like "213" or 

     // "23/3" or "4.27"


Console.WriteLine( frac );
// displays the current value of frac1 object;

Operators overloaded (overloaded for fractions, integers and doubles) for Fraction object include:

Fraction frac=new Fraction("1/2"); // initialize a fraction with 1/2

Console.WriteLine( frac+2.5 );     // will display 3

Overloaded conversion further enhances the capabilities of the class. See how simple it is to work with fractions:

Fraction frac="1/2" // implicit cast from string to 

frac="22.5"         // implicit cast from string to fraction

frac=10.25          // implicit cast from double to fraction

frac=15             // implicit cast from integer/long to fraction

Finally, as an exercise, guess the output of the following code:

Fraction f=0.5;                 // initialize frac=1/2

Console.WriteLine( f-0.25 );    // Yes, you are right. "1/4" is displayed

Console.WriteLine( f+"1/4" );
   // not sure??? It will display "3/4" because "1/4" has 

   // been converted to fraction and then added to our frac object

Implementation details

The class uses simple mathematics to do all the work. Let us see some of these simple techniques:

Applications

There are a lot of applications of Fraction class. An example is a matrix class, see my article on Matrix class in C#.

History

Version 2.0

Version 2.1

Version 2.2 (changes by Marc Brooks and Jeffrey Sax).

Version 2.3 (changes by Marc Brooks and Jeffrey Sax)

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralThanks a million! Pin
anhldbk
20:59 27 Oct '09  
GeneralAlso check out the Fraction class from Apache Pin
Sire404
4:03 27 Mar '09  
Generalgcd in binary Pin
TheKing2
11:35 8 Mar '07  
GeneralSimple change Pin
Daenris
14:26 24 Sep '06  
GeneralRe: Simple change Pin
Daenris
14:33 24 Sep '06  
GeneralRe: Simple change Pin
dodiggitydag
16:10 12 Oct '06  
GeneralA very good class Pin
Mina Momtaz
23:33 13 May '06  
GeneralBug in CompareTo(Fraction right) method. Pin
Marc Brooks
8:25 9 Nov '05  
GeneralSome improvements for this class. Pin
Marc C. Brooks
14:22 20 Dec '04  
GeneralRe: Some improvements for this class. Pin
Syed Mehroz Alam
21:06 20 Dec '04  
GeneralRe: Some improvements for this class. Pin
Jeffrey Sax
22:28 20 Dec '04  
GeneralRe: Some improvements for this class. Pin
Marc Brooks
11:26 11 Jan '05  
GeneralRe: Some improvements for this class. Pin
Hardy Wang
8:01 7 Mar '09  
GeneralA few suggestions Pin
Jeffrey Sax
17:19 16 Dec '04  
GeneralRe: A few suggestions Pin
Syed Mehroz Alam
20:54 20 Dec '04  
GeneralRe: A few suggestions Pin
Jeffrey Sax
22:45 20 Dec '04  


Last Updated 15 Feb 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009