定点类#

group 定点

类型定义

using decimal32 = fixed_point<int32_t, Radix::BASE_10>#

32 位十进制定点数

using decimal64 = fixed_point<int64_t, Radix::BASE_10>#

64 位十进制定点数

using decimal128 = fixed_point<__int128_t, Radix::BASE_10>#

128 位十进制定点数

枚举类型

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#

函数

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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

返回值:

如果 lhsrhs 相等则为 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

返回值:

如果 lhsrhs 不相等则为 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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_tint64_t

公共函数

inline explicit scaled_integer(Rep v, scale_type s)#

scaled_integer 的构造函数

参数:
  • v – 定点数的值

  • s – 值的比例因子

公共成员

Rep value#

定点数的值。

scale_type scale#

值的比例因子。

template<typename Rep, Radix Rad>
class fixed_point#
#include <fixed_point.hpp>

表示具有固定精度数值的类型。

目前,仅支持二进制和十进制 fixed_point 数。二元操作只能与其他 fixed_point 数进行。

模板参数:
  • Rep – 表示类型(int32_tint64_t

  • Rad – 基数(Radix::BASE_2Radix::BASE_10

公共类型

using rep = Rep#

表示类型。

公共函数

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)#

不执行移位(假定值已移位)的构造函数

参数:

sscaled_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#

创建具有新 scalefixed_point 数字的方法

返回的 fixed_point 数字将与 this 具有相同的值、底层表示和基数,唯一改变的是尺度。

参数:

scale – 返回的 fixed_point 数字的 scale

返回值:

具有新 scalefixed_point 数字

inline explicit operator std::string() const#

返回 fixed_point 值的字符串表示。

公有静态属性

static constexpr auto rad = Rad#

基数。

友元

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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

返回值:

如果 lhsrhs 相等则为 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

返回值:

如果 lhsrhs 不相等则为 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • 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 – 操作数 lhsrhs 的表示类型

  • Rad1 – 操作数 lhsrhs 的基数类型

参数:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

返回值:

如果 lhs 大于 rhs 则为 true,否则为 false