New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant folding issue with floats in mcs #7522

Open
joncham opened this Issue Mar 9, 2018 · 3 comments

Comments

Projects
None yet
4 participants
@joncham
Contributor

joncham commented Mar 9, 2018

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
@migueldeicaza

This comment has been minimized.

Member

migueldeicaza commented Mar 9, 2018

This is caused by Mono using higher precision for math by default, it uses doubles to process floats, and can be solved by calling MCS like this:

MONO_ENV_OPTIONS=-O=float32 mcs foo.exe

Perhaps a good reason to finally switch the default in Mono to 32 bit float processing.

@luhenry

This comment has been minimized.

Member

luhenry commented Mar 9, 2018

See #6985 for float32 as default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment