超过芯片组允许的最大数据类型。 C ++

时间:2017-05-23 01:17:31

标签: c++ types embedded

我有一些超出c ++长数据类型的数学。什么是最好的解决方法?我在嵌入式芯片组上做基本的数学+,/和*。我在循环中将数字相加,然后将大数字分开。

1 个答案:

答案 0 :(得分:5)

通常,long(与long int相同)的大小至少为4个字节。但是,检查一下你自己。如果您发现long long int的字节数多于long int,并且您的值不超过long long int中可存储的最大值,请使用该值。使用#include <limits>并使用此标头内的函数来确定整数类型中可存储的最大值。

例如,

#include <limits>
#include <iostream>

int main()
{
    std::cout << std::numeric_limits<int>::max() << std::endl;
    std::cout << std::numeric_limits<long int>::max() << std::endl;
    std::cout << std::numeric_limits<long long int>::max() << std::endl;
}

这会在我的机器上输出以下内容:

2147483647
9223372036854775807
9223372036854775807

但是,如果您的值超出了long long int中可以存储的值,请使用大号库。