0

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"

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Jean Walrave
  • 191
  • 1
  • 2
  • 13

1 Answers1

3

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.
phuclv
  • 37,963
  • 15
  • 156
  • 475
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Implementing a proper BigDecimal type is not trivial. There are many things to consider. Especially rounding is not so easy, but also things like multi-precision division, multiplication, etc. may be quite a daunting task. Probably more so than all the rest of the VM. – Rudy Velthuis Jul 21 '17 at 23:04
  • @RudyVelthuis: OP is not supposed to implement a general-purpose BigDecimal, just something at the school project level. – einpoklum Jul 21 '17 at 23:39
  • Even a simple implementation (that does not implement stuff like Karatsuba or Burnikel-Ziegler or other optimizations) is not so easy. Especially rounding and division, but also parsing and displaying won't be very easy, not even with something like BCD or strings. I personally find it a little too much for a school project. – Rudy Velthuis Jul 22 '17 at 01:31