Giter VIP home page Giter VIP logo

longnums's Introduction

LongNums

A custom made proof-of-concept class for arbitrary precision arithmetic.
Supports only integers.


Table of Operations

Key for examples-


Capitalised variables indicate `LongNumber` objects. Lower case variables indicate `int`s or `long`s.
Operation Function/
Operator
Example Notes
Input >> std::cin >> X;
Output << std::cout << X;
Assignment = X = y;
X = 123;
X = "-112";
Can equate with a long, LongNumber or a std::string. Will throw a InvalidInputException if it is unable to parse the string.
Comparison <
>
==
!=
X < Y;
X > 1123;
X == y;
X != 0;
Can compare a LongNumber with other LongNumbers, as well as longs and std::strings. Can use combined comparisons like <= or >=.
Addition +
+=
++
X + y;
X + Y;
X += Y;
X++;
++X;
Can add with longs as well as LongNumbers. Each notation has its standard meaning.
Subtraction -
-=
--
X - y;
X - Y;
X -= Y;
X--;
--X;
Can subtract longs as well as LongNumbers from the LongNumber. Each notation has its standard meaning.
Multiplication *
*=
X * y;
X * Y;
X *= Y;
Can multiply with longs as well as LongNumbers. Each notation has its standard meaning.
Division /
/=
X / y;
X / Y;
X /= Y;
Can divide with longs as well as LongNumbers. Each notation has its standard meaning. Dividing by zero will throw a DivisionByZero exception.
Modulo %
%=
X % y;
X % Y;
X %= Y;
Can find the modulus of LongNumber with longs as well as LongNumbers. Each notation has its standard meaning. Finding the modulus with 0 will throw a DivisionByZero exception.
Absolute absolute() X.absolute() Returns a LongNumber. Will not change the contents of X.
Cast to Long toLong() X.toLong() Returns a long. Does not account for overflow.

Exceptions

Name Function
DivideByZeroException Thrown when any attempt is made to divide by 0.
InvalidInputException Thrown when the entered string does not correspond to an integer.

longnums's People

Contributors

aadityanaik avatar ealmansi avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

longnums's Issues

General feedback

There's a few other points I thought of bringing up when I was doing the minor refactoring in my last PR. I haven't really had time to suggest these via a PR, so I'll just write the list of comments here:

  • The name of the class would probably feel more intuitive in singular and with consistent case. Ie: LongNumber or BigNumber or BigInteger instead of Longnums.
  • You're defining a custom type supporting "natural" operations via operator overloading, yet it does not conform to the typical interface. Here's a really good reference as to which operators your custom type would be expected to implement: http://articles.emptycrate.com/2009/10/12/nobody_understands_c_part_8_operator_overloading.html.
  • There's no need to define new operators for each int type you want your custom type to be interoperable with. For example, if you want your custom type to be able to perform arithmetic operations, or be comparable with, long ints simply add a constructor with the signature Longnums(const long int x) and you get all these operations for free.
  • Functions returning a boolean value derived from an expression should return the expression itself. I mean doing this return expr instead of this if (expr) return true; else return false;.
  • You have duplicated logic in quite a few places. For example, when implementing comparison operators you would want to define only two of them (== and <, for example), and define the rest in terms of these two. Also, instead of implementing addition and subtraction you could define subtraction as a - b = a + (-b).
  • I'm not sure why you're adding a base property to the Longnums class, since this is expected to be always the same for all instances of the class. Not sure if I'm missing something here, tho.
  • A better alternative to using base 10^8 would probably be just using unsigned ints to represent your numbers (essentially using base 2^32).

Best of luck. Cheers.

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.