Ensure.That
Ensure.That copied to clipboard
Missing validation for double and float against NaN
Please add support for checking if a double or float has or does not have a value of NaN.
checking that a double or float has a value like this Ensure.That(x).IsNot(double.NaN)
does not work. see https://learn.microsoft.com/en-us/dotnet/api/system.double.nan?view=net-8.0
Hey @DanAvni 👋
Thanks for the bug report, that does seem against expectations (see below).
It’s been a while since the last activity on this library - myself, I’ve been over in Go- and Python-land. If you have the bandwidth to prepare a PR, that’d go a long way towards getting the change in. ComparableArg.double.cs is a starting place.
Performance checklist
I don’t have concerns over using a method call here instead of ==, since the result is incorrect today. We do want to avoid boxing/allocations on the happy path to decrease upgrade-cost to consumers (double.Equals
instead of casting to an IComparable as mentioned below should do the job efficiently).
Work around
If that’s not possible at this time, you could use the Extension Method support to locally add…
static double IsNotNaN(this Param<double> src)
Confirmation
- In the BCL == is false but any method-based equality or comparison is True
- If the
double
typehint is lost in EnsureThat, andIComparable<double>
is kept instead, the result istrue
(The IComparable method uses CompareTo, the double override-method uses ==)