C++学习笔记八:极限和数学运算<limits><cmath>

这篇具有很好参考价值的文章主要介绍了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

 文章来源地址https://www.toymoban.com/news/detail-750782.html

到了这里,关于C++学习笔记八:极限和数学运算<limits><cmath>的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Vue源码学习(二):<templete>渲染第一步,模板解析

    好家伙,   在正式内容之前,我们来思考一个问题, 当我们使用vue开发页面时, tamplete中的内容是如何变成我们网页中的内容的 ?   它会经历四步: 解析模板:Vue会解析 template 中的内容,识别出其中的指令、插值表达式( {{}} ),以及其他元素和属性。 生成AST:解析模板后,

    2024年02月09日
    浏览(28)
  • Rust中的智能指针:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak<T>

    智能指针(smart pointers)是一类数据结构,是拥有数据所有权和额外功能的指针。是指针的进一步发展 指针(pointer)是一个包含内存地址的变量的通用概念。这个地址引用,或 ” 指向”(points at)一些其 他数据 。引用以 符号为标志并借用了他们所 指向的值。除了引用数据

    2023年04月20日
    浏览(46)
  • 详解dedecms后台编辑器将回车<br>改为<p>的方法

    DEDECMS编辑器默认回车[确认键]是返回 这样的。 有时候我们需要返回 这样的,今天我们就讲讲后台编辑器将回车将 改为 的有效教程吧。 方法也很简单,首先我们找到dedecms后来台编辑器的配置文件。 所在路径为:/include/ckeditor/config.js 我们打开它,里面有两个字: 替换为 这

    2024年02月02日
    浏览(39)
  • <html>

    在iOS开发中,经常会涉及到支付功能,这里对常见的微信支付做一下详细说明 微信开放平台(微信支付须要付费的。注冊成功后使用的主要为AppKey/SecretKey,当中SecrectKey交由后台完毕集成) 微信开放平台是商户APP接入微信支付开放接口的申请入口。通过此平台可申请微信APP支付。

    2024年02月08日
    浏览(28)
  • class<T extends interface> 或 class<T extends abstract class>

    Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。 类型通配符一般是使用 ? 代替具体的类型参数 要声明一个有界的类型参数,首先列出类型参数的名称,后跟extends,最后紧跟它的上界

    2024年02月12日
    浏览(26)
  • 将较大的数组赋值<el-table></el-table>时,会导致页面卡顿,甚至崩溃

    遇到的问题:将长度为40的数组数据赋值el-table/el-table,我发现loading没有效果,后面发现是页面卡住了,loading直接没有出现。 经过查询资料,发现el-table会有卡顿的问题,看到有的博主推荐使用一款叫umy-ui的插件,我就试了试,发现卡顿的问题解决了。 官网: http://www.umyui

    2024年02月05日
    浏览(42)
  • ChatGPT还是有点东西的-public static <T> List<T> Arrays.asList(T... a) {...}

    业务开发需要判断业务状态是否在30、40、50、60的集合内,所以写了以下代码 自我Review代码时,验证了下这行代码,发现状态为30时,仍然返回false。 在自我怀疑中调整代码,并验证,代码如下: 没想很明白,于是问了下ChatGPT。 Arrays.asList() 方法返回的结果类型取决于传入的

    2024年02月12日
    浏览(31)
  • protolator - Protobuf <==> json

    github.com/hyperledger/fabric-config/protolator 是 Hyperledger Fabric 中的一个 Go 包,用于将 Protocol Buffers(ProtoBuf)消息和 JSON 格式之间进行转换。它提供了一种方便的方式来将 Fabric 配置文件(以 ProtoBuf 格式表示)与 JSON 配置文件之间进行相互转换。这对于 Fabric 的配置管理和部署非常有

    2024年02月15日
    浏览(26)
  • 记录--新的HTML标签 :<search>

    本文介绍了一种新的HTML元素搜索方法,并提供了一个实用的工具来帮助开发者快速找到所需的元素。这对于那些需要处理大量HTML元素的开发者来说是非常有用的。文章还通过提供一些常见元素的用法示例,帮助开发者更好地理解和应用这些元素。在众多元素中找到特定的元

    2024年02月09日
    浏览(34)
  • <四>move移动语义和forward类型转发

    move : 移动语义,得到右值类型 forward:类型转发,能够识别左值和右值类型 只有两种形式的引用,左值引用和右值引用,万能引用不是一种引用类型,它存在于模板的引用折叠情况,但是能够接受左值和右值 区分左值和右值得一个简单方式就是能不能取地址 一个右值一旦有名字那

    2024年02月02日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包