This might be a very dumb question, but if I have a CGFloat with value of 44.0, how do I make it to -44.0? I tried doing -myFloat and 0 - myFloat but it gives me 0. Why is this? Also I don't want a multiply by -1 answer
这可能是一个很愚蠢的问题,但是如果我有一个值为44。0的CGFloat,我如何使它变成-44。0呢?我尝试了-myFloat和0 -myFloat,但它给了我0。这是为什么呢?我也不想让a乘以-1
3 个解决方案
#1
4
If myFloat is equal to 44.0, then the expression -myFloat will be evaluated as -44.0. You've got a bug somewhere else in your code. Can you post the expression where it "gives you 0"?
如果myFloat等于44.0,那么表达式-myFloat将被计算为-44.0。你的代码中有一个错误。你能把“给你0”的表达式贴出来吗?
Using a clumsy workaround will just make your code harder to read and even worse to debug in the long run. Let's sort out the actual problem. :)
使用笨拙的变通方法只会使您的代码难于阅读,从长远来看甚至更难以调试。让我们解决实际的问题。:)
#2
3
Its very simple dude. Just multiply it with -1.
它非常简单的家伙。把它和-1相乘。
i.e. 44.0 * -1 = -44.0
Update:
更新:
Solution 2 as mentioned by Inafziger
Inafziger提到的解决方案2
yourNo = 0 - yourNo
#3
2
Okay, so you can't multiply by -1, how about:
所以不能乘以-1,那么
myFloat = 0 - myFloat;
or
或
myFloat = myFloat - 2 * myFloat;
#1
4
If myFloat is equal to 44.0, then the expression -myFloat will be evaluated as -44.0. You've got a bug somewhere else in your code. Can you post the expression where it "gives you 0"?
如果myFloat等于44.0,那么表达式-myFloat将被计算为-44.0。你的代码中有一个错误。你能把“给你0”的表达式贴出来吗?
Using a clumsy workaround will just make your code harder to read and even worse to debug in the long run. Let's sort out the actual problem. :)
使用笨拙的变通方法只会使您的代码难于阅读,从长远来看甚至更难以调试。让我们解决实际的问题。:)
#2
3
Its very simple dude. Just multiply it with -1.
它非常简单的家伙。把它和-1相乘。
i.e. 44.0 * -1 = -44.0
Update:
更新:
Solution 2 as mentioned by Inafziger
Inafziger提到的解决方案2
yourNo = 0 - yourNo
#3
2
Okay, so you can't multiply by -1, how about:
所以不能乘以-1,那么
myFloat = 0 - myFloat;
or
或
myFloat = myFloat - 2 * myFloat;