これは、Apple の のシステムpow()
がこの場合に行うことに従う小さなテスト プログラムです。Source/Intel/xmm_power.c
Libm-2026
#include <stdio.h>
int main() {
// basically lines 1130-1157 of xmm_power.c, modified a bit to remove
// irrelevant things
double x = .3;
int i = 3;
//calculate ix = f**i
long double ix = 1.0, lx = (long double) x;
//calculate x**i by doing lots of multiplication
int mask = 1;
//for each of the bits set in i, multiply ix by x**(2**bit_position)
while(i != 0)
{
if( i & mask )
{
ix *= lx;
i -= mask;
}
mask += mask;
lx *= lx; // In double this might overflow spuriously, but not in long double
}
printf("%.40f\n", (double) ix);
}
これは を出力します。これは、Matlab とPython で0.0269999999999999962252417162744677625597
取得した結果と一致します(後者は単にこのコードを呼び出していることがわかっています)。対照的に、私にとっては を取得します。これは、小数点以下の桁数まで出力するように要求した場合と同じものであり、おそらく最も近い double です。.3 ^ 3
.3 ** 3
.3 * .3 * .3
0.0269999999999999996946886682280819513835
0.027
そこでアルゴリズムです。各ステップで設定されている値を正確に追跡することはできますが、それを行うための別のアルゴリズムを考えると、非常にわずかに小さい数値に丸められることはそれほど驚くべきことではありません。