I want to be able to test if zero is positive or negative in swift.
let neg: CGFloat = -0
let pos: CGFloat = +0
if pos == neg {
// this gets executed, but I don't want this
}
The code above does not work like I need it to. Can someone help me?
There are “negative zero” and “positive zero” floating point numbers. You can disambiguate them by checking the
.signproperty, but they (intentionally) compare as equal:Note that you have to use floating point literals ("-0.0" or "+0.0") in the initialization. The integer literals ("+0", "-0") are equal and converted to positive zero.