定点类#
- group 定点
类型定义
枚举类型
-
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
- 模板参数:
T – 表示类型
- 返回值:
如果
fixed_point
实现支持该类型,则返回true
-
template<typename Rep, typename T>
inline auto addition_overflow(T lhs, T rhs)# 用于识别加法时整数溢出的函数。
- 模板参数:
Rep – 要检查是否溢出的整数类型
T – lhs 和 rhs 的类型(确保它们是相同类型)
- 参数:
lhs – 加法的左侧操作数
rhs – 加法的右侧操作数
- 返回值:
如果加法导致溢出则为 true,否则为 false
-
template<typename Rep, typename T>
inline auto subtraction_overflow(T lhs, T rhs)# 用于识别减法时整数溢出的函数。
- 模板参数:
Rep – 要检查是否溢出的整数类型
T – lhs 和 rhs 的类型(确保它们是相同类型)
- 参数:
lhs – 减法的左侧操作数
rhs – 减法的右侧操作数
- 返回值:
如果减法导致溢出则为 true,否则为 false
-
template<typename Rep, typename T>
inline auto division_overflow(T lhs, T rhs)# 用于识别除法时整数溢出的函数。
- 模板参数:
Rep – 要检查是否溢出的整数类型
T – lhs 和 rhs 的类型(确保它们是相同类型)
- 参数:
lhs – 除法的左侧操作数
rhs – 除法的右侧操作数
- 返回值:
如果除法导致溢出则为 true,否则为 false
-
template<typename Rep, typename T>
inline auto multiplication_overflow(T lhs, T rhs)# 用于识别乘法时整数溢出的函数。
- 模板参数:
Rep – 要检查是否溢出的整数类型
T – lhs 和 rhs 的类型(确保它们是相同类型)
- 参数:
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
相等,则相加_value
。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后相加_value
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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
,然后计算模。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
- 模板参数:
Rep – 表示类型(
int32_t
或int64_t
)
公共函数
-
inline explicit scaled_integer(Rep v, scale_type s)#
scaled_integer
的构造函数- 参数:
v – 定点数的值
s – 值的比例因子
-
template<typename Rep, Radix Rad>
class fixed_point# - #include <fixed_point.hpp>
表示具有固定精度数值的类型。
目前,仅支持二进制和十进制
fixed_point
数。二元操作只能与其他fixed_point
数进行。- 模板参数:
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)# 将执行移位以适当存储值(来自整型)的构造函数
- 模板参数:
T – 您从中构造的整型
- 参数:
value – 将从中构造的值
scale – 应用于 Rad 以执行移位的指数
-
inline explicit fixed_point(scaled_integer<Rep> s)#
不执行移位(假定值已移位)的构造函数
- 参数:
s – scaled_integer,包含比例因子和已移位的值
-
template<typename T>
inline fixed_point(T const &value)# 构造
fixed_point
数的“无比例因子”构造函数,使用指定值和零比例因子- 模板参数:
T – 正在从中构造的值类型
- 参数:
value – 将从中构造的值
-
inline fixed_point()#
默认构造函数,构造一个值和比例因子均为零的
fixed_point
数。
-
template<typename U>
inline explicit constexpr operator U() const# 用于转换为整型的显式转换运算符。
- 模板参数:
U – 正在显式转换为的整型
- 返回值:
采用十进制(即人类可读格式)表示的
fixed_point
数
-
inline operator scaled_integer<Rep>() const#
将
fixed_point
数转换为scaled_integer
- 返回值:
fixed_point
数的scaled_integer
表示
-
inline rep value() const#
返回
fixed_point
数底层值的方法。- 返回值:
fixed_point
数的底层值
-
inline scale_type scale() const#
返回
fixed_point
数比例因子的方法。- 返回值:
fixed_point
数的比例因子
-
inline explicit constexpr operator bool() const#
转换为
bool
的显式转换运算符- 返回值:
作为布尔值表示的
fixed_point
值(零为false
,非零为true
)
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator+=(fixed_point<Rep1, Rad1> const &rhs)# operator +=
- 模板参数:
Rep1 – 操作数
rhs
的表示类型Rad1 – 操作数
rhs
的基数类型 (基)
- 参数:
rhs – 被加到
this
的数字- 返回值:
和
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator*=(fixed_point<Rep1, Rad1> const &rhs)# operator *=
- 模板参数:
Rep1 – 操作数
rhs
的表示类型Rad1 – 操作数
rhs
的基数类型 (基)
- 参数:
rhs – 被乘到
this
的数字- 返回值:
积
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator-=(fixed_point<Rep1, Rad1> const &rhs)# operator -=
- 模板参数:
Rep1 – 操作数
rhs
的表示类型Rad1 – 操作数
rhs
的基数类型 (基)
- 参数:
rhs – 从
this
减去的数字- 返回值:
差
-
template<typename Rep1, Radix Rad1>
inline fixed_point<Rep1, Rad1> &operator/=(fixed_point<Rep1, Rad1> const &rhs)# operator /=
- 模板参数:
Rep1 – 操作数
rhs
的表示类型Rad1 – 操作数
rhs
的基数类型 (基)
- 参数:
rhs – 被
this
除的数字- 返回值:
商
-
inline fixed_point<Rep, Rad> &operator++()#
operator ++ (前置递增)
- 返回值:
递增后的结果
-
inline fixed_point<Rep, Rad> rescaled(scale_type scale) const#
创建具有新
scale
的fixed_point
数字的方法返回的
fixed_point
数字将与this
具有相同的值、底层表示和基数,唯一改变的是尺度。- 参数:
scale – 返回的
fixed_point
数字的scale
- 返回值:
具有新
scale
的fixed_point
数字
-
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)# operator + (用于相加两个
fixed_point
数字)如果
_scale
相等,则相加_value
。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后相加_value
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后相减_value
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
相等,则直接计算模。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后计算模。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
结果
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
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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)# operator != (用于比较两个
fixed_point
数字是否不等)如果
_scale
相等,则比较_value
。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后比较_value
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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)# operator <= (用于比较两个
fixed_point
数字)如果
_scale
相等,则比较_value
。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后比较_value
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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)# operator >= (用于比较两个
fixed_point
数字)如果
_scale
相等,则比较_value
。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后比较_value
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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)# operator < (用于比较两个
fixed_point
数字)如果
_scale
相等,则比较_value
。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后比较_value
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
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)# operator > (用于比较两个
fixed_point
数字)如果
_scale
相等,则比较_value
。如果_scale
不相等,则将具有较大_scale
的数字移位到较小的_scale
,然后比较_value
。- 模板参数:
Rep1 – 操作数
lhs
和rhs
的表示类型Rad1 – 操作数
lhs
和rhs
的基数类型
- 参数:
lhs – 左侧操作数
rhs – 右侧操作数
- 返回值:
如果
lhs
大于rhs
则为 true,否则为 false
-
enum scale_type#