12

Constant folding issue with floats in mcs

 3 years ago
source link: https://github.com/mono/mono/issues/7522
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Steps to Reproduce

  1. Compile test case with mcs and run
  2. Notice the floating point constants emitted by the compiler
using System;

public class Foo
{
	public static void Main()
	{
		Console.WriteLine((1.7f - 0.2f) % 1.5f);
		Console.WriteLine((1.5f) % 1.5f);
	}
}

Current Behavior

4.470348E-08
0

IL from mcs compiled exe

    .method public static hidebysig 
           default void Main ()  cil managed 
    {
        // Method begins at RVA 0x2058
        .entrypoint
        // Code size 21 (0x15)
        .maxstack 8
        IL_0000:  ldc.r4 4.4703483581542969e-08
        IL_0005:  call void class [mscorlib]System.Console::WriteLine(float32)
        IL_000a:  ldc.r4 0.
        IL_000f:  call void class [mscorlib]System.Console::WriteLine(float32)
        IL_0014:  ret 
    } // end of method Foo::Main

Expected Behavior

0
0

IL from mcs compiled exe

    .method public static hidebysig 
           default void Main ()  cil managed 
    {
        // Method begins at RVA 0x2050
        .entrypoint
        // Code size 24 (0x18)
        .maxstack 8
        IL_0000:  nop 
        IL_0001:  ldc.r4 0.
        IL_0006:  call void class [mscorlib]System.Console::WriteLine(float32)
        IL_000b:  nop 
        IL_000c:  ldc.r4 0.
        IL_0011:  call void class [mscorlib]System.Console::WriteLine(float32)
        IL_0016:  nop 
        IL_0017:  ret 
    } // end of method Foo::Main

On which platforms did you notice this

[x ] macOS
[ ] Linux
[ ] Windows

Version Used:

mcs --version
Mono C# compiler version 5.4.1.0

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK