-1

There is a way in java to use BigInteger Class For Big Integer addition,subtraction, multiplication and division. but i want to do it by c++ without any built in class. can anyone give me the idea??

Till
  • 27,559
  • 13
  • 88
  • 122
shawon
  • 984
  • 7
  • 15
  • Why the bigdata tag? – xiaofeng.li May 05 '16 at 22:12
  • Sure, you are dealing with **big** integers, but those can be hold in the memory of your laptop, right? – xiaofeng.li May 05 '16 at 22:13
  • 1
    You do it one digit/word at a time, or you get a library (or you use a language that supports it better out of the "box.") – Michael Dorgan May 05 '16 at 22:21
  • Search the internet for "c++ big number library", since the standard C++ language has no faculties for big number support. – Thomas Matthews May 05 '16 at 22:55
  • For the simplest, self-teaching approach, just create a class that stores its value in a `std::string` - one digit per character - and start implementing the maths operators using the same logic you would use doing it with pencil and paper. Be mindful of the difference between ASCII '0' and numeric 0. For a more serious approach, use a `std::vector` or `std::vector` in combination with `bool` for sign. If you have a go and get stuck, ***then*** post a specific question. If you just want a library, seeking recommendations are off-topic for S.O., though Luke's obliged. – Tony Delroy May 06 '16 at 01:20
  • Yeah using string i did. but what about when it exceeds limit of string data?? i want the perfect algorithm like modulus mathmatic procedure. i heared that big integer is implemented by modulus arithmatic. but i don't know the implementation. – shawon May 06 '16 at 03:46
  • @shawon: strings can grow pretty much as large as your virtual memory (i.e. many billions of digits on any PC). A binary representation will be 2.56 times more memory efficient, but that's not orders-of-magnitude - not a reason to care for a learning exercise. Whatever you heard about modulus arithmetic would be more relevant for the `std::vectror` approach I mentioned, where you'd need to think about more digits carrying or being lost than you're used to doing maths long-hand on paper. For the string approach though, just forget that and have a go. – Tony Delroy May 06 '16 at 07:52

1 Answers1

2

For big integer arithmetic in C++, there's no standard library support. But you can use GMP.

xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30