I have Double
data type, cause I need result with floating number, but if my result is negative, it broke all my algorithm. Is there maybe a unsigned data type with floating point?
How to change from negative number to zero in Swift [closed]
Asked Answered
You should share some code, perhaps a minimal, complete and verifiable example of the issue. Also, you might want to clarify "broke" (e.g. got a result different than you expected? crashed? etc.; and if it crashed, share the precise error message). –
Faso
Using an unsigned float data type (if it existed) wouldn't help. It would immediately crash when you give it a negative. value. You have a deeper underlying logic issue. –
Tuesday
Use max
to limit numbers from going below zero:
let posOrZero = max(possiblyNegative, 0)
If possiblyNegative
is above zero, it's going to be the result of max
; Otherwise, zero is returned.
You deserve an upvote just for being able to decipher the question –
Serotonin
© 2022 - 2024 — McMap. All rights reserved.