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 是脚本本身的名字 $ ...
随机推荐
- CodeForces 1324F Maximum White Subtree
题意 给定一棵\(n\)个节点的无根树,每个节点为黑色或者白色,每个点的答案为包含该点的子树(指无根子树)的白色节点数减黑色节点数的最大值 分析 对于无根树的题一般指定某一个点为根,不妨设为\(1\) ...
- gitlab与LDAP 联调
gitlab整理 目录 gitlab整理 1.安装Gitlab依赖包 2.下载,安装 3.配置,访问域名及邮箱 4.初始化,启动 5.访问,以及邮箱测试 5.1汉化 6.问题总结处理 6.1安装时出现 ...
- 【matplotlib基础】--手绘风格
Matplotlib 中有一个很有趣的手绘风格.如果不是特别严肃的分析报告,使用这个风格能给枯燥的数据分析图表带来一些活泼的感觉. 使用手绘风格非常简单,本篇主要手绘风格的效果以及如何配置中文的支持. ...
- 地理探测器Geodetector下载、使用、结果分析方法
本文介绍Geodetector软件的下载方法,以及地理探测器分析的完整操作,并对其结果加以解读. 首先,我们介绍Geodetector软件的下载方法.进入软件官网,可以看到其中的第四个部分为软 ...
- linux查看IP、域名、端口的网络是否相通
linux查看IP.域名.端口的网络是否相通 1. ping # 检索当前域名对应的IP地址 ping 域名 # 查看IP是否相通 ping IP 2. tlenet # 查看指定IP的端口是否相通, ...
- Llama2-Chinese项目:2.3-预训练使用QA还是Text数据集?
Llama2-Chinese项目给出pretrain的data为QA数据格式,可能会有疑问pretrain不应该是Text数据格式吗?而在Chinese-LLaMA-Alpaca-2和open-l ...
- 树莓派3B/3B+的串口使用
树莓派包含两个串口,一个称之为硬件串口(/dev/ttyAMA0),一个称之为mini串口(/dev/ttyS0).硬件串口由硬件实现,有单独的波特率时钟源,性能高.可靠.mini串口时钟源是由CPU ...
- QFluentWidgets: 基于 C++ Qt 的 Fluent Design 组件库
简介 QFluentWidgets 是一个基于 Qt 的 Fluent Designer 组件库,内置超过 150 个开箱即用的 Fluent Designer 组件,支持亮暗主题无缝切换和自定义主题 ...
- [知识管理] Obsidian + Remotely Save插件 + 第三方存储/OSS(七牛云)的同步方案
0 序言 在几经选择.对比之后,我选择:Obsidian + Remotely Save插件 + 第三方存储/OSS(七牛云) 的方案来搭建自己的[知识管理系统]. 对比分析知识管理工具的过程,详情参 ...
- PostgreSQL学习笔记-1.基础知识:创建、删除数据库和表格
PostgreSQL 创建数据库 PostgreSQL 创建数据库可以用以下三种方式:1.使用 CREATE DATABASE SQL 语句来创建.2.使用 createdb 命令来创建.3.使用 p ...