差分

ナビゲーションに移動 検索に移動

.NETのfloat.Epsilon定数は計算機イプシロンではない

1,976 バイト追加, 2018年2月15日 (木) 03:48
ページの作成:「C#においてfloat型(Single型)の絶対値をfloat.Epsilon定数と比較している箇所の挙動がどうもおかしい。 ググってMSDNを見る...」
C#においてfloat型(Single型)の[[絶対値]]をfloat.Epsilon定数と比較している箇所の挙動がどうもおかしい。

[[ググって]][[MSDN]]を見るとサラッと絶望的なひとことメモが。
The value of the F:System.Single.Epsilon property is not equivalent to machine epsilon,
which represents the upper bound of the relative error due to rounding in floating-point arithmetic.
https://msdn.microsoft.com/ja-jp/library/system.single.epsilon(v=vs.110).aspx

要約すると「浮動小数点演算の丸の相対誤差の上限(丸め誤差発生時にズレるであろう最大値)」であり「[[計算機イプシロン]]」ではない。
名前紛らわしすぎだろ。

「奥村晴彦『C言語による最新アルゴリズム事典』技術評論社,1991年,ISBN4-87408-414-1,2400円」という書籍に[[計算機イプシロン]]を動的に求める[[アルゴリズム]]が詳細に解説されており、また[[ソースコード]]は公式サイトにおいても配布されている。この本は非常に面白いので迷わず買え。
https://oku.edu.mie-u.ac.jp/~okumura/algo/

速攻でC#に移植した。
<source lang="csharp">
using System;

class MainClass
{
static float Foo(float x) { return x; }

static float Epsilon()
{
int b, p;
float x, y, eps;

x = y = 2;
while (Foo(x + 1) - x == 1) x *= 2;
while (Foo(x + y) == x) y *= 2;
b = (int)(Foo(x + y) - x);
p = 1; x = b;
while (Foo(x + 1) - x == 1) { p++; x *= b; }
eps = 1;
while (Foo(1 + eps / 2) > 1) eps /= 2;
eps = Foo(1 + eps) - 1;

return eps;
}

public static void Main(string[] args)
{
Console.WriteLine("epsilon = {0}", Epsilon());
}
}
</source>

[[category: .NET]]
[[category: Mono]]
[[category: Xamarin]]
匿名利用者

案内メニュー