3

I'm coding a simple calculator just to get started with iPhone dev. The thing is I have a (-) button thats supposed to negate whatever is put to the screen already by doing a simple *-1. It works fine except when the previous input is 0.

Scenario: With empty screen or 0, I tap (-) to negate. Then when I tap for example 9 I want it to read (or rather be) -9. Not 9 as it is now. FYI I'm atm working with primitive ints everywhere but I'm prepared to abort that.

Questions: How to represent negative 0? Is there a data type I can use that supports it? NSInteger?

Or is the only way to do some hacky workaround with state boolean and stringWithFormat etc?

Appreciate the help. /O

Oskar Lund
  • 339
  • 2
  • 11
  • 1
    It sounds to me like your question is really something along the lines of "how should I design my program to input negative numbers?", rather than "how can I represent negative 0?". Neither the Unix xcalc nor the Windows Calculator program accept negative input the way you describe -- they interpret a "-" in that context as an infix subtraction operator, and the following digits as the second operand. Usually the key that flips the sign is labeled "+/-" rather than "-", and has no effect if "0" is currently displayed. – Jim Lewis Nov 16 '10 at 23:02
  • Perhaps you're right but I seem to have gotten answers along the lines I was hoping for anyway. Good point about the label though, I will change that. Funny thing is that the iPhone calculator works the way I'm striving for, but the Calculator on the Mac does not. It works the same as my "unfinished" one. :) – Oskar Lund Nov 16 '10 at 23:15

2 Answers2

1

float and double support -0 (which is different than 0 if memory serves). See here for more information, though I'm not convinced using -0 in your application will be particularly helpful for users. Do you have a compelling use case that requires it?

fbrereto
  • 35,429
  • 19
  • 126
  • 178
  • The use case would be my scenario that I described. If it's compelling I don't really know ;) It's just functionality found in any calculator, the native iPhone one for example. I'm just mimicking. – Oskar Lund Nov 16 '10 at 22:55
  • Just thought of another use case: Say I wanna calculate 15 minus -5. The natural would be to tap 15, then the plus button, then the negate button followed by 5. This doesn't work now. I have to negate after the 5. – Oskar Lund Nov 16 '10 at 23:07
-4

There's no such thing as negative zero.

For a binary integer, setting the sign bit to 1 and all other bits to zero, you get the smallest negative value for that integer size. (Assuming signed numbers.)

ThatBlairGuy
  • 2,426
  • 1
  • 19
  • 33
  • Negative zero is actually used in mathematical analysis, especially in limit calculations. I don't know how useful it is in a simple calculator, however. – Jeff B Nov 16 '10 at 22:51
  • 2
    For twos-complement signed integers, there is no negative zero. But for other formats like IEEE floating point or using a sign bit and a magnitude, there is in fact a way to represent -0. – Alex Martini Nov 16 '10 at 22:55
  • Yeah, I found a Wikipedia entry mentioning that http://en.wikipedia.org/wiki/Signed_zero, but I stand by my answer for int. Likewise, the float and double implementations I've worked with use a signed zero as a flag for Not a Number. – ThatBlairGuy Nov 16 '10 at 22:55
  • @Alex Martini - I guess it really comes down to which IEEE spec the iPhone follows. – ThatBlairGuy Nov 16 '10 at 22:57