定点类#

group fixed_point_classes

类型定义

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

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

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

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

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

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

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

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

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

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

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

Parameters:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

Returns:

如果 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

Template Parameters:
  • Rep1 – 操作数 lhsrhs 的表示类型

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

Parameters:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

Returns:

如果 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

Template Parameters:
  • Rep1 – 操作数 lhsrhs 的表示类型

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

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

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

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

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

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

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

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

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

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_tint64_t

公共函数

inline explicit scaled_integer(Rep v, scale_type s)#

scaled_integer的构造函数

Parameters:
  • v – 定点数的值

  • s – 值的比例

公共成员

Rep value#

定点数的值。

scale_type scale#

值的规模。

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

一种用于表示具有固定精度的数字的类型。

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

Template Parameters:
  • 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)#

构造函数将执行移位以适当地存储值(从整数类型)

Template Parameters:

T – 你正在从中构造的整数类型

Parameters:
  • value – 将从以下内容构造的值

  • scale – 应用于Rad以执行位移的指数

inline explicit fixed_point(scaled_integer<Rep> s)#

不会执行位移的构造函数(假设值已经位移)

Parameters:

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

用于创建具有新scalefixed_point数字的方法

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

Parameters:

scale – 返回的 fixed_point 数字的 scale

Returns:

fixed_point 数字,带有新的 scale

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

运算符 + (用于添加两个 fixed_point 数字)

如果 _scale 相等,则 _value 相加。如果 _scale 不相等,则将具有较大 _scale 的数字调整为较小的 _scale,然后相加 _value

Template Parameters:
  • Rep1 – 操作数 lhsrhs 的表示类型

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

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

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

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

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

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

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

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

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

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

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

Parameters:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

Returns:

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

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

Parameters:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

Returns:

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

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

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

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

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

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

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

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

Parameters:
  • lhs – 左侧操作数

  • rhs – 右侧操作数

Returns:

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