Storing and printing 10+ digit integer in c++

后端 未结 8 1695
北荒
北荒 2020-12-21 14:39

I\'m using cout to print digits to the console. I am also storing values of up to 13+billion as a digit and doing computations on it. What data type should I use?

Wh

相关标签:
8条回答
  • 2020-12-21 15:37

    just use double in the declaration statement

    0 讨论(0)
  • 2020-12-21 15:38

    Just a note that both int64_t and long long are included in C99 and in C++ 0x, but not in the current version of C++. As such, using either does put your code at some risk of being non-portable. Realistically, however, that risk is probably already pretty low -- to the point that when/if you port your code, there are likely to be much bigger problems.

    If, however, you really want to assure against that possibility, you might consider using a double precision floating point. Contrary to popular belief, floating point types can represent integers exactly up to a certain limit -- that limit being set (in essence) by the size of the mantissa in the F.P. type. The typical implementation of a double has a 53-bit mantissa, so you can represent 53-bit integers with absolute precision. That supports numbers up to 9,007,199,254,740,992 (which is substantially more than 13 of either of the popular meanings of "billion").

    0 讨论(0)
提交回复
热议问题