7.4.11 丸めモード

現在の丸めモードが何か調べるには getround, 丸めモードを設定するには setround() を用いる。
今の丸めモードは?
>> getround
ans =
     0
0 は最近偶数への丸め (rounding to the nearest even), $ -1$ は切り捨て ($ -\infty$ 方向への丸目, rounding downwards), $ 1$ は切り上げ ($ +\infty$ 方向への丸目, round upwards),

丸めモードの実験コード
format long
rndold = getround;
setround(-1);
x = 0;
for i = 1:100000, x = x+0.1; end
x

setround(0);
x = 0;
for i = 1:100000, x = x+0.1; end
x

setround(1);
x = 0;
for i = 1:100000, x = x+0.1; end
x

setround(rndold);
0$ 0.1$$ 10$ 万回足すと、丸め誤差がなければ1万になるはず。
実行結果 (downwards, nearest, upwards の順)
x =
     9.999999999947978e+03
x =
     1.000000000001885e+04
x =
     1.000000000003054e+04

桂田 祐史
2020-09-03