Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

tom_int

#include <boost/multiprecision/tommath.hpp>

namespace boost{ namespace multiprecision{

class tommath_int;

typedef number<tommath_int >         tom_int;

}} // namespaces

The tommath_int back-end is used via the typedef boost::multiprecision::tom_int. It acts as a thin wrapper around the libtommath tom_int to provide an integer type that is a drop-in replacement for the native C++ integer types, but with unlimited precision.

Things you should know when using this type:

Example:
#include <boost/multiprecision/tommath.hpp>
#include <iostream>

int main()
{
   boost::multiprecision::tom_int v = 1;

   // Do some arithmetic:
   for(unsigned i = 1; i <= 1000; ++i)
      v *= i;

   std::cout << v << std::endl; // prints 1000!
   std::cout << std::hex << v << std::endl; // prints 1000! in hex format

   try{
      std::cout << std::hex << -v << std::endl; // Ooops! can't print a negative value in hex format!
   }
   catch(const std::runtime_error& e)
   {
      std::cout << e.what() << std::endl;
   }

   try{
      // v is not a 2's complement type, bitwise operations are only supported
      // on positive values:
      v = -v & 2;
   }
   catch(const std::runtime_error& e)
   {
      std::cout << e.what() << std::endl;
   }

   return 0;
}

PrevUpHomeNext