https://issues.dlang.org/show_bug.cgi?id=13489
Walter Bright <[email protected]> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |[email protected] --- Comment #2 from Walter Bright <[email protected]> --- Consider this C++ code: bool foo(long double a) { return a; } DMC++ generates: fld tbyte ptr 4[ESP] fldz fucompp ST(1),ST fstsw AX sahf jne L13 jp L13 xor EAX,EAX jmp short L18 L13: mov EAX,1 L18: ret and g++ generates: fld tbyte ptr 8[RSP] mov EAX,1 fldz fucomip fstp ST setp DL cmovz EAX,DL ret In other words, both of them treat NaN as "TRUE". I fear that if we deviate from this behavior, we'll get subtly broken code that is written by former C++ programmers or that is transliterated from C++. Having cast(bool)d yield different results than d!=0 to me is very surprising behavior. Having cast(bool)d rewritten to be d<>0 is also problematic as Don Clugston is a vocal advocate of having the <> operator removed from D. Issuing a warning or error for if(d) can be done, but then the user simply rewrites it as if(d!=0) and I'm not sure if anything was accomplished. So I'm not sure what the right answer would be. I do strongly suggest that calculations that return money values have sanity checks in them for reasonable dollar values; not just 0, Infinity or NaN checks. An amount of 9 trillion dollars shouldn't have gotten far. Such checks can find a great many more mistakes than NaNs would. Another possibility is to not use lround(), i.e. make your own lround that asserts that its argument is not NaN. Note that lround() in D just defers to the C one, which is underspecified as to what happens with NaN or Infinity arguments. I would not rely on C's lround() for financial calculations without first checking its argument. --
