boost:math.constants
概述
- 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 }2root_three
: 3\sqrt[ ]{3 }3in_two
: ln2\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相关推荐
- boost:从0到1开发boost(linux、clion)
boost 1.安装包下载 下载地址 选一个就好 并将安装包放到centos的适当目录下 2.设置编译器和所选库 解压 $ tar -xvf boost_1_74_0_b1.tar.bz2 运行解压后 ...
- 8:Math.atan2、PathMeasure、点赞飘心效果、点赞数字滚动+1效果、集成支付
1.Math.atan()与Math.atan2()的区别 angle = Math.atan(slope) 复制代码 slope斜率值为y/x,返回值angle为一个角度的弧度制,因为角度的周期性, ...
- boost::geometry::math::equals用法的测试程序
boost::geometry::math::equals用法的测试程序 实现功能 C++实现代码 实现功能 boost::geometry::math::equals用法的测试程序 C++实现代码 ...
- boost : has_to_string的用法测试程序
boost : has_to_string的用法测试程序 实现功能 C++实现代码 实现功能 boost : has_to_string的用法测试程序 C++实现代码 #include <boo ...
- boost:stl概念检查的测试程序
boost:stl概念检查的测试程序 实现功能 C++实现代码 实现功能 boost:stl概念检查的测试程序 C++实现代码 #include <boost/concept_check.hpp ...
- Boost:Boost概念的function_requires()的测试程序
Boost:Boost概念的function_requires()的测试程序 实现功能 C++实现代码 实现功能 Boost:Boost概念的function_requires()的测试程序 C++实 ...
- boost:验证BOOST_CONCEPT_ASSERT捕获了错误
boost:验证BOOST_CONCEPT_ASSERT捕获了错误 实现功能 C++实现代码 实现功能 boost:验证BOOST_CONCEPT_ASSERT捕获了错误 C++实现代码 #ifdef ...
- boost:验证Boost概念检查的class_requires 库应该在预期的时间内捕获错误
boost:验证Boost概念检查的class_requires 库应该在预期的时间内捕获错误 实现功能 C++实现代码 实现功能 boost:验证Boost概念检查的class_requires 库 ...
- boost:验证Boost的BOOST_CLASS_REQUIRE宏
boost:验证Boost的BOOST_CLASS_REQUIRE宏 实现功能 C++实现代码 实现功能 boost:验证Boost的BOOST_CLASS_REQUIRE宏 C++实现代码 #inc ...
- Boost:可移植地自定义boost :: hash的测试程序
Boost:可移植地自定义boost :: hash的测试程序 实现功能 C++实现代码 实现功能 Boost的container_hash模块,可移植地自定义boost :: hash的测试程序 C ...
最新文章
- Java-- String源码分析
- SpringMvc项目加载顺序及上下文小结
- PHP中foreach遍历循环的使用(两种用法)
- Spring Boot : ApplicationListner
- logstash grok切分nginx日志
- 通过二进制方式安装innobackupex
- Bailian2966 时区转换【时区计算】
- linux mint php mysql_Installing LAMP (Linux, Apache, MySQL and PHP) On Linux Mint
- 小程序 省市区地址选择器
- linux输出替换字符串,Linux命令之sed-替换字符串
- 万达电商为何刻意回避阿里与马云
- mysql_帮助命令/通配搜索/help help用法(official doc)
- Labview的子VI
- setenv: command not found
- 中国游戏媒体市场动态前景与竞争策略分析报告(2021-2026年)
- 面阵相机靶面详解and镜头选择andFA镜头视野计算
- 搞不定思想和组织转变,何谈企业转型
- C#从入门到精通学习笔记——(2)
- 多语言翻译插件 支持android/IOS
- “铁路之声——自动播音系统”的应用操作演示