Does anyone know why the last line outputs -4.0 instead of 4.0?
extension Double {
var absoluteValue: Double {
if self > 0.0 {
return self
}
else {
return -1 * self
}
}
}
var minusTwo = -2.0
minusTwo.absoluteValue // 2.0
let minusThree = -3.0
minusThree.absoluteValue // 3.0
-4.0.absoluteValue // -4.0
It’s just being parsed as -(4.0.absoluteValue)
. This works:
> (-4.0).absoluteValue
$R2: Double = 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With