「.NETのfloat.Epsilon定数は計算機イプシロンではない」の版間の差分

imported>Administrator
Administrator (トーク) による版 8909 を取り消し
imported>Administrator
Administrator (トーク) による版 8908 を取り消し
12行目: 12行目:


== 解決 ==
== 解決 ==
「奥村晴彦『C言語による最新アルゴリズム事典』技術評論社,1991年,ISBN4-87408-414-1,2400円」という書籍に[[計算機イプシロン]]を動的に求める[[アルゴリズム]]が詳細に解説されており、また[[ソースコード]]は公式サイトにおいても配布されている。この本は非常に面白いので迷わず買え。個人的にはこの手の技術書を買う際に目次と技術評論社という名前だけで信頼して買うようになった一冊である。
https://oku.edu.mie-u.ac.jp/~okumura/algo/
速攻でC#に[[移植]]した(ほぼコピペしただけ)。
<source lang="csharp">
<source lang="csharp">
     using System;
     using System;


         static double MachineEpsilon()
    class MainClass
    {
         static float Foo(float x) { return x; }
 
        static float MachineEpsilon()
         {
         {
             double eps = 1.0;
             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;


            do
            {
                eps /= 2.0;
            }
            while ((double)(1.0 + eps) != 1.0);
             return eps;
             return eps;
         }
         }
        public static void Main(string[] args)
        {
            Console.WriteLine("epsilon = {0}", MachineEpsilon());
        }
    }
</source>
</source>