无法通过计算得到0.30000000000000004
Can't get 0.30000000000000004 by calculating
当我在控制台 运行 中 0.1 + 0.2
结果是 0.30000000000000004
。所以我试着自己计算一下。这是我采取的步骤。
1) 将 0.1
表示为 IEEE754 double:
0.1 = 0 01111111011 1001100110011001100110011001100110011001100110011010
2) 将 0.2
表示为 IEEE754 double:
0.2 = 0 01111111100 1001100110011001100110011001100110011001100110011010
这里的计算应该是正确的,因为我已经使用 检查了它们,它显示了 JavaScript 如何存储数字。
3) 将两个数字转换为科学记数法:
0.1 = 1.1001100110011001100110011001100110011001100110011010
x 2
-4
0.2 = 1.1001100110011001100110011001100110011001100110011010
x 2
-3
现在,由于指数不同,让我们将 0.2
调整为 -4
:
0.2 = 0.11001100110011001100110011001100110011001100110011010
x 2
-4
4) 添加他们:
1.1001100110011001100110011001100110011001100110011010
+ 0.1100110011001100110011001100110011001100110011001101
------------------------------------------------------
10.0110011001100110011001100110011001100110011001100111
所以总和是:
10.0110011001100110011001100110011001100110011001100111
x 2
-4
5) 标准化:
1.00110011001100110011001100110011001100110011001100111
x 2
-3
6) 小数点后四舍五入为52位:
1.1100110011001100110011001100110011001100110011010000
x 2
-3
删除指数后,我得到以下结果:
0.001110011001100110011001100110011001100110011001101
当我使用 this calculator 将其转换为十进制时,显示如下:
0.225000000000000088817841970012523233890533447265625
不完全符合预期0.30000000000000004
。
我在这里错过了什么?
尝试 #2:(修正了调整指数时的错误)
3) 将两个数字转换为科学记数法:
0.1 = 1.1001100110011001100110011001100110011001100110011010
x 2
-4
0.2 = 1.1001100110011001100110011001100110011001100110011010
x 2
-3
现在,由于指数不同,让我们将 0.2
调整为 -4
:
0.2 = 11.00110011001100110011001100110011001100110011001101
x 2
-4
4) 添加他们:
1.1001100110011001100110011001100110011001100110011010
+ 11.0011001100110011001100110011001100110011001100110100
------------------------------------------------------
100.110011001100110011001100110011001100110011001100111
所以总和是:
100.110011001100110011001100110011001100110011001100111
x 2
-4
5) 标准化:
1.00110011001100110011001100110011001100110011001100111
x 2
-2
6) 使用round to the nearest
算法将其四舍五入为52位。这里最接近的是 t运行 分类数字(向下舍入):
1.0011001100110011001100110011001100110011001100110011
x 2
-2
删除指数后,我得到以下结果:
0.010011001100110011001100110011001100110011001100110011
当我使用 this calculator 将其转换为十进制时,显示如下:
0.299999999999999988897769753748434595763683319091796875
这次快到了:),但仍然关闭。你们能帮我解决问题吗?
你有:
0.2 = 1.1001100110011001100110011001100110011001100110011010 x 2 ** -3
如果你想得到指数-4,你需要将尾数乘以 2,而不是除以它:
0.2 = 10.1001100110011001100110011001100110011001100110011010 x 2 ** -4
但实际上你想要的可能是 0.1
和 -3
指数:
0.1 = 0.1100110011001100110011001100110011001100110011001101(0) x 2 ** -3
在 "Attempt #2"...
中回答您的具体问题
1.00110011001100110011001100110011001100110011001100111 x 2^-2
是54位,第54位是'1',中间情况。你必须四舍五入才能得到有效数字 "even",所以答案是
1.00110011001100110011001100110011001100110011001101 x 2^-2
这是
0.0100110011001100110011001100110011001100110011001101
这是
0.3000000000000000444089209850062616169452667236328125
当我在控制台 运行 中 0.1 + 0.2
结果是 0.30000000000000004
。所以我试着自己计算一下。这是我采取的步骤。
1) 将 0.1
表示为 IEEE754 double:
0.1 = 0 01111111011 1001100110011001100110011001100110011001100110011010
2) 将 0.2
表示为 IEEE754 double:
0.2 = 0 01111111100 1001100110011001100110011001100110011001100110011010
这里的计算应该是正确的,因为我已经使用
3) 将两个数字转换为科学记数法:
0.1 = 1.1001100110011001100110011001100110011001100110011010
x 2
-4
0.2 = 1.1001100110011001100110011001100110011001100110011010
x 2
-3
现在,由于指数不同,让我们将 0.2
调整为 -4
:
0.2 = 0.11001100110011001100110011001100110011001100110011010
x 2
-4
4) 添加他们:
1.1001100110011001100110011001100110011001100110011010
+ 0.1100110011001100110011001100110011001100110011001101
------------------------------------------------------
10.0110011001100110011001100110011001100110011001100111
所以总和是:
10.0110011001100110011001100110011001100110011001100111
x 2
-4
5) 标准化:
1.00110011001100110011001100110011001100110011001100111
x 2
-3
6) 小数点后四舍五入为52位:
1.1100110011001100110011001100110011001100110011010000
x 2
-3
删除指数后,我得到以下结果:
0.001110011001100110011001100110011001100110011001101
当我使用 this calculator 将其转换为十进制时,显示如下:
0.225000000000000088817841970012523233890533447265625
不完全符合预期0.30000000000000004
。
我在这里错过了什么?
尝试 #2:(修正了调整指数时的错误)
3) 将两个数字转换为科学记数法:
0.1 = 1.1001100110011001100110011001100110011001100110011010
x 2
-4
0.2 = 1.1001100110011001100110011001100110011001100110011010
x 2
-3
现在,由于指数不同,让我们将 0.2
调整为 -4
:
0.2 = 11.00110011001100110011001100110011001100110011001101
x 2
-4
4) 添加他们:
1.1001100110011001100110011001100110011001100110011010
+ 11.0011001100110011001100110011001100110011001100110100
------------------------------------------------------
100.110011001100110011001100110011001100110011001100111
所以总和是:
100.110011001100110011001100110011001100110011001100111
x 2
-4
5) 标准化:
1.00110011001100110011001100110011001100110011001100111
x 2
-2
6) 使用round to the nearest
算法将其四舍五入为52位。这里最接近的是 t运行 分类数字(向下舍入):
1.0011001100110011001100110011001100110011001100110011
x 2
-2
删除指数后,我得到以下结果:
0.010011001100110011001100110011001100110011001100110011
当我使用 this calculator 将其转换为十进制时,显示如下:
0.299999999999999988897769753748434595763683319091796875
这次快到了:),但仍然关闭。你们能帮我解决问题吗?
你有:
0.2 = 1.1001100110011001100110011001100110011001100110011010 x 2 ** -3
如果你想得到指数-4,你需要将尾数乘以 2,而不是除以它:
0.2 = 10.1001100110011001100110011001100110011001100110011010 x 2 ** -4
但实际上你想要的可能是 0.1
和 -3
指数:
0.1 = 0.1100110011001100110011001100110011001100110011001101(0) x 2 ** -3
在 "Attempt #2"...
中回答您的具体问题1.00110011001100110011001100110011001100110011001100111 x 2^-2
是54位,第54位是'1',中间情况。你必须四舍五入才能得到有效数字 "even",所以答案是
1.00110011001100110011001100110011001100110011001101 x 2^-2
这是
0.0100110011001100110011001100110011001100110011001101
这是
0.3000000000000000444089209850062616169452667236328125