views:

30

answers:

3

I need a function that will return the opposite of a number, but i also need to be able to specify the 'center'.

eg. Center = 0. If the number was 10, it would return -10.

I need this: Center = 100. If the number was 10, it would return 190.

Any idea how it could be done?

Thanks.

+1  A: 

opp = center - (x - center)

Jim Lewis
+1  A: 

Opposite = 2 * Center - X

djacobson
I'm so conflicted as to which to give my vote, Jim clearly wins on time, but can I forgive him for the double negative?
Jean-Bernard Pellerin
The suspense is *figuratively* killing me! :)
djacobson
@Jean: Well, my way is less susceptible to intermediate results overflowing, but I'd be a liar if I claimed to have been thinking about that as I was writing it!
Jim Lewis
+2  A: 
-(int) returnOppositeOf:(int)myNumber aroundCenter:(int)center
{

return ( (center*2) - myNumber);

}
Jesse Naugher
+1 for actually knowing ObjC syntax...
djacobson
i knew it might come in handy SOMEDAY...
Jesse Naugher
And yet, not the accepted answer. Perhaps the OP actually needed this for math class, rather than iPhone dev?
djacobson