Home

Another strange debugger/Math.Sqrt interaction

I'm seeing another strange behavior that seems independent from other stuff I've found today. It seems that the mere presence of a breakpoint on a line can affect the computation.

I'm using Linqpad 6.5.5 x64

.NET Core version (host): 3.0.0
.NET Core version (queries): 3.0.0
Roslyn Version: 3.3.1-beta4-19462-11
FSharp.Compiler.Service version: 32.0.0.0

Here's the reproduction.

  1. Create a C# statements script
Console.WriteLine(Math.Sqrt(1));
Console.WriteLine(Math.Sqrt(1)); // breakpoint
Console.WriteLine(Math.Sqrt(1));
Console.WriteLine(Math.Sqrt(4));
Console.WriteLine(Math.Sqrt(4)); // breakpoint 
Console.WriteLine(Math.Sqrt(4));
Console.WriteLine(Math.Sqrt(9));
Console.WriteLine(Math.Sqrt(9)); // breakpoint
Console.WriteLine(Math.Sqrt(9));
  1. Set breakpoints on the appropriate lines.
  2. Ensure compiler optimizations are turned off.
  3. Execute the script.
  4. For each breakpoint, just continue the script.

The final output (on my machine) is

1
1.0000000000000278
1
2
2.0000000000000555
2
3
3.000000000000074
3

Comments

  • I've delved into this a little more. I don't understand it but I've scoped out the behavior a little more. It seems that in certain cases, a breakpoint causes the first byte of double to get overwritten.

    http://share.linqpad.net/2t5kcw.linq

    So far, I've observed 251, 184, and 185 overwriting the first byte. Curiously it seems that the double value 0 is immune from this behavior, although infinity and NaN are not.

    const double F = 1;
    double f;
    
    f = F;
    Console.WriteLine(BitConverter.GetBytes(f)[0]);
    
    f = F; // breakpoint
    Console.WriteLine(BitConverter.GetBytes(f)[0]);
    
    f = F; // breakpoint
    Console.WriteLine("{0}", BitConverter.GetBytes(f)[0]);
    
    f = F; // breakpoint
    Console.WriteLine("{1} {0}", f, BitConverter.GetBytes(f)[0]);
    

    Output:

    0
    251
    184
    185 1.000000000000041
    
  • I'm seeing the same behavior in Visual Studio. This is very peculiar, and I'd suggest logging a bug report either on the Visual Studio site or here:
    https://github.com/dotnet/coreclr/issues

    If you mention that you're experiencing the same behavior in LINQPad's debugger, it should help narrow it down for them.

Sign In or Register to comment.