Minimal/Maximal 整数和双精度
Minimal/Maximal Integer and Double
我尝试了以下代码
#include <iostream>
#include <limits>
using namespace std;
cout << numeric_limits<int>::min() << endl;
cout << numeric_limits<int>::max() << endl;
cout << numeric_limits<double>::min() << endl;
cout << numeric_limits<double>::max() << endl;
输出
-2147483648
2147483647
2.22507e-308
1.79769e+308
为什么 minimal-double 不是 maximal-double 的 (-1) 倍? Afaik 对于整数,第一位用作符号(这可能是最大整数的绝对值低于最小整数的绝对值的原因,不是吗?)。如何描述负双打,因为对我来说,似乎 8 个双字节中的 none 负责符号。到现在为止,我读到浮点数是对称的。我不太明白这一点,因为整数值也应该是对称的?!
谁能为业余爱好者解释一下?
numeric_limits 行为如下:
- 对于数字:最小值和最大值是可能的取值范围
- 对于浮点数:min 是最小的可能值,max 是最大的可能值
参见http://en.cppreference.com/w/cpp/types/numeric_limits/min:
Returns the minimum finite value representable by the numeric type T.
For floating-point types with denormalization, min returns the minimum
positive normalized value. Note that this behavior may be unexpected,
especially when compared to the behavior of min for integral types. To
find the value that has no values less than it, use
numeric_limits::lowest. min is only meaningful for bounded types and
for unbounded unsigned types, that is, types that represent an
infinite set of negative values have no meaningful minimum.
cout << numeric_limits<double>::lowest() << endl;
将显示您所期望的 (1.79769e+308
)
又一个在 std 中混淆命名的例子。
来自 http://en.cppreference.com/w/cpp/types/numeric_limits/min:
For floating-point types with denormalization, min returns the minimum positive normalized value.
对于整数类型,与您的看法相反,没有要求第一位代表符号。在现实世界中,这绝对是不真实的。
至于整数类型的最小值和最大值可能不同的原因(即一个不是另一个的负数)....当前编译器的典型实现选择是用一些特定的整数类型来表示位数。可能的不同值(位值的不同可能组合)的数量 2
增加到位数。所以一个 8
位 signed char
可以表示 256
(2
到 8
)不同的值,一个 16
位 int 可以表示 65536
不同的值,32
位 int 可以表示 14294967296
不同的值。请注意,所有这些值都是偶数。其中一个可表示值将为零,这会留下奇数个其他不同的值。这要么意味着值必须加倍(两个不同的位模式表示相同的值),要么 [实践中通常的实现选择] 最大值和最小值的大小不相等。顺便说一句,这也是 std::numeric_limits::max()
通常为无符号整数类型给出奇数的原因。
对于浮点数,min()
的规格是最小正值(量级最小的正值),max()
是最大的。如果你想要离零最远的负值,使用 (C++11 起)std::numeric_limits<float>::lowest()
.
我尝试了以下代码
#include <iostream>
#include <limits>
using namespace std;
cout << numeric_limits<int>::min() << endl;
cout << numeric_limits<int>::max() << endl;
cout << numeric_limits<double>::min() << endl;
cout << numeric_limits<double>::max() << endl;
输出
-2147483648
2147483647
2.22507e-308
1.79769e+308
为什么 minimal-double 不是 maximal-double 的 (-1) 倍? Afaik 对于整数,第一位用作符号(这可能是最大整数的绝对值低于最小整数的绝对值的原因,不是吗?)。如何描述负双打,因为对我来说,似乎 8 个双字节中的 none 负责符号。到现在为止,我读到浮点数是对称的。我不太明白这一点,因为整数值也应该是对称的?!
谁能为业余爱好者解释一下?
numeric_limits 行为如下:
- 对于数字:最小值和最大值是可能的取值范围
- 对于浮点数:min 是最小的可能值,max 是最大的可能值
参见http://en.cppreference.com/w/cpp/types/numeric_limits/min:
Returns the minimum finite value representable by the numeric type T. For floating-point types with denormalization, min returns the minimum positive normalized value. Note that this behavior may be unexpected, especially when compared to the behavior of min for integral types. To find the value that has no values less than it, use numeric_limits::lowest. min is only meaningful for bounded types and for unbounded unsigned types, that is, types that represent an infinite set of negative values have no meaningful minimum.
cout << numeric_limits<double>::lowest() << endl;
将显示您所期望的 (1.79769e+308
)
又一个在 std 中混淆命名的例子。 来自 http://en.cppreference.com/w/cpp/types/numeric_limits/min:
For floating-point types with denormalization, min returns the minimum positive normalized value.
对于整数类型,与您的看法相反,没有要求第一位代表符号。在现实世界中,这绝对是不真实的。
至于整数类型的最小值和最大值可能不同的原因(即一个不是另一个的负数)....当前编译器的典型实现选择是用一些特定的整数类型来表示位数。可能的不同值(位值的不同可能组合)的数量 2
增加到位数。所以一个 8
位 signed char
可以表示 256
(2
到 8
)不同的值,一个 16
位 int 可以表示 65536
不同的值,32
位 int 可以表示 14294967296
不同的值。请注意,所有这些值都是偶数。其中一个可表示值将为零,这会留下奇数个其他不同的值。这要么意味着值必须加倍(两个不同的位模式表示相同的值),要么 [实践中通常的实现选择] 最大值和最小值的大小不相等。顺便说一句,这也是 std::numeric_limits::max()
通常为无符号整数类型给出奇数的原因。
对于浮点数,min()
的规格是最小正值(量级最小的正值),max()
是最大的。如果你想要离零最远的负值,使用 (C++11 起)std::numeric_limits<float>::lowest()
.