When you want to do many calculations with big decimals, you often loose a little information. If you were able to use fractions instead of decimals you’re going to safe some information (concerning the result). Well, with this simple class you can do a lot of manipulations using fractions.


Numerator – fraction’s numerator
Denominator – fraction’s denominator
Form – proper / improper (if |fraction|<1 it is proper, else – it’s improper)


You can initialize the fraction by calling its constructor or by direct equality with an integer. There are four constructors:

  1. The first one is the default one. It initializes a fraction with numerator 0 and denominator 1
  2. The second one takes one argument (int). It’s to present integers as fractions with denominator 1
  3. The third one takes two arguments (both int) – the numerator and the denominator of the fraction
  4. The last one takes one argument again, but it’s a string. It converts a string like a/b into a fraction with numerator a and denominator b

I want to add some things else. The denominator of the fraction is always greater than zero. The sign of the fraction is always kept in its numerator. The zero fraction is always represented as 0/1. That means, if you try to change the numerator to zero, the denominator is automatically set to 1.

Arithmetic Operations

You can perform sum, subtraction, multiplication and division on this fractions. All operations are possible for both fractions and integers. For example, you can add up an integer to a fraction. The integer is automatically converted to a fraction with denominator 1. You can use a method or a sign for the corresponding operation. Here are some examples:


Fractions can be compared using the standard signs for comparison: <>=. As the denominator of each fraction is positive, we can implement the following method to compare two fractions. Let’s say we have these two fractions ; a/b and c/d, and ‘#’ is one of possible signs between them. a/b # c/d <=> ad # bc.


There are some useful methods, that can help you in different situations. For example, you can simplify the the fraction, get its reciprocal or round it to a decimal. I am going to show you the method, which simplifies the fraction.

As you can see, the algorithm finds the greatest common divisor (GCD) of the numerator & denominator and divides both of them by it. I have posted a simple implementation of the Euclidean algorithm for determination of the GCD.


If you encourage any problems with this class, please get in touch with me and I will help you. Well, that’s it! Have fun! 🙂

[ Assembly File ] [ Source Code ]