With DMD v2.072.2 on Windows 7 this code produces an assertion error: unittest{ enum Enum: double{A = 0.1} bool test(in Enum a){return a is Enum.A;} assert(test(Enum.A)); } When `A = 1` or other values that can be represented more exactly as a floating point, the code does not produce an error. Given that this code does not produce such an error, I'm especially inclined to think this is not intended behavior: unittest{ enum Enum: double{A = 0.1} assert(Enum.A is Enum.A); }
This issue seems not to occur when using `enum Enum: real` instead of double. From IRC: <skl> hmm it does a byte comparison but they differ, one is 00D0CC... the other is CDCCCCCC... <adam_d_ruppe> i am guessing that the enum treats it more like a literal than the variable does so it gets imprecise passed to functions <skl> it stores the literial enum value as a real 80-bit floating point number <skl> but it looks like it passes a double into the function
This seems to work without the parameter attr 'in'. test(Enum a)
compiler explorer shows clearly that for (in Enum) a real (80 bits) comparison occurs while for (Enum) a more correct (and faster) 64 bits. https://godbolt.org/z/W5PYMn
0.1 cannot be represented exactly with floating point types. In particular, taking an 80 bit value and comparing it with a rounded 64 bit value will not come out as identical. Rounding and inexact floating point operations are just something we have to deal with.