[Issue 13489] Boolean semantics of floating point types should use "<> 0"

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Sep 26 07:46:15 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13489

--- Comment #8 from David Eckardt <david.eckardt at sociomantic.com> ---
> 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.

Absolutely. It was just an impressing situation where NaN caught me. I don't
blame D for not catching this particular bug, and I'm aware there are
alternative ways of detecting this bug.

My point is: NaN was intentionally chosen as the initial floating point value
to detect the common bug of forgetting to initialize a variable
(http://dlang.org/faq.html#nan). The standard way to detect bugs is a plain
assert(). So assert() should fail with NaN.
I suggest the boolean behaviour of <>0 because believe it makes more sense in
general, is compatible to !=0 for integers and pointers and would make assert()
fail with NaN.

> Having cast(bool)d yield different results than d!=0 to me is very
> surprising behavior.

I agree it can be considered surprising, and that's an essential point of this
debate. But at least if d is a negative 0, cast(bool)d is false even though the
binary data in d contain a set bit (i.e., if d is a double,
cast(bool)*cast(ulong*)&d is true). This can be considered surprising, too. On
the other hand, by its nature NaN causes many surprises so I believe we can
live with a false boolean result if we have the benefit of catching a well
known bug.

(PS. Amounts like 80,833,756,980 Vietnamese Đồng do happen so limiting them is
far from trivial...)

--


More information about the Digitalmars-d-bugs mailing list