C++学习笔记八:极限和数学运算

发布时间 2023-12-12 18:04:53作者: Sternenhimmel1997

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 源文档:

https://en.cppreference.com/w/cpp/header/cmath

 

2.2 部分库函数:

std::abs(): 绝对值

std::exp(): e的乘方

std::pow(a,b): a的b次方

std::log(): e的对数

std::log10(): 10的对数

std::sqrt(): 开平方根

std::round(): 四舍五入

三角函数(单位是弧度制):sin(), sinf(float num), sinl(long double number)