Invert a positive CGFloat to a negative value

This can be a very silly question, but if I have a CGFloat value of 44.0, how can I make it to -44.0? I've tried doing -myFloat and 0 for myFloat, but that gives me 0. Why is this? Also I don't want to multiply by -1 answer

+3


source to share


3 answers


If myFloat is 44.0, then the -myFloat expression evaluates to -44.0. You have a bug somewhere else in your code. Can you post an expression where it "gives you 0"?



Using a clumsy solution will make your code easier to read and worse, debug it in the long run. Let's sort out the real problem. :)

+4


source


Its a very basic dude. Just multiply it by -1.

i.e. 44.0 * -1 = -44.0

      

Update:



Solution 2 as mentioned by Inafziger

yourNo = 0 - yourNo

      

+3


source


Ok, so you can't multiply by -1, how about:

myFloat = 0 - myFloat;

      

or

myFloat = myFloat - 2 * myFloat;

      

+2


source







All Articles