c ++中long double中小数点后允许的最大位数

时间:2016-10-15 05:51:46

标签: c++ types double precision

我解决了问题访问http://www.spoj.com/problems/CHI_ROOT/

(找到数字的第n个根并打印结果的前101个有效数字 没有小数点,尾随和前导零)使用long double

sizeof(long double)=12;

因此为long double变量分配的总位数不是96位(12 * 8)。

变量可存储的最大值约为10 ^ 28。

我想知道变量在小数点后可以存储的最大位数 它是如何存储的?

1 个答案:

答案 0 :(得分:1)

您可以查询the numeric_limits class template<cfloat> macros以获取可以准确显示的点后的有效小数位数:

for (int i = 0; i == 10; i++) { // here i==10 is never true as i=0
                                // so it should be i<10 or i <=10
    if (numberList.contains(input.nextInt())) {

    } else {
        numberList.add(input.nextInt());
    }
}

在x86上,#include <limits> #include <iostream> int main() { std::cout << std::numeric_limits<float>::digits10 << std::endl; std::cout << FLOAT_DIG << std::endl; // 6 std::cout << std::numeric_limits<double>::digits10 << std::endl; std::cout << DBL_DIG << std::endl; // 15 std::cout << std::numeric_limits<long double>::digits10 << std::endl; std::cout << LDBL_DIG << std::endl; // 18 } 通常以extended precision格式存储(只有80位,而不是96位;由于对齐,sizeof被填充为12个字节)。

请注意,位数远小于101.您应该找出一个算法来计算第n个根到任意精度。