Skip to content

bhishmapitamah/Rational_Library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rational_Library

Rational Library Static header for C++.

The library allows to use rational numbers in your project. The number does not reduce itself to simplest form automatically. It has to be simplified when and where required using the inbuilt function.

The rational class uses intmax_t as the base data type for storing numerator and denominator.

Constructor

The constructor can be called using any of the fundamental integer types as numerator or denominator.

Examples

  • rational r; // Constructs with value of 1/1
  • rational r(3,4); // Constructs with value of 3/4
  • rational r(3); // Constructs with value of 3/1
  • rational r('a',3); // Constructs with value of 97/3 (ASCII value of 'a' is 97)

Operators

The class supports arithmatic and relational operators.

  • Shorthand Assignment Operators +=, -=, *=, /=
  • Arithmatic Operators +, -, *, /
  • Relational Operators <, <=, >, >=, ==, !=

The operands can be both rational or fundamental integer types.

I/O Operators

The input and output stream operators are also overloaded for ease of use.

Examples

  • cin >> a >> b; // a and b are two rational objects
  • cout << a << b; // a and b are two rational objects

The intput format is x/y (3/4), if the denominator is not specified it is assumed to be 1.

The output format is also similar x/y (3/4).

Other Member Function

Other member functions of the class.

  • GCD function int gcd = r.GCD(); // Returns the GCD of the number
  • Simplify function for normalizing r.simplify(); // 6/8 is simplified to 3/4
  • Set function for in place setting r.set(3,4); // sets the value of r to 3/4
  • num for getting the value of numerator int num = r.num(); // returns the value of numerator
  • den for getting the value of denominator int den = r.den(); // returns the value of denominator

Other Utility functions

Other functions defined as utility for the class.

  • abs for absolute a = abs(b); // -3/4 returns 3/4

References

Releases

No releases published

Packages

No packages published