1) <limits>庫(kù):
1.1 源文檔:
https://en.cppreference.com/w/cpp/types/numeric_limits
#include <limits>
?
1.2 庫(kù)函數(shù):
函數(shù)解釋:
對(duì)于一個(gè)浮點(diǎn)數(shù),lowest表示最小的可表示的負(fù)數(shù),min表示最小的可表示的接近0的數(shù),max表示最大的可表示的正數(shù)
對(duì)于一個(gè)有符號(hào)整數(shù),min表示可以表示的最小的負(fù)數(shù),max表示可以表示的最大的證書
std::cout << "The range for short is from " << std::numeric_limits<short>::min() << " to "
<< std::numeric_limits<short>::max() << std::endl;
std::cout << "The range for unsigned short is from " << std::numeric_limits<unsigned short>::min() << " to "
<< std::numeric_limits<unsigned short>::max() << std::endl;
std::cout << "The range for int is from " << std::numeric_limits<int>::min() << " to "
<< std::numeric_limits<int>::max() << std::endl;
std::cout << "The range for unsigned int is from " << std::numeric_limits<unsigned int>::min() << " to "
<< std::numeric_limits<unsigned int>::max() << std::endl;
std::cout << "The range for long is from " << std::numeric_limits<long>::min() << " to "
<< std::numeric_limits<long>::max() << std::endl;
std::cout << "The range for float is from " << std::numeric_limits<float>::min() << " to "
<< std::numeric_limits<float>::max() << std::endl;
std::cout << "The range(with lowest) for float is from " << std::numeric_limits<float>::lowest() << " to "
<< std::numeric_limits<float>::max() << std::endl;
std::cout << "The range(with lowest) for double is from " << std::numeric_limits<double>::lowest() << " to "
<< std::numeric_limits<double>::max() << std::endl;
std::cout << "The range(with lowest) for long double is from " << std::numeric_limits<long double>::lowest() << " to "
<< std::numeric_limits<long double>::max() << std::endl;
//Other facilities
//More info : https://en.cppreference.com/w/cpp/types/numeric_limits
std::cout << "int is signed : " << std::numeric_limits<int>::is_signed << std::endl;
std::cout << "int digits : " << std::numeric_limits<int>::digits << std::endl; //digits is the number of digits in base-radix that can be represented by the type T without change. For integer types, this is the number of bits not counting the sign bit and the padding bits
輸出結(jié)果:
The range for short is from -32768 to 32767
The range for unsigned short is from 0 to 65535
The range for int is from -2147483648 to 2147483647
The range for float is from 1.17549e-38 to 3.40282e+38
The range(with lowest) for float is from -3.40282e+38 to 3.40282e+38
The range(with lowest) for double is from -1.79769e+308 to 1.79769e+308 ? ? ?
The range(with lowest) for long double is from -1.18973e+4932 to 1.18973e+4932
int is signed : 1
int digits : 31
?
2)<cmath>庫(kù)
2.1 源文檔:
#include <cmath>
https://en.cppreference.com/w/cpp/header/cmath
?
2.2 部分庫(kù)函數(shù):
std::abs(a): 絕對(duì)值
std::exp(a): e的乘方
std::pow(a,b): a的b次方
std::log(a): e的對(duì)數(shù)
std::log10(a): 10的對(duì)數(shù)
std::sqrt(a): 開(kāi)平方根
std::round(a): 四舍五入
三角函數(shù)(單位是弧度制):sin(), sinf(float num), sinl(long double number)
?
2.3 對(duì)char類型和short int類型的數(shù)學(xué)計(jì)算:
編譯器無(wú)法處理小于4bytes的數(shù)據(jù)的計(jì)算,char類型占據(jù)1 Byte,short int類型占據(jù)2 Bytes, 在進(jìn)行運(yùn)算時(shí)會(huì)自動(dòng)轉(zhuǎn)換為int類型文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-750782.html
short int var1 {10}; // 2 bytes
short int var2 {20};
char var3 {40}; //1
char var4 {50};
std::cout << "size of var1 : " << sizeof(var1) << std::endl;
std::cout << "size of var2 : " << sizeof(var2) << std::endl;
std::cout << "size of var3 : " << sizeof(var3) << std::endl;
std::cout << "size of var4 : " << sizeof(var4) << std::endl;
auto result1 = var1 + var2 ;
auto result2 = var3 + var4;
std::cout << "size of result1 : " << sizeof(result1) << std::endl; // 4
std::cout << "size of result2 : " << sizeof(result2) << std::endl; // 4
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-750782.html
到了這里,關(guān)于C++學(xué)習(xí)筆記八:極限和數(shù)學(xué)運(yùn)算<limits><cmath>的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!