boost之multiprecision
multiprecision
boost中提供的高精度库,支持高精度整型,浮点型等。并且提供统一的接口模板,只需要指定对应的后端类型即可实现对应类型的高精度计算:
boost::multiprecision::number<XX_backend>
cpp_int_backend
提供高精度整型后端类型,需引入头文件#include <boost/multiprecision/cpp_int.hpp>:
template <unsigned MinBits = 0,
unsigned MaxBits = 0,
cpp_integer_type SignType = signed_magnitude,
cpp_int_check_type Checked = unchecked,
class Allocator = std::allocator<limb_type> >
class cpp_int_backend;
- MinBits:底层整型占用的最少位宽
- MaxBits:底层整型占用的最大位宽
- SignType:有符号整型还是无符号整型(任意精度的整型只能为signed!)
- Checked:当发生数值溢出,从字符串转换失败,对负数进行位运算是否抛出异常
- Allocator:内存分配器,当MinBits==MaxBits时,定义为void,表示不需要动态内存分配
cpp_int中无符号型用二进制补码表示,有符号型用原码表示,并且额外用一位来表示符号;不允许任意精度的无符号整型也是因为二进制补码表示需要基于定长的精度。
cpp_int使用示例
有符号数为原码表示,并且额外用一个位表示符号:
int main() {
std::cout << "uint128 max: " << std::numeric_limits<boost::multiprecision::uint128_t>::max() << std::endl;
std::cout << "int128 max: " << std::numeric_limits<boost::multiprecision::int128_t>::max() << std::endl;
std::cout << "int128 min: " << std::numeric_limits<boost::multiprecision::int128_t>::min() << std::endl;
return 0;
}

当有符号数转换为无符号数,先是按位截断,再对无符号数的二进制补码求负数:
int main() {
auto s = std::numeric_limits<boost::multiprecision::int128_t>::min();
auto u = (boost::multiprecision::uint128_t)s;
std::cout << "s: " << s << std::endl;
std::cout << "u: " << u << std::endl;
return 0;
}

boost之multiprecision的更多相关文章
- 浮点运算与boost.multiprecision
在C++中,float占4个字节,double占8个字节,均采用 IEEE 754 浮点标准:内部都是以二进制为基础,表述实数,有些实数可以被精确表述,比如0.2,但有些不行,比如0.3.针对这一点, ...
- Boost 1.61.0 Library Documentation
http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
- C++ Boost库分类总结
c# 程序员写c++,各种不适应.尤其是被内存操作和几十种字符串类型的转换,简直疯了,大小写转换竟然要手动写代码实现. Boost看介绍不错,也不知道能不能跨平台.过几天要上linux写c++, 也不 ...
- 在Visual Sutdio 2017中使用boost库
在Visual Sutdio 2017中使用boost库 转载 https://blog.csdn.net/u011054333/article/details/78648294 对C++有一 ...
- boost强分类器的实现
boost.cpp文件下: bool CvCascadeBoost::train( const CvFeatureEvaluator* _featureEvaluator, int _numSampl ...
- Boost信号/槽signals2
信号槽是Qt框架中一个重要的部分,主要用来解耦一组互相协作的类,使用起来非常方便.项目中有同事引入了第三方的信号槽机制,其实Boost本身就有信号/槽,而且Boost的模块相对来说更稳定. signa ...
- 玩转Windows服务系列——使用Boost.Application快速构建Windows服务
玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...
- boost::function的用法
本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1. 介绍 Boost.Func ...
随机推荐
- SpringMVC-Controller&RestFul
Controller与RestFul 目录 Controller与RestFul 1. Controller 1. 控制器Controller 2. 利用接口定义控制器 1. 实现Controller ...
- Vue 侦听器 watch
1. 侦听器 watch Vue 提供了一种更通用的方式来观察和响应 Vue 实例上的数据变动:侦听属性 当属性发生改变时,自动触发属性对应的侦听器. 当需要在数据变化时执行异步或开销较大的操作时,这 ...
- 一起来读官方文档-----SpringIOC(04)
1.4.2.依赖性和详细配置 如上一节所述,您可以将bean属性和构造函数参数定义为对其他托管bean(协作者)的引用或内联定义的值.Spring的基于XML的配置元数据为此目的在其和元素中支持子元素 ...
- 尤雨溪:TypeScript不会取代JavaScript
来源 |evrone.com译者 | 核子可乐策划 | 蔡芳芳 近日,Evrone 与 Vue.js 的作者尤雨溪进行了一次访谈,了解他对于无后端与全栈方法.以及 Vue.js 适用场景的看法,还有他 ...
- elasticsearch 索引清理脚本及常用命令
elastic索引日志清理不及时,很容易产生磁盘紧张,官网给出curl -k -XDELETE可以清理不需要的索引日志. 清理脚本 #!/bin/bash #Author: 648403020@qq. ...
- Java并发包之Executors
概述 Executor.ExecutorService.ScheduledExecutorService.ThreadFactory.Callable的工厂和工具类. 方法 构造一个固定线程数目的线程 ...
- day55:django:cookie&session
目录 1.Cookie 1.Cookie前戏 2.Cookie的引入 3.django中操作cookie 2.Session 1.cookie的局限性 2.session技术 3.django操作se ...
- 白话ansible-runner--1.环境搭建
最近在Windows10上的项目需要使用到ansible API调用,参考 本末大神 推荐ansible API用官网封装的ansible-runner开发比较友好,ansible-runner是an ...
- solr综合案例
1. 综合案例 1.1. 需求 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字.分类.价格搜索商品信息,也可以根据价格进行排序,并且实现分页功能. 界面如下: 1.2分析 开发人员需要的 ...
- 解决flutter 运行时:Waiting for another flutter command to release the startup lock...
执行 Flutter 包管理相关命令时有可能遇到 Waiting for another flutter command to release the startup lock... 这样的错误,可尝 ...