読者です 読者をやめる 読者になる 読者になる

検証用テストコード (C#)

using System;
using System.Diagnostics;
class FPStrictTest
{
    static void Main(string[] args)
    {
        Test1();
        Test2();
    }

    unsafe static void Test1()
    {
        long l1 = 0x7fe0000000000000;
        double d1 = *(double*)(&l1);
        double d2 = *(double*)(&l1);

        d1 += d2;
        d1 -= d2;

        Trace.Assert( double.IsPositiveInfinity( d1 ), "strictfp semantics violation");
    }

    unsafe static void Test2()
    {
        long l1 = 0x0008008000000000;
        long l2 = 0x3ff0000000000001;
        long l3 = 0x000fffffffffffff;
        long l4 = 0x3fefffffffffffff;

        double d1 = *(double*)(&l1);
        double d2 = *(double*)(&l2);
        double d3 = *(double*)(&l3);
        double d4 = *(double*)(&l4);

        double ret1 = d1 * d2;
        double ret2 = d3 / d4;
        long dr1 = *(long*)(&ret1);
        long dr2 = *(long*)(&ret2);

        Trace.Assert((dr1 & 0x1) != 1, "strictfp semantics violation");
        Trace.Assert((dr2 & 0x1) != 1, "strictfp semantics violation");
    }
}