0

I'm trying to print Ackermann's function values in C++

the first 50 results are right but as the number starts to grow out the last digit start to be printed wrongly.

for example:

// consider ack(m, n) as the ackermann function

case ack(3, 51) should print 18014398509481981 but instead it prints 18014398509481980. The diffence is always between 1 and 3 on the last digit case ack(3, 175) should print 383123885216472214589586756787577295904684780545900544 and it prints 383123885216472214589586756787577295904684780545900541.

i'm trying to print these giant numbers without storing then into a variable because i don't know any native cpp datatype that can store such big numbers so i instead printed the value directly like that:

 cout << setprecision(0) << fixed << (8*pow(2,n)-3) << "\n";

what can i do to print the right value? can i do it differently?

Kakiz
  • 1,145
  • 1
  • 9
  • 18
  • 1
    Basic numeric data types in C++ have a fixed upper and lower limit and your number is bigger than what `uint64_t` (the largest possible unsigned integer data-type) can store. You might need to use an [unbounded integer library that simulates usage of numbers and arithmetic with strings instead](https://github.com/connormanning/little-big-int). – Ruks Apr 14 '20 at 06:11
  • *i don't know any native cpp datatype that can store such big numbers* -- There is [boost::cpp_int](https://www.boost.org/doc/libs/1_72_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/cpp_int.html). – PaulMcKenzie Apr 14 '20 at 06:12
  • There's also [GMP](https://gmplib.org/) (on Linux) and [MPIR](http://mpir.org/) (on Windows). – john Apr 14 '20 at 06:15
  • A temporary value still has a data type, in this case double, a double can normally only represent integers up to 2**53 without losing precision – Alan Birtles Apr 14 '20 at 06:29

0 Answers0