Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C#
Article

Fraction class in C#

Rate me:
Please Sign up or sign in to vote.
4.63/5 (22 votes)
14 Feb 2005CPOL4 min read 221.5K   8.1K   50   29
An article representing floating point numbers as fractions.

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.

C#
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:

  • Unary: - (Negation)
  • Binary +, -, *, /
  • Relational operators such as ==, !=, <, >, <=, >=.
C#
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:

C#
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:

C#
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:

  • To convert a double to a fraction, we keep multiplying the given double number with 10 until it is converted to an integer number.
  • To convert a given string to a fraction, we treat all the value before "/" as numerator and after "/" as denominator.
  • To normalize a fraction, we divide its numerator and denominator by their GCD (found by famous Euler's formula).
  • To add two fractions, we use simple school formula to get numerator and denominator of the resultant fraction and then normalize it:
    C#
    Numerator = frac1.Numerator*frac2.Denominator 
                + frac2.Numerator*frac1.Denominator;
    Denominator = frac1.Denominator*frac2.Denominator;
  • To overload arithmetic operators for integers and doubles, we first convert them to fractions and then perform the operation.

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

  • Changed Numerator and Denominator from Int32 (integer) to Int64 (long) for increased range.
  • Renamed ConvertToString() to ToString().
  • Added the capability of detecting/raising overflow exceptions.
  • Fixed the bug that very small numbers, e.g. 0.00000001, could not be converted to fraction.
  • Fixed other minor bugs.

Version 2.1

  • Overloaded conversions from/to fractions.

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

  • Less overflows by finding the GCD for Add [Jeffery Sax] and Multiply [Marc C. Brooks]
  • Understands and handles NaN, PositiveInfinity, NegativeInfinity just like double [Marc C. Brooks]
  • Fixed several uses of int where long was correct [Marc C. Brooks]
  • Made value-type (struct) [Jeffery Sax]
  • Added ToInt32(), ToInt64() which throw for invalid (NaN, PositiveInfinity, NegativeInfinity) [Marc C. Brooks]
  • Removed redundant Value property [Jeffery Sax]
  • Added explicit conversion to Int32 and Int64 [Marc C. Brooks]
  • Better handling of exceptions [Marc C. Brooks]
  • Reorganized code, added XML doc and regions [Marc C. Brooks]
  • Proper implementations of Equals [Marc C. Brooks, Jeffery Sax]
  • Uses Math.Log(xx,2) and Math.Pow(xx,2) to get the best accuracy possible when converting doubles [Marc C. Brooks, Jeffery Sax]

Version 2.3 (changes by Marc Brooks and Jeffrey Sax)

  • Fixed double-to-fraction logic to use continued fraction rules to get best possible precision [bug fix for Syed Mehroz Alam, idea from Jeffery Sax]
  • Added static readonly values for NaN, PositiveInfinity, NegativeInfinity [idea from Jeffery Sax]
  • Moved comparisons into an implementation of IComparer [idea from Jeffery Sax]
  • No longer throws for NaN(s) involved in Add, Subtract, Multiply, Divide operations [idea from Jeffery Sax]
  • Added static readonly values for Zero, MinValue, MaxValue, Epsilon to better mirror double [Marc C. Brooks]
  • Added IsInfinity to better mirror double [Marc C. Brooks]
  • Added modulus and % operators [Marc C. Brooks]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan

Syed Mehroz Alam, living in Karachi, Pakistan, is a developer focusing Microsoft technologies. He has completed his bachelors as a Computer Systems Engineer in 2006 and is currently pursuing a Masters degree in Computer Science. He loves to learn, discover and master all aspects of .NET and SQL Server. Mehroz has developed rich internet enterprise applications using Silverlight in addition to the traditional ASP.NET and Windows Forms applications. He has worked with all three components of SQL Business Intelligence Studio: SSIS, SSRS and SSAS for developing BI Solutions and Data warehouse. He loves to write complex TSQL queries and evaluate his skills by participating in various TSQL Challenges. His blog can be viewed at http://smehrozalam.wordpress.com.


Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1351769713-Feb-24 1:49
Member 1351769713-Feb-24 1:49 
QuestionBug Pin
H. L. McMaken14-Nov-23 15:33
H. L. McMaken14-Nov-23 15:33 
QuestionPretty Fractions Pin
dynamichael1-Aug-20 6:26
dynamichael1-Aug-20 6:26 
BugSome issues with comparison Pin
tharhtwe1-Apr-19 17:15
tharhtwe1-Apr-19 17:15 
BugClass doesnt support big numbers Pin
Member 1179687521-Jul-16 4:04
Member 1179687521-Jul-16 4:04 
GeneralRe: Class doesnt support big numbers Pin
Member 1116400319-Mar-17 5:35
Member 1116400319-Mar-17 5:35 
QuestionSigned Number for Both Numerator and Denominator? Pin
Member 1215322924-Mar-16 8:06
Member 1215322924-Mar-16 8:06 
QuestionGlobalization issue Pin
Anders Eriksson17-Feb-14 2:58
Anders Eriksson17-Feb-14 2:58 
SuggestionHandling Radios & Percents Pin
Duane McKinney19-Jun-13 6:46
Duane McKinney19-Jun-13 6:46 
SuggestionRe: Square roots Pin
mla15425-Jul-12 5:13
mla15425-Jul-12 5:13 
GeneralUnit tests for this class using NUnit Pin
Syed Mehroz Alam16-Feb-10 2:07
Syed Mehroz Alam16-Feb-10 2:07 
GeneralThanks a million! Pin
Le Duc Anh27-Oct-09 19:59
professionalLe Duc Anh27-Oct-09 19:59 
GeneralAlso check out the Fraction class from Apache Pin
Sire40427-Mar-09 3:03
Sire40427-Mar-09 3:03 
Generalgcd in binary Pin
TheKing28-Mar-07 10:35
TheKing28-Mar-07 10:35 
GeneralSimple change Pin
Daenris24-Sep-06 13:26
Daenris24-Sep-06 13:26 
GeneralRe: Simple change Pin
Daenris24-Sep-06 13:33
Daenris24-Sep-06 13:33 
GeneralRe: Simple change Pin
dodiggitydag12-Oct-06 15:10
dodiggitydag12-Oct-06 15:10 
GeneralRe: Simple change Pin
Nick Alexeev12-Oct-14 9:40
professionalNick Alexeev12-Oct-14 9:40 
GeneralA very good class Pin
Mina Momtaz13-May-06 22:33
Mina Momtaz13-May-06 22:33 
GeneralBug in CompareTo(Fraction right) method. Pin
Marc Brooks9-Nov-05 7:25
Marc Brooks9-Nov-05 7:25 
GeneralSome improvements for this class. Pin
Marc Brooks20-Dec-04 13:22
Marc Brooks20-Dec-04 13:22 
GeneralRe: Some improvements for this class. Pin
Syed Mehroz Alam20-Dec-04 20:06
Syed Mehroz Alam20-Dec-04 20:06 
GeneralRe: Some improvements for this class. Pin
Jeffrey Sax20-Dec-04 21:28
Jeffrey Sax20-Dec-04 21:28 
GeneralRe: Some improvements for this class. Pin
Marc Brooks11-Jan-05 10:26
Marc Brooks11-Jan-05 10:26 
GeneralRe: Some improvements for this class. Pin
Hardy Wang7-Mar-09 7:01
Hardy Wang7-Mar-09 7:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.