Reputation: 13983
I have a "creative" algorithm I'm working on, and there is a case where I need to return negative Nan
.
extension Decimal {
func placement(
between gains: Decimal,
and losses: Decimal
) -> Decimal {
if gains == losses {
return self > gains ? (1 / 0) : (-1 / 0)
}
return (self - losses) / (gains - losses)
}
}
Unfortunately (-1 / 0)
produces Nan instead of -Nan
.
I've accidentally created -Nan
previously, unfortunately, I don't remember how it happened.
Upvotes: 0
Views: 289
Reputation: 13983
print(-Double.nan) // produces -nan
print(-Decimal.nan) // produces Nan
I think Decimal might not track signed nan. It doesn't distinguish between positive and negative nan but if it is negative nan, isNan
will produce true.
Upvotes: 0
Reputation: 17844
When something is literally "not a number", how can it have a meaningful sign?
And, from a practical perspective, how do you expect to distinguish "negative NaN" from "NaN"? Decimal.nan == -Decimal.nan
is true, as well as Decimal.nan < 0
and -Decimal.nan < 0
let n = Decimal.nan
let nn = -Decimal.nan
print (n < 0) // true
print (nn < 0) // true
print (nn < n) // false
print (n < nn) // false
print (n == nn) // true
Upvotes: 1