C/C++——求下面資料型別的最大值和最小值: char, short, int, long, float, double, long double和numeric_limits使用

readyao發表於2016-02-24

numeric_limits參考標準庫介紹:http://www.cplusplus.com/reference/limits/numeric_limits/

下面是一個小例子:

// numeric_limits example
#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits

int main () {
  std::cout << std::boolalpha;
  std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
  std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
  std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
  std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
  std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
  return 0;
}

輸出:

Minimum value for int: -2147483648
Maximum value for int: 2147483647
int is signed: true
Non-sign bits in int: 31
int has infinity: false

求下面資料型別的最大值和最小值是什麼:char, short, int, long, float, double, long double, unsigned


#include <iostream>
#include <limits>
#include <climits>//可以不要,裡面有<span style="font-family: Arial, Helvetica, sans-serif;">CHAR_MAX 的定義</span>

using namespace std;

int main()
{
    cout << "char max = " << int(numeric_limits<char>::max()) << endl;
    cout << "char min = " << int(numeric_limits<char>::min()) << endl;

    //cout << "char max = " << CHAR_MAX << endl;
    //cout << "char min= " << CHAR_MIN << endl;

    cout << "short max = " << numeric_limits<short>::max() << endl;
    cout << "short min = " << numeric_limits<short>::min() << endl;

    cout << "int max = " << numeric_limits<int>::max() << endl;
    cout << "int min = " << numeric_limits<int>::min() << endl;

    cout << "long max = " << numeric_limits<long>::max() << endl;
    cout << "long min = " << numeric_limits<long>::min() << endl;

    cout << "float max = " << numeric_limits<float>::max() << endl;
    cout << "float min = " << numeric_limits<float>::min() << endl;

    cout << "double max = " << numeric_limits<double>::max() << endl;
    cout << "double min = " << numeric_limits<double>::min() << endl;

    cout << "long double max = " << numeric_limits<long double>::max() << endl;
    cout << "long double min = " << numeric_limits<long double>::min() << endl;

    cout << "unsigned max = " << numeric_limits<unsigned>::max() << endl;
    cout << "unsigned min = " << numeric_limits<unsigned>::min() << endl;

    return 0;
}


需要注意到的是:

    cout << "char max = " << int(numeric_limits<char>::max()) << endl;
    cout << "char min = " << int(numeric_limits<char>::min()) << endl;

輸出:

char max = 127
char min = -128
short max = 32767
short min = -32768
int max = 2147483647
int min = -2147483648
long max = 9223372036854775807
long min = -9223372036854775808
float max = 3.40282e+38
float min = 1.17549e-38
double max = 1.79769e+308
double min = 2.22507e-308
long double max = 1.18973e+4932
long double min = 3.3621e-4932
unsigned max = 4294967295
unsigned min = 0

min() T Minimum finite value.
For floating types with denormalization (variable number of exponent bits): minimum positive normalized value.
Equivalent to CHAR_MINSCHAR_MINSHRT_MININT_MINLONG_MINLLONG_MINFLT_MIN,DBL_MINLDBL_MIN or 0, depending on type.
max() T Maximum finite value.
Equivalent to CHAR_MAXSCHAR_MAXUCHAR_MAXSHRT_MAXUSHRT_MAXINT_MAXUINT_MAX,LONG_MAXULONG_MAXLLONG_MAXULLONG_MAXUINT_LEAST16_MAXUINT_LEAST32_MAXFLT_MAX,DBL_MAX or LDBL_MAX, depending on type.

int(numeric_limits<char>::max()) == CHAR_MAX

int(numeric_limits<char>::min()) == CHAR_MIN

相關文章