Can anyone explain this strange behavior with signed floats in C#?

前端 未结 11 2044
别跟我提以往
别跟我提以往 2020-12-07 07:17

Here is the example with comments:

class Program
{
    // first version of structure
    public struct D1
    {
        public double d;
        public int f         


        
11条回答
  •  甜味超标
    2020-12-07 07:39

    Simpler test case:

    Console.WriteLine("Good: " + new Good().Equals(new Good { d = -.0 }));
    Console.WriteLine("Bad: " + new Bad().Equals(new Bad { d = -.0 }));
    
    public struct Good {
        public double d;
        public int f;
    }
    
    public struct Bad {
        public double d;
    }
    

    EDIT: The bug also happens with floats, but only happens if the fields in the struct add up to a multiple of 8 bytes.

提交回复
热议问题