Floating-Point Roundoff Error
Floating-Point Roundoff Error
表示
A rule of thumb is that 32-bit single-precision numbers have about
seven accurate decimal digits, and 64-bit double-precision numbers have
about 16. It is not possible to represent every rational number using 32,
or even 64, bits. As such, many floating-point numbers are rounded to a
nearby representable value. 、
如下列表显示: 32f-bit表示的浮点数,6378137.0f 的下一个数字是: 6378137.5f
For example, 6378137.25f is not a representable value and is rounded down to
6378137.0f; likewise, 6378137.26f is not representable and is rounded up to
6378137.5f.
To make matters worse, gaps between representable floating-point val
ues are dependent on the values themselves: larger values have larger gaps
between them.In fact, if the numbers are large enough, whole numbers
are skipped.
Listing 5.2 shows that the next representable value after 17000000.0f is 17000002.0f. When modeling the world using meters, values in this 2-m gap are rounded up or down. Seventeen million meters may
sound large considering that it is over ten million meters above Earth.
float f = 6378137.0f;// 6378137.0
float f1 = 6378137.1f; // 6378137.0
float f2 = 6378137.2f; // 6378137.0
float f25 = 6378137.25f; // 6378137.0
float f26 = 6378137.26f; // 6378137.5
float f3 = 6378137.3f; // 6378137.5
float f4 = 6378137.4f; // 6378137.5
float f5 = 6378137.5f; // 6378137.
Addition & Subtraction & Accumlate
There are other concerns too; in particular, addition and subtraction of
values of different magnitudes drop least-significant bits, and thus precision,
from the smaller values. The larger the variation between values, the more
precision that is lost. To add icing to the cake, rounding errors accumulate
from every floating-point operation.
posted on 2026-10-16 11:33 Ultraman_X 阅读(4) 评论(0) 收藏 举报
浙公网安备 33010602011771号