4

I'm trying to add subtract and multiply numbers with huge decimals C++.

Example:

4125487821547.87879845215584844588 - 354556689.899455132265468

What I figured so far is that I need to save inputs as a string, but not sure how to proceed after that.

Appreciate the help in advance Thanks

handlerFive
  • 870
  • 10
  • 23
Djon
  • 41
  • 1
  • You have to either write your own functions for this or use some library. Using a library is probably easier. – Qubit Sep 13 '18 at 12:02
  • Im looking for my own functions at first. How would that be possible? Thanks – Djon Sep 13 '18 at 12:12
  • 3
    If you just need to do addition and subtraction, and performance is not a vital concern, you can treat the numbers as strings and write a routine that will locate the decimal point and then add or subtract digit-by-digit (as learned in elementary school). If you need higher level math functions, and don't want to use an already created library, expect to spend anywhere from 6 months to 10 years (depending on capability) to create your own. – Eljay Sep 13 '18 at 12:28
  • `numeric_limits::digits10` is 18 on most implementations. Thus there is not support for this in the standard library. So you're either asking us how to implement a new type, which is much too broad, or you're asking for a library which implements this new type. Either way unless you can rephrase this question it's outside the scope of https://stackoverflow.com – Jonathan Mee Sep 13 '18 at 12:33
  • 1
    @Djon *Im looking for my own functions at first. How would that be possible?* -- Write the code to do it. This is a typical college level assignment -- write a class that can add, subtract, and multiply "big numbers". Usually the numbers are represented as a string, and you have to write the program to add, subtract, multiply, etc. using the same methods as the way you do this in elementary school (ex. add column going right to left, make provisions for the carry, etc.) – PaulMcKenzie Sep 13 '18 at 12:51

1 Answers1

9

You need a big integer class or library. There are several implementations available, just to give you an overview on how to use such an external dependency, here is a solution based on Boost:

#include <boost/multiprecision/cpp_int.hpp>

using BigInt = boost::multiprecision::cpp_int;

You can now construct instances by passing string or integral literals to the constructor and do all standard arithmetic operations with these objects, e.g.

const BigInt i("8787984521558484092344588");
const BigInt j("32308942039402934");

std::cout << i - j << "\n";

One nice detail of such classes is that they usually demonstrate one of the few justified scenarios for non-explicit constructors with one argument, i.e., for the sake of smooth interoperability with builtin integral types. Example:

int n = 42;

// Use builtin ints like BigInts via implicit BigInt(int) ctor:
std::cout << (i + n)/(j % 3) << "\n";

You only need the Boost headers for these snippets, no linkage is required. Check out the docs when proceeding with this library.

lubgr
  • 37,368
  • 3
  • 66
  • 117