简体   繁体   English

IEEE 754浮点数,最大数<1?

[英]IEEE 754 floating point, what is the largest number < 1?

When using IEEE 754 floating point representation ( double type in c++), numbers that are very close to (representable) integers are rounded to their closest integer and represented exactly. 当使用IEEE 754浮点表示(c ++中的double类型)时,非常接近(可表示)整数的数字将四舍五入到它们最接近的整数并精确表示。 Is that true? 真的吗?
Exactly how close does a number have to be to the nearest representable integer before it is rounded? 在舍入之前,数字究竟与最接近的可表示整数有多接近?
Is this distance constant? 这个距离是否恒定?
For example, given that 1 can be represented exactly, what is the largest double less than 1? 例如,假设1可以精确表示,那么最大的double小于1是多少?

When using IEEE 754 floating point representation ( double type in c++), numbers that are very close to (representable) integers are rounded to their closest integer and represented exactly. 当使用IEEE 754浮点表示(c ++中的double类型)时,非常接近(可表示)整数的数字将四舍五入到它们最接近的整数并精确表示。

This depends upon whether the number is closer to the integer than to other values representable. 这取决于数字是否更接近整数而不是其他可表示的值。 0.99999999999999994 is not equal to 1 , but 0.99999999999999995 is. 0.99999999999999994不等于1 ,但是0.99999999999999995是。

Is this distance constant? 这个距离是否恒定?

No, it becomes less with larger magnitudes - in particular with larger exponents in the representation. 不,它在更大的范围内变得更小 - 特别是在表示中具有更大的指数。 Larger exponents imply larger intervals to be covered by the mantissa, which in turn implies less precision overall. 较大的指数意味着尾数覆盖较大的间隔,这反过来意味着整体精度较低。

For example, what is the largest double less than 1? 例如,最小的双倍小于1是多少?

std::nexttoward(1.0, 0.0) . std::nexttoward(1.0, 0.0) Eg 0.999999999999999889 on Coliru . 例如, 在Coliru的0.999999999999999889

You will find much more definitive statements regarding the opposite direction from 1.0 The difference between 1.0 and the next larger number is documented here: 你会发现更多关于1.0的相反方向的明确陈述1.0和下一个更大的数字之间的差异记录在这里:

std::numeric_limits<double>::epsilon()

The way floating point works, the next smaller number should be exactly half as far away from 1.0 as the next larger number. 浮点的工作方式,下一个较小的数字应该与下一个较大的数字正好相差一半。

The first IEEE double below 1 can be written unambiguously as 0.99999999999999989, but is exactly 0.99999999999999988897769753748434595763683319091796875. 低于1的第一个IEEE双精度数可以明确地写为0.99999999999999989,但正好是0.99999999999999988897769753748434595763683319091796875。

The distance is not constant, it depends on the exponent (and thus the magnitude) of the number. 距离不是恒定的,它取决于数字的指数(以及数量)。 Eventually the gap becomes larger than 1, meaning even (not as opposed to odd - odd integers are the first to get rounded) integers will get rounded somewhat (or, eventually, a lot). 最终差距变得大于1,意味着偶数(不是奇数 - 奇数整数是第一个得到舍入的)整数将有点圆(或者,最终,很多)。

The binary representation of increasing IEEE floating point numbers can be seen as a increasing integer representation: 增加IEEE浮点数的二进制表示可以看作是递增的整数表示:

Sample Hack (Intel): 示例Hack(英特尔):

#include <cstdint>
#include <iostream>
#include <limits>

int main() {
    double one = 1;
    std::uint64_t one_representation = *reinterpret_cast<std::uint64_t*>(&one);
    std::uint64_t lesser_representation = one_representation - 1;
    std::cout.precision(std::numeric_limits<double>::digits10 + 1);
    std::cout << std::hex;
    std::cout << *reinterpret_cast<double*>(&lesser_representation)
              << " [" << lesser_representation
              << "] < " << *reinterpret_cast<double*>(&one_representation)
              << " [" << one_representation
              << "]\n";
}

Output: 输出:

0.9999999999999999 [3fefffffffffffff] < 1 [3ff0000000000000]

When advancing the integer representation to its limits, the difference of consecutive floating point numbers is increasing, if exponent bits change. 当将整数表示推进到其极限时,如果指数位改变,则连续浮点数的差异正在增加。

See also: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ 另见: http//randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

When using IEEE 754 floating point representation (double type in c++), numbers that are very close to exact integers are rounded to the closest integer and represented exactly. 当使用IEEE 754浮点表示(c ++中的双精度类型)时,非常接近精确整数的数字将四舍五入为最接近的整数并精确表示。 Is that true? 真的吗?

This is false. 这是错误的。

Exactly how close does a number have to be to the nearest int before it is rounded? 在舍入之前,数字到底是多么接近最接近的int?

When you do a binary to string conversion the floating point number gets rounded to the current precision (for printf family of functions the default precision is 6) using the current rounding mode. 当您执行二进制到字符串转换时,使用当前舍入模式将浮点数舍入为当前精度(对于printf系列函数,默认精度为6)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM