概述

  • math.constants库提供了π\piπ、eee、 2\sqrt[ ]{2 }2​ 等常用的数学常数,支持float、double、long double精度,而且还支持自定义类型以获得更高的精度
  • math.constants库位于名字空间boost::math,需要包含头文件<boost/math/constants/constants.hpp>,即:
#include <boost/math/constants/constants.hpp>
using namespace boost::math;

基本用法

math.constants库定义了很多科学计算中用到的常数,精度高达小数点后100位,比较常用的有:

  • pi : 圆周率π\piπ
  • e : 自然对数的底
  • root_tow : 2\sqrt[ ]{2 }2​
  • root_three : 3\sqrt[ ]{3 }3​
  • in_two : ln⁡2\ln{2}ln2

等等,这些都是编译期常数,没有运行时开销,所以运算效率很高

为了方便使用,math.constants库在名字空间boost::math里有定义了三个子名字空间,分别是float_constants、double_constants和long_double_constants,我们可以直接使用这些名字空间里对应的常数。

示例:

#include <iostream>
using std::cout;
using std::endl;#include <boost/math/constants/constants.hpp>
using namespace boost::math;//void case1()
{cout << std::setprecision(64);         //设置显示精度位64位auto a = float_constants::pi * 2 * 2;cout << "area \t\t= " << a << endl;using namespace double_constants;auto x = root_two * root_three;cout << "root 2 * 3 \t= " << x << endl;cout << "root pi \t= " << root_pi << endl;cout << "pi pow e \t= " << pi_pow_e << endl;
}int main()
{case1();
}

高级用法

math.constants库模仿C++14里的模板变量特性,在boost::math::constants里定义了同名的函数模板,可以不必使用名字空间直接指定数值类型。如果我们使用另一个数学库----高精度数据库boost.multiprecision,那么就可以获得比long double还要高的精确度:

#include <iostream>
using namespace std;#include <boost/math/constants/constants.hpp>
using namespace boost::math;#include <boost/multiprecision/cpp_dec_float.hpp>
void case1()
{using namespace constants;typedef decltype(pi<float>) pi_t;assert(is_function<pi_t>::value);  assert(pi<float>() == float_constants::pi);  // 函数模板获取pi值assert(pi<double>() == double_constants::pi); // double精度typedef boost::multiprecision::cpp_dec_float_100 float_100;cout << setprecision(100)         // 设置精度位100位<< pi<float_100>() << endl;   // 100位小数浮点数
}int main()
{case1();
}

boost:math.constants相关推荐

  1. boost:从0到1开发boost(linux、clion)

    boost 1.安装包下载 下载地址 选一个就好 并将安装包放到centos的适当目录下 2.设置编译器和所选库 解压 $ tar -xvf boost_1_74_0_b1.tar.bz2 运行解压后 ...

  2. 8:Math.atan2、PathMeasure、点赞飘心效果、点赞数字滚动+1效果、集成支付

    1.Math.atan()与Math.atan2()的区别 angle = Math.atan(slope) 复制代码 slope斜率值为y/x,返回值angle为一个角度的弧度制,因为角度的周期性, ...

  3. boost::geometry::math::equals用法的测试程序

    boost::geometry::math::equals用法的测试程序 实现功能 C++实现代码 实现功能 boost::geometry::math::equals用法的测试程序 C++实现代码 ...

  4. boost : has_to_string的用法测试程序

    boost : has_to_string的用法测试程序 实现功能 C++实现代码 实现功能 boost : has_to_string的用法测试程序 C++实现代码 #include <boo ...

  5. boost:stl概念检查的测试程序

    boost:stl概念检查的测试程序 实现功能 C++实现代码 实现功能 boost:stl概念检查的测试程序 C++实现代码 #include <boost/concept_check.hpp ...

  6. Boost:Boost概念的function_requires()的测试程序

    Boost:Boost概念的function_requires()的测试程序 实现功能 C++实现代码 实现功能 Boost:Boost概念的function_requires()的测试程序 C++实 ...

  7. boost:验证BOOST_CONCEPT_ASSERT捕获了错误

    boost:验证BOOST_CONCEPT_ASSERT捕获了错误 实现功能 C++实现代码 实现功能 boost:验证BOOST_CONCEPT_ASSERT捕获了错误 C++实现代码 #ifdef ...

  8. boost:验证Boost概念检查的class_requires 库应该在预期的时间内捕获错误

    boost:验证Boost概念检查的class_requires 库应该在预期的时间内捕获错误 实现功能 C++实现代码 实现功能 boost:验证Boost概念检查的class_requires 库 ...

  9. boost:验证Boost的BOOST_CLASS_REQUIRE宏

    boost:验证Boost的BOOST_CLASS_REQUIRE宏 实现功能 C++实现代码 实现功能 boost:验证Boost的BOOST_CLASS_REQUIRE宏 C++实现代码 #inc ...

  10. Boost:可移植地自定义boost :: hash的测试程序

    Boost:可移植地自定义boost :: hash的测试程序 实现功能 C++实现代码 实现功能 Boost的container_hash模块,可移植地自定义boost :: hash的测试程序 C ...

最新文章

  1. Java-- String源码分析
  2. SpringMvc项目加载顺序及上下文小结
  3. PHP中foreach遍历循环的使用(两种用法)
  4. Spring Boot : ApplicationListner
  5. logstash grok切分nginx日志
  6. 通过二进制方式安装innobackupex
  7. Bailian2966 时区转换【时区计算】
  8. linux mint php mysql_Installing LAMP (Linux, Apache, MySQL and PHP) On Linux Mint
  9. 小程序 省市区地址选择器
  10. linux输出替换字符串,Linux命令之sed-替换字符串
  11. 万达电商为何刻意回避阿里与马云
  12. mysql_帮助命令/通配搜索/help help用法(official doc)
  13. Labview的子VI
  14. setenv: command not found
  15. 中国游戏媒体市场动态前景与竞争策略分析报告(2021-2026年)
  16. 面阵相机靶面详解and镜头选择andFA镜头视野计算
  17. 搞不定思想和组织转变,何谈企业转型
  18. C#从入门到精通学习笔记——(2)
  19. 多语言翻译插件 支持android/IOS
  20. “铁路之声——自动播音系统”的应用操作演示

热门文章

  1. kangle安装php7.0_搭建Kangle+EasyPanel对接SWAP IDC虚拟主机自助开通完整教程
  2. 颠覆性创始人Tony Delgado在波多黎各启动编码训练营
  3. android apk 应用分发平台
  4. craig gentry_为Craig投票!
  5. ESP8266获取心知天气
  6. EMCP 物联网云平台短信报警使用说明
  7. 航空摄影与正射摄影的区别
  8. Python系列17-数据可视化之下载数据
  9. 用p5.js绘制创意自画像
  10. MySQL中explain用法含义说明