贴心的limits...
测试代码:
#include <iostream> #include <stdio.h> #include <limits> #include <math.h> using namespace std; int main() { //double 有效数字16位 double test3 = 1.2345678912345678e17; printf("%.17lf ", test3); test3 = 1.23456789123456789123e17; printf("%.17lf ", test3); int a = (int)pow(2, 32); cout << "int 2^32: " << a << "=== "; int b = (int)pow(2, 64); cout << "long long to int: " << b << "=== "; long long c = (long long)pow(2, 64); cout << "long long 2^64: " << c << "=== "; cout << "type ---" << "+++++++++++++++size+++++++++++++++ "; cout << "bool ---" << "size:" << sizeof(bool) << " max:" << (numeric_limits<bool>::max()) << " min:" << numeric_limits<bool>::min() << endl; cout << "int ---" << "size:" << sizeof(int) << " max:" << (numeric_limits<int>::max()) << " min:" << numeric_limits<int>::min() << endl; cout << "long long---" << "size:" << sizeof(long long) << " max:" << (numeric_limits<long long>::max()) << " min:" << numeric_limits<long long>::min() << endl; cout << "double ---" << "size:" << sizeof(double) << " max:" << (numeric_limits<double>::max()) << " min:" << numeric_limits<double>::min() << endl; cout << "long ---" << "size:" << sizeof(long) << " max:" << (numeric_limits<long>::max()) << " min:" << numeric_limits<long>::min() << endl; }
运行:
其中:关于double
double就是IEEE754的64位浮点数
1位符号位
11位指数位
52位尾数位
即 精确到52位2进制位。
也就是说,精确到log(2^52)/log(10) = 15.6535597 位10进制位。
然后,float和double的精度是由尾数的位数来决定的。浮点数在内存中是按科学计数法来存储的,其整数部分始终是一个隐含着的“1”,
由于它是不变的,故不能对精度造成影响。
所以,有效数字是15-16位,没有精确到小数点后几位之说。【大概是?T_T】
然后附偷来的详细一点的: