定点类#
- group fixed_point_classes
类型定义
枚举
-
enum scale_type#
fixed_point的缩放类型。
值:
-
enum class Radix : int32_t#
在构造
fixed_point时使用的范围枚举器示例:
using decimal32 = fixed_point<int32_t, Radix::BASE_10>; using binary64 = fixed_point<int64_t, Radix::BASE_2>;
值:
-
enumerator BASE_2#
-
enumerator BASE_10#
-
enumerator BASE_2#
函数
-
template<typename T>
inline constexpr auto is_supported_representation_type()# 如果表示类型由
fixed_point支持,则返回true- Template Parameters:
T – 表示类型
- Returns:
true如果类型被fixed_point实现支持
-
template<typename Rep, typename T>
inline auto addition_overflow(T lhs, T rhs)# 用于识别加法时整数溢出的函数。
- Template Parameters:
Rep – 用于检查溢出的整数类型
T – lhs 和 rhs 的类型(确保它们是相同的类型)
- Parameters:
lhs – 加法的左侧
rhs – 加法的右侧
- Returns:
如果加法导致溢出则为true,否则为false
-
template<typename Rep, typename T>
inline auto subtraction_overflow(T lhs, T rhs)# 用于识别减法时整数溢出的函数。
- Template Parameters:
Rep – 用于检查溢出的整数类型
T – lhs 和 rhs 的类型(确保它们是相同的类型)
- Parameters:
lhs – 减法的左侧
rhs – 减法的右侧
- Returns:
如果减法导致溢出则为true,否则为false
-
template<typename Rep, typename T>
inline auto division_overflow(T lhs, T rhs)# 用于识别除法时整数溢出的函数。
- Template Parameters:
Rep – 用于检查溢出的整数类型
T – lhs 和 rhs 的类型(确保它们是相同的类型)
- Parameters:
lhs – 除法的左侧
rhs – 除法的右侧
- Returns:
如果除法导致溢出则为true,否则为false
-
template<typename Rep, typename T>
inline auto multiplication_overflow(T lhs, T rhs)# 用于识别乘法时整数溢出的函数。
- Template Parameters:
Rep – 用于检查溢出的整数类型
T – lhs 和 rhs 的类型(确保它们是相同的类型)
- Parameters:
lhs – 乘法的左侧
rhs – 乘法的右侧
- Returns:
如果乘法导致溢出则为true,否则为false
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator+(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则_value相加。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后相加_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point总和
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator-(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则减去_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后减去_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point差异
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator*(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# _scale被添加,_value被相乘。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point产品
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator/(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# _scale被减去,_value被除以。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point商
-
template<typename Rep1, Radix Rad1>
inline bool operator==(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs和rhs相等则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline bool operator!=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs和rhs不相等则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline bool operator<=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs小于或等于rhs,则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline bool operator>=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs大于或等于rhs,则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline bool operator<(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs小于rhs则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline bool operator>(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs大于rhs则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> operator%(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 如果
_scale相等,则直接计算模数。如果_scale不相等,则将具有较大_scale的数字移位到较小的_scale,然后计算模数。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point数字
-
template<typename Rep, typename cuda::std::enable_if_t<is_supported_representation_type<Rep>()>* = nullptr>
struct scaled_integer# - #include <fixed_point.hpp>
用于在值已经移位时构造
fixed_point的辅助结构。示例:
using decimal32 = fixed_point<int32_t, Radix::BASE_10>; auto n = decimal32{scaled_integer{1001, 3}}; // n = 1.001
- Template Parameters:
Rep – 表示类型(可以是
int32_t或int64_t)
公共函数
-
inline explicit scaled_integer(Rep v, scale_type s)#
scaled_integer的构造函数- Parameters:
v – 定点数的值
s – 值的比例
-
template<typename Rep, Radix Rad>
class fixed_point# - #include <fixed_point.hpp>
一种用于表示具有固定精度的数字的类型。
目前,仅支持二进制和十进制
fixed_point数字。二进制操作只能与其他fixed_point数字一起执行- Template Parameters:
Rep – 表示类型(可以是
int32_t或int64_t)Rad – 基数/进制(可以是
Radix::BASE_2或Radix::BASE_10)
公共函数
-
template<typename T>
inline explicit fixed_point(T const &value, scale_type const &scale)# 构造函数将执行移位以适当地存储值(从整数类型)
- Template Parameters:
T – 你正在从中构造的整数类型
- Parameters:
value – 将从以下内容构造的值
scale – 应用于Rad以执行位移的指数
-
inline explicit fixed_point(scaled_integer<Rep> s)#
不会执行位移的构造函数(假设值已经位移)
- Parameters:
s – scaled_integer 包含比例和已经移位值的
-
template<typename T>
inline fixed_point(T const &value)# “无比例”构造函数,用于构造具有指定值和零比例的
fixed_point数字- Template Parameters:
T – 正在从中构造的值类型
- Parameters:
value – 将从以下内容构造的值
-
inline fixed_point()#
默认构造函数,构造一个值为零且比例为零的
fixed_point数字。
-
template<typename U>
inline explicit constexpr operator U() const# 显式转换运算符用于转换为整数类型。
- Template Parameters:
U – 被显式转换为的整数类型
- Returns:
以10为基数的
fixed_point数字(即可读格式)
-
inline operator scaled_integer<Rep>() const#
将
fixed_point数字转换为scaled_integer- Returns:
scaled_integer表示的fixed_point数字
-
inline rep value() const#
返回
fixed_point数字的基础值的方法。- Returns:
fixed_point数字的底层值
-
inline scale_type scale() const#
返回
fixed_point数字比例的方法。- Returns:
fixed_point数字的规模
-
inline explicit constexpr operator bool() const#
显式转换为
bool的运算符- Returns:
fixed_point值作为布尔值(零为false,非零为true)
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator+=(fixed_point<Rep1, Rad1> const &rhs)# operator +=
- Template Parameters:
Rep1 – 操作数
rhs的表示类型Rad1 – 操作数
rhs的基数(基)类型
- Parameters:
rhs – 被加到
this上的数字- Returns:
总和
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator*=(fixed_point<Rep1, Rad1> const &rhs)# 运算符 *=
- Template Parameters:
Rep1 – 操作数
rhs的表示类型Rad1 – 操作数
rhs的基数(基)类型
- Parameters:
rhs – 被乘以
this的数字- Returns:
产品
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator-=(fixed_point<Rep1, Rad1> const &rhs)# 操作符 -=
- Template Parameters:
Rep1 – 操作数
rhs的表示类型Rad1 – 操作数
rhs的基数(基)类型
- Parameters:
rhs – 从
this中减去的数字- Returns:
差异
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator/=(fixed_point<Rep1, Rad1> const &rhs)# 运算符 /=
- Template Parameters:
Rep1 – 操作数
rhs的表示类型Rad1 – 操作数
rhs的基数(基)类型
- Parameters:
rhs – 被
this除的数- Returns:
商数
-
inline fixed_point<Rep, Rad> &operator++()#
运算符 ++(后增量)
- Returns:
递增的结果
-
inline fixed_point<Rep, Rad> rescaled(scale_type scale) const#
用于创建具有新
scale的fixed_point数字的方法返回的
fixed_point数字将具有与this相同的值、底层表示和基数,唯一改变的是比例。- Parameters:
scale – 返回的
fixed_point数字的scale- Returns:
fixed_point数字,带有新的scale
-
inline explicit operator std::string() const#
返回fixed_point值的字符串表示。
朋友
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator+(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 运算符 + (用于添加两个
fixed_point数字)如果
_scale相等,则_value相加。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后相加_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point总和
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator-(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 操作符 - (用于减去两个
fixed_point数字)如果
_scale相等,则减去_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后减去_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point差异
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator*(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator * (用于乘以两个
fixed_point数字)_scale被添加,_value被相乘。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point产品
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator/(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 运算符 / (用于两个
fixed_point数字的除法)_scale被减去,_value被除以。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point商
-
template<typename Rep1, Radix Rad1>
inline friend fixed_point<Rep1, Rad1> operator%(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 运算符 % (用于计算两个
fixed_point数的模运算)如果
_scale相等,则直接计算模数。如果_scale不相等,则将具有较大_scale的数字移位到较小的_scale,然后计算模数。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
生成的
fixed_point数字
-
template<typename Rep1, Radix Rad1>
inline friend bool operator==(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# operator == (用于比较两个
fixed_point数字)如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs和rhs相等则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline friend bool operator!=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 运算符 != (用于比较两个
fixed_point数字)如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs和rhs不相等则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline friend bool operator<=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 运算符 <= (用于比较两个
fixed_point数字)如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs小于或等于rhs,则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline friend bool operator>=(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 操作符 >= (用于比较两个
fixed_point数字)如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs大于或等于rhs,则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline friend bool operator<(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 运算符 < (用于比较两个
fixed_point数字)如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs小于rhs则为 true,否则为 false
-
template<typename Rep1, Radix Rad1>
inline friend bool operator>(fixed_point<Rep1, Rad1> const &lhs, fixed_point<Rep1, Rad1> const &rhs)# 操作符 > (用于比较两个
fixed_point数字)如果
_scale相等,则比较_value。如果_scale不相等,则将具有较大_scale的数字调整为较小的_scale,然后比较_value。- Template Parameters:
Rep1 – 操作数
lhs和rhs的表示类型Rad1 – 操作数
lhs和rhs的基数(基)类型
- Parameters:
lhs – 左侧操作数
rhs – 右侧操作数
- Returns:
如果
lhs大于rhs则为 true,否则为 false
-
enum scale_type#