1) <limits>库:
1.1 源文档:
https://en.cppreference.com/w/cpp/types/numeric_limits
#include <limits>
1.2 库函数:
函数解释:
对于一个浮点数,lowest表示最小的可表示的负数,min表示最小的可表示的接近0的数,max表示最大的可表示的正数
对于一个有符号整数,min表示可以表示的最小的负数,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
输出结果:
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>库
2.1 源文档:
#include <cmath>
https://en.cppreference.com/w/cpp/header/cmath
2.2 部分库函数:
std::abs(a): 绝对值
std::exp(a): e的乘方
std::pow(a,b): a的b次方
std::log(a): e的对数
std::log10(a): 10的对数
std::sqrt(a): 开平方根
std::round(a): 四舍五入
三角函数(单位是弧度制):sin(), sinf(float num), sinl(long double number)
2.3 对char类型和short int类型的数学计算:
编译器无法处理小于4bytes的数据的计算,char类型占据1 Byte,short int类型占据2 Bytes, 在进行运算时会自动转换为int类型文章来源:https://www.toymoban.com/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
文章来源地址https://www.toymoban.com/news/detail-750782.html
到了这里,关于C++学习笔记八:极限和数学运算<limits><cmath>的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!