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?
Asked
Active
Viewed 4,629 times
4

Rob
- 415,655
- 72
- 787
- 1,044

netatmoBench
- 75
- 1
- 8
-
3You should share some code, perhaps a [minimal, complete and verifiable example](http://stackoverflow.com/help/mcve) 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). – Rob Jul 17 '16 at 18:14
-
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. – Alexander Jul 17 '16 at 18:18
1 Answers
38
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.

Sergey Kalinichenko
- 714,442
- 84
- 1,110
- 1,523
-
4You deserve an upvote just for being able to decipher the question – Matthew Mullin Apr 17 '19 at 11:31