超过 C++ 的最大整数数据类型
Exceeding C++'s largest integer datatype
我正在编写一个组合计算器,对于较大的计算,我最终会遇到long long int
或int64_t
溢出。至少有可能把这个数字转换成这样的数字吗:6.7090373691429E+19?
这是我的代码:
#include <iostream>
#include <string.h>
#include <math.h>
int main() {
std::string charset;
int i, length; int64_t total = 0;
std::cout << "Charset: ";
std::cin >> charset;
std::cout << "Length: ";
std::cin >> length;
for (i=0;i<(length+1);i++) {
total += pow(charset.size(),i);
}
std::cout << "nPossible combinations: " << total << std::endl;
return 0;
}
C++标准库不包括任意大小的整数类型。
您可以使用Boost Multiprecision。它有不同的后端,使用专用库(如GMP(和没有外部依赖关系的自定义后端(cpp_int(。
编辑:公平地说,微软已经在评论中提到了Boost Multiprecision。
相关文章:
- 防止主数据类型C++的隐式转换
- 什么是 16 字节有符号整数数据类型?
- C++11 中是否有实际的 4 位整数数据类型
- 创建一个带有限制的数字数据类型,一个介于 -10 和 10 之间的整数
- 如何仅接受整数并忽略其他数据类型
- 编译器是否有可能检测整数溢出或其他数据类型溢出的可能性
- 1 字节整数数据类型
- 对应于 AS3 编号(整数)的C++数据类型是什么?
- 小数、双精度、整数等数据类型的范围
- 在整数数据类型和连续两个字符类型之后.第 2 个字符的数据类型跳过..为什么
- C++如何将整数和浮点等数据类型封送为字节字符串
- C++字符串 DD:HH:MM:SS 到整数数据类型
- 表示本机有符号和无符号整数大小的数据类型
- 有没有一种方法可以为非整数创建枚举数据类型
- 对于一般用途的整数,我应该使用什么数据类型?
- 布尔数据类型vs整数数据类型来表示真值
- C++11 在map<key,value>中存储值(整数和字符串)的多种数据类型的最简单方法?
- 如果我将数字存储为整数数据类型,如何检查另一个数字中是否存在一个数字
- 从双精度数据类型获取特定整数的方法
- 超过 C++ 的最大整数数据类型