否定浮点数总是安全的吗
Is it always safe to negate a floating point number
考虑:
double f = foo();
double g = -f;
其中 foo()
可以 return 分配给 f
的任何内容。
double g = -f;
在 C 和 C++ 中安全吗?对于 IEEE 754 类型,它显然是,但 C 和 C++ 不限制浮点实现(不像 Java)。
如果它是安全的,那么 -g
是否总是将 true
与 foo()
进行比较?
(以上对2的补码中的int
不成立)
浮动类型由 C 标准(至少草案 N1570)的 §5.2.4.2.2 定义:
The characteristics of floating types are defined in terms of a model that describes a representation of floating-point numbers and values that provide information about an implementation’s floating-point arithmetic.21) The following parameters are used to define the model for each floating-point type:
s
sign (±1)
b
base or radix of exponent representation (an integer > 1)
e
exponent (an integer between a minimum emin and a maximum emax)
p
precision (the number of base-b digits in the significand)
fk
nonnegative integers less than b (the significand digits)
A floating-point number (x) is defined by the following model:
所以是的,该表达式是 有点 安全,除非 foo
返回了 "other kinds of floating point numbers" 标准未规定:
In addition to normalized floating-point numbers ( f1 > 0 if x ≠ 0), floating types may be able to contain other kinds of floating-point numbers, such as subnormal floating-point numbers (x ≠ 0, e = emin, f1 = 0) and unnormalized floating-point numbers (x ≠ 0, e > emin, f1 = 0), and values that are not floating-point numbers, such as infinities and NaNs.
和:
An implementation may give zero and values that are not floating-point numbers (such as infinities and NaNs) a sign or may leave them unsigned.
对此可能还有其他注意事项,但标准对这些类型的特征进行了相当多的详细说明。您可以在 this publicly available draft.
中阅读有关它们的所有信息
考虑:
double f = foo();
double g = -f;
其中 foo()
可以 return 分配给 f
的任何内容。
double g = -f;
在 C 和 C++ 中安全吗?对于 IEEE 754 类型,它显然是,但 C 和 C++ 不限制浮点实现(不像 Java)。
如果它是安全的,那么 -g
是否总是将 true
与 foo()
进行比较?
(以上对2的补码中的int
不成立)
浮动类型由 C 标准(至少草案 N1570)的 §5.2.4.2.2 定义:
The characteristics of floating types are defined in terms of a model that describes a representation of floating-point numbers and values that provide information about an implementation’s floating-point arithmetic.21) The following parameters are used to define the model for each floating-point type:
s
sign (±1)b
base or radix of exponent representation (an integer > 1)e
exponent (an integer between a minimum emin and a maximum emax)p
precision (the number of base-b digits in the significand)fk
nonnegative integers less than b (the significand digits)A floating-point number (x) is defined by the following model:
所以是的,该表达式是 有点 安全,除非 foo
返回了 "other kinds of floating point numbers" 标准未规定:
In addition to normalized floating-point numbers ( f1 > 0 if x ≠ 0), floating types may be able to contain other kinds of floating-point numbers, such as subnormal floating-point numbers (x ≠ 0, e = emin, f1 = 0) and unnormalized floating-point numbers (x ≠ 0, e > emin, f1 = 0), and values that are not floating-point numbers, such as infinities and NaNs.
和:
An implementation may give zero and values that are not floating-point numbers (such as infinities and NaNs) a sign or may leave them unsigned.
对此可能还有其他注意事项,但标准对这些类型的特征进行了相当多的详细说明。您可以在 this publicly available draft.
中阅读有关它们的所有信息