C/C++——求下面資料型別的最大值和最小值: char, short, int, long, float, double, long double和numeric_limits使用
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_MIN, SCHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN, FLT_MIN,DBL_MIN, LDBL_MIN or 0 ,
depending on type. |
max() | T | Maximum finite value. Equivalent to CHAR_MAX, SCHAR_MAX, UCHAR_MAX, SHRT_MAX, USHRT_MAX, INT_MAX, UINT_MAX,LONG_MAX, ULONG_MAX, LLONG_MAX, ULLONG_MAX, UINT_LEAST16_MAX, UINT_LEAST32_MAX, FLT_MAX,DBL_MAX or LDBL_MAX, depending on type. |
int(numeric_limits<char>::max()) == CHAR_MAX
相關文章
- c++ 基本資料型別(int、float、double、long、long long)最大值,最小是表示方法C++資料型別
- C/C++中各種型別int、long、double、char表示範圍(最大最小值)C++型別
- C/C++_int/float/double的最大值和最小值C++
- int float double 各型別的最大值最小值型別
- C語言中關於float、double、long double精度及數值範圍理解C語言
- int型別和long long型別運算執行時間的差別型別
- float型別最大值和最小值型別
- CAPL指令碼中常用到的資料型別轉換——數字型別(int/double)和字串型別(char array)指令碼資料型別字串
- C語言 int,float,double整型和浮點型資料在相互運算時bug原因C語言
- 準確詳解:C/C++ float、double資料型別的表示範圍及精度C++資料型別
- Java long型別和Long型別的那些事Java型別
- c語言中%f輸出double型和float型值C語言
- 關於MYSQL中FLOAT和DOUBLE型別的儲存MySql型別
- float和double有什麼區別?
- java double、float型別的比較Java型別
- Long raw和Long型別總結型別
- decimal,float和double的區別是什麼?Decimal
- C++11獲取double型別的最大最小值C++型別
- PLSQL Language Reference-PL/SQL資料型別-SQL資料型別-LONG和LONG RAW變數SQL資料型別變數
- int/double資料範圍
- 測試Java中的long,int基本型別Java型別
- Python 四種數值型別(int,long,float,complex)區別及轉換Python型別
- 【基礎語法】short、int、long轉為byte
- long型別資料的擷取型別
- 【PL/SQL】IEEE浮點型別BINARY_FLOAT和BINARY_DOUBLESQL型別
- c++中getopt和getopt_long的使用方法C++
- c# (int)double顯式轉化C#
- c++ 16進位制資料轉doubleC++
- 操作LONG型別型別
- ora-00997 非法使用LONG資料型別資料型別
- ORA-00997: 非法使用 LONG 資料型別資料型別
- sql server資料庫如何儲存陣列,int[]float[]double[]陣列儲存到資料庫方法SQLServer資料庫陣列
- Long型別的資料,利用COPY命令遷移型別
- Java中long和Long有什麼區別 (轉載)Java
- LONG型別複製型別
- long型別相關型別
- long資料型別跨平臺問題資料型別
- longValue( )和Long.valueOf( )的區別