Search code examples
c++calculatorprecisionbigdecimal

C++ type with 200 digits after decimal point?


I have a project for my school, and the goal of it is create simple "Virtual Machine" who execute operations.

To do that, we can store some numbers with types, they are: int8, int16, int32, float, double, and big decimal.

The only type i don't know is the "big decimal", it is describe with: "The maximum of digits to display after the decimal point is are float(7), double(15), bigdecimal(200)."

So big decimal type can have 200 digits after the decimal point.

Any type in C++ ca do that? If i have to deal with strings it's more complex for the operations...

Thanks you in advance !

PS: "• all (external) libraries, except the STL are explicity forbidden"


Solution

  • There is no "big-decimal" type in C++; from your description of the problem, it seems you are subposed to implement your own type with very-high precision.

    There is more than one way to do this. Here are some hints/potential directions:

    • If d1 ... dk are digits, then the k-digit number 0.d1d2...dk is equal to 10-k * d1 d2 ... dk (i.e. think mantissa and exponent).
    • 200 = lots. Don't try to manually fit your number in enough basic types to get 200 digits.
    • The standard library has several classes you could use. Still, you can hardly go wrong by basing your work on an std::vector. Of course, what exactly you want to put in that vector is a different question.