Share your repls and programming experiences

← Back to all posts
Fractions Library
finlay44111 (32)

I made a simple library for c++ for handling fractions, it is pretty basic atm but i am open to suggestions for new stuff to be added in. at the moment a fraction class is two integers for a numerator and a denominator, and i have overloaded some operators (*,/,+,-,==,!=,>,<,>=,<=) which work as you expect they would. you can also call the simplify function on the fraction object to give you, yes you guessed it, a simplified fraction.

currently the +,-,*,/ operators only work with a fraction and another fraction or a fraction and an integral type, i am working on making a constructor that takes in a floating point value and will approximate it to a fraction. which would make addition, subtraction, etc possible with floating point numbers. the comparison operators wotk with any numeric type

Example Usage:

int main
  frac::fraction fraction1(2,7);
  frac::fraction fraction2(3,17);
  std::cout << fraction1 + fraction2 << std::endl;