C++学习笔记八:极限和数学运算<limits><cmath>
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类型
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
C++学习笔记八:极限和数学运算<limits><cmath>的更多相关文章
- Learning ROS forRobotics Programming Second Edition学习笔记(八)indigo rviz gazebo
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
- python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑
python3.4学习笔记(八) Python第三方库安装与使用,包管理工具解惑 许多人在安装Python第三方库的时候, 经常会为一个问题困扰:到底应该下载什么格式的文件?当我们点开下载页时, 一般 ...
- Go语言学习笔记八: 数组
Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...
- 【opencv学习笔记八】创建TrackBar轨迹条
createTrackbar这个函数我们以后会经常用到,它创建一个可以调整数值的轨迹条,并将轨迹条附加到指定的窗口上,使用起来很方便.首先大家要记住,它往往会和一个回调函数配合起来使用.先看下他的函数 ...
- go微服务框架kratos学习笔记八 (kratos的依赖注入)
目录 go微服务框架kratos学习笔记八(kratos的依赖注入) 什么是依赖注入 google wire kratos中的wire Providers injector(注入器) Binding ...
- Redis学习笔记八:集群模式
作者:Grey 原文地址:Redis学习笔记八:集群模式 前面提到的Redis学习笔记七:主从复制和哨兵只能解决Redis的单点压力大和单点故障问题,接下来要讲的Redis Cluster模式,主要是 ...
- Java IO学习笔记八:Netty入门
作者:Grey 原文地址:Java IO学习笔记八:Netty入门 多路复用多线程方式还是有点麻烦,Netty帮我们做了封装,大大简化了编码的复杂度,接下来熟悉一下netty的基本使用. Netty+ ...
- 认真学习shell的第一天-数学运算
shell中的数学运算有三种方式: (1)let,用let的时候,变量名称前不用添加$ (2)[],[]中变量可使用也可不使用$ (3)(())变量名之前必须添加$
- TensorFlow学习笔记2——数据类型及简单运算
0. 小试牛刀 首先,激活tensorflow环境( source activate tensorflow ),随后在ipython里: import tensorflow as tf sess = ...
- linux学习:特殊符号,数学运算,图像与数组与部分终端命令用法整理
指令:let.expr.array.convert.tput.date.read.md5.ln.apt.系统信息 一:特殊符号用法整理 系统变量 $# 是传给脚本的参数个数 $0 是脚本本身的名字 $ ...
随机推荐
- 【AI绘画模型汇总】分享5个国内实用的AI绘画模型网站-C站AI模型平替网站
鉴于大家未必会有魔法工具访问civitai(C站)下载AI模型,这里我搜集整理了5个实用的国内版AI模型素材库,无障碍访问下载Stable diffusion模型. 1.LiblibAI 访问速度快, ...
- PyAV 使用浅谈
背景: PyAV是一个用于音频和视频处理的Python库,它提供了一个简单而强大的接口,用于解码.编码.处理和分析各种音频和视频格式.PyAV基于FFmpeg多媒体框架,它本质上是FFmpeg 的Py ...
- 干掉 CRUD!这个API开发神器效率爆炸,无需定义MVC类!!
简介 magic-api 能够只通过 UI 界面就能完成简单常用的接口开发,能够支持市面上多数的关系性数据库,甚至还支持非关系性数据库 MongoDB. 通过 magic-api 提供的 UI 界面完 ...
- WPF学习 - 用鼠标移动、缩放、旋转图片(2)- 使用MatrixTransform
在上一篇文章中,提到了以鼠标控制变换图片的方法. 这种方法在某种情况下可以,例如,直接在windows窗体上.但我发现,当把它封装到一个控件中的时候,它就不行了. 经过不断的尝试,我找到了一种更简单的 ...
- K8s 多集群实践思考和探索
作者:vivo 互联网容器团队 - Zhang Rong 本文主要讲述了一些对于K8s多集群管理的思考,包括为什么需要多集群.多集群的优势以及现有的一些基于Kubernetes衍生出的多集群管理架构实 ...
- 文心一言 VS 讯飞星火 VS chatgpt (89)-- 算法导论8.3 2题
二.用go语言,下面的排序算法中哪些是稳定的:插入排序.归并排序.堆排序和快速排序?给出一个能使任何排序算法都稳定的方法.你所给出的方法带来的额外时间和空间开销是多少? 文心一言: 在Go语言中,以下 ...
- Journey -「CQOI 2021」
Day -1 Thu. & Fri. 恰逢学校运动会,于是向班主任申请了不去,然后就在机房坐着.不美好的事情可能就是文化课老师还留了这两天的作业,不过-> 一旦放弃了作业,什么都好说了呢 ...
- HashMap底层源码分析
HashMap底层原理实现 1.HashMap初始化 jdk1.8版本之后:数组+链表+红黑树实现,先去观看HashMap的构造方法: 构造方法: public HashMap() { this.lo ...
- c语言代码练习13
//打印九九乘法表#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int x = 0; int y = ...
- 软件开发人员 Kubernetes 入门指南|Part 2
在第 1 部分中,我们讲解了 Kubernetes 的核心组件,Kubernetes 是一种开源容器编排器,用于在分布式环境中部署和扩展应用程序:我们还讲解了如何在集群中部署一个简单的应用程序,然后更 ...