Skip to content

Latest commit

 

History

History
103 lines (75 loc) · 3.67 KB

ieee754-floating-point.md

File metadata and controls

103 lines (75 loc) · 3.67 KB

IEEE-754 浮点数算术规范

数值比较

比较两个浮点数的数值分为 OrderedUnordered 两种方法,二者的主要区别是 NaN 的处理。

PTX ISA 9.3.1.2 Floating Point Comparisons

ordered

The ordered floating-point comparisons are eq, ne, lt, le, gt and ge. If either operand is NaN, the result is False. Below table lists the ordered floating-point comparison operators.

Meaning Floating-Point Operator
a == b && !isNaN(a) && !isNaN(b) eq
a != b && !isNaN(a) && !isNaN(b) ne
a < b && !isNaN(a) && !isNaN(b) lt
a <= b && !isNaN(a) && !isNaN(b) le
a > b && !isNaN(a) && !isNaN(b) gt
a >= b && !isNaN(a) && !isNaN(b) ge

unordered

To aid comparison operations in the presence of NaN values, unordered floating-point comparisons are provided: equ, neu, ltu, leu, gtu, and geu. If both operands are numeric values (not NaN), then the comparison has the same result as its ordered counterpart. If either operand is NaN, then the result of the comparison is True. Below table lists the floating-point comparison operators accepting NaN values.

Meaning Floating-Point Operator
a == b || isNaN(a) || isNaN(b) equ
a != b || isNaN(a) || isNaN(b) neu
a < b || isNaN(a) || isNaN(b) ltu
a <= b || isNaN(a) || isNaN(b) leu
a > b || isNaN(a) || isNaN(b) gtu
a >= b || isNaN(a) || isNaN(b) geu

To test for NaN values, two operators num (numeric) and nan (isNaN) are provided. num returns True if both operands are numeric values (not NaN), and nan returns True if either operand is NaN. Below table lists the floating-point comparison operators testing for NaN values.

Meaning Floating-Point Operator
!isNaN(a) && !isNaN(b) num
isNaN(a) || isNaN(b) nan

min/max

浮点数 min/max 按照 754 spec 分为 maximum/minimummaximumNumber/minimumNumber 两种,二者的主要区别是 NaN 的处理。

According to PTX ISA 9.7.3.11 Floating Point Instructions: min.

maximum/minimum

maximum(a, b) NaN, num → NaN

num, NaNNaN

NaN, NaNNaN

其他(不包含 NaN 时,正常比较大小)

PTX ISA通过 min.NaNmax.NaN 指令 modifier 来达成。

maximumNumber/minimumNumber

maximumNumber(a, b)

NaN, num → num

num, NaN → num

NaN, NaNNaN

其他(不包含 NaN 时,正常比较大小)

PTX ISA 通过 minmax 指令 modifier 来达成。

NaN & Inf

NaN:指数位全是 1,尾数位不全为 0; Inf:指数位全是 1,尾数位都是 0;根据符号位是 0,1 来区分是 Inf 还是 -Inf

  • NaN

    Alt text

  • Inf

    Alt text

  • -Inf

    Alt text

References

  1. float toy