RobustGeometry.NET icon indicating copy to clipboard operation
RobustGeometry.NET copied to clipboard

advise: add "NoOptimization" flag

Open HenryHo2006 opened this issue 9 years ago • 1 comments

some unit test code like var result = a + b - a, it will be fault in release mode, add [MethodImpl(MethodImplOptions.NoOptimization)] to avoid fault.

HenryHo2006 avatar Mar 08 '16 18:03 HenryHo2006

Are you sure about this? I would expect the compiler to never simplify a+b-a to b, since the semantics would change. Could you help me reproduce the difference?

I could get no difference with and without the NoOptimization using this code (in both cases I get infinity - as I'd expect):

using System;
using System.Runtime.CompilerServices;

namespace TestFloatOptimization
{
    class Program
    {
        static void Main(string[] args)
        {
            double a = double.MaxValue;
            double b = double.MaxValue;
            double withOpt = WithOpt(a, b);
            double noOpt = NoOpt(a, b);

            Console.WriteLine("WithOpt: " + withOpt.ToString("R"));
            Console.WriteLine("NoOpt:   " + noOpt.ToString("R"));
            Console.ReadLine();
        }

        static double WithOpt(double a, double b)
        {
            return a+b-a;
        }

        [MethodImpl(MethodImplOptions.NoOptimization)]
        static double NoOpt(double a, double b)
        {
            return a+b-a;
        }
    }
}

govert avatar Apr 20 '16 10:04 govert