GithubHelp home page GithubHelp logo

bhishmapitamah / rational_library Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 9 KB

Rational Library Static header for C++.

CMake 2.37% C++ 97.63%
cpp header rational rational-numbers library cmake

rational_library's Introduction

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

rational_library's People

Contributors

bhishmapitamah avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.