C++ 关于数值极限

1 分钟阅读

关于数值极限

#include <limits>
#include <iostream>

int main()
{
    std::cout << "type\t\t\tlowest()\t\tmin()\t\t\tmax()\n\n";

    std::cout << "short\t\t\t"
              << std::numeric_limits<short>::lowest() << "\t\t\t"
              << std::numeric_limits<short>::min() << "\t\t\t"
              << std::numeric_limits<short>::max() << "\n";
    std::cout << "int\t\t\t"
              << std::numeric_limits<int>::lowest() << "\t\t"
              << std::numeric_limits<int>::min() << "\t\t"
              << std::numeric_limits<int>::max() << "\n";
    std::cout << "long\t\t\t"
              << std::numeric_limits<long>::lowest() << "\t\t"
              << std::numeric_limits<long>::min() << "\t\t"
              << std::numeric_limits<long>::max() << "\n";
    std::cout << "long long\t\t"
              << std::numeric_limits<long long>::lowest() << "\t"
              << std::numeric_limits<long long>::min() << "\t"
              << std::numeric_limits<long long>::max() << "\n";
    std::cout << "unsigned short\t\t"
              << std::numeric_limits<unsigned short>::lowest() << "\t\t\t"
              << std::numeric_limits<unsigned short>::min() << "\t\t\t"
              << std::numeric_limits<unsigned short>::max() << "\n";
    std::cout << "unsigned int\t\t"
              << std::numeric_limits<unsigned int>::lowest() << "\t\t\t"
              << std::numeric_limits<unsigned int>::min() << "\t\t\t"
              << std::numeric_limits<unsigned int>::max() << "\n";
    std::cout << "unsigned long\t\t"
              << std::numeric_limits<unsigned long>::lowest() << "\t\t\t"
              << std::numeric_limits<unsigned long>::min() << "\t\t\t"
              << std::numeric_limits<unsigned long>::max() << "\n";
    std::cout << "unsigned long long\t"
              << std::numeric_limits<unsigned long long>::lowest() << "\t\t\t"
              << std::numeric_limits<unsigned long long>::min() << "\t\t\t"
              << std::numeric_limits<unsigned long long>::max() << "\n";


    system("pause");
}

64 位

在64位系统上Visual Studio 输出结果:

1592038101047

换为2的幂级数表示为(64位系统下):

type lowest() min() max()
short -32768 -32768 32767
int -2147483648 -2147483648 2147483647
long -2147483648 -2147483648 2147483647
long long -9223372036854775808 -9223372036854775808 9223372036854775807
unsigned short 0 0 65535
unsigned int 0 0 4294967295
unsigned long 0 0 4294967295
unsigned long long 0 0 18446744073709551615

32 位

在32位系统上Visual Studio 输出结果:

1592038101047

type lowest() min() max()
short -32768 -32768 32767
int -2147483648 -2147483648 2147483647
long -2147483648 -2147483648 2147483647
long long -9223372036854775808 -9223372036854775808 9223372036854775807
unsigned short 0 0 65535
unsigned int 0 0 4294967295
unsigned long 0 0 4294967295
unsigned long long 0 0 18446744073709551615

标签:

分类:

更新时间: