Check if Integer is Positive or Negative - Objective C
Asked Answered
H

4

10

How can I tell in objective-c coding if an integer is positive or negative. I'm doing this so that I can write an "if" statement stating that if this integer is positive then do this, and if its negative do this.

Thanks,

Kevin

Herculie answered 17/6, 2010 at 21:17 Comment(1)
Are you forgetting zero, which is neither positive nor negative?Alfonsoalfonzo
M
38
if (x >= 0)
{
    // do positive stuff
}
else
{
    // do negative stuff
}

If you want to treat the x == 0 case separately (since 0 is neither positive nor negative), then you can do it like this:

if (x > 0)
{
    // do positive stuff
}
else if (x == 0)
{
    // do zero stuff
}
else
{
    // do negative stuff
}
Monitor answered 17/6, 2010 at 21:19 Comment(7)
Until they discover functions like log(x).Wilterdink
@BlueRaja - Danny Pflughoeft: well it doesn't have a - in front of if, so that's good enough for me. ;-)Monitor
@Deniz: Mathematicians say 0 is a non-negative value; but it's not positive.Curtis
@BlueRaja - Danny Pflughoeft: if...else statemens evaluate logical expressions as true/false, so for computer "if it is not negative, it is positive".Edmond
@Deniz Acay: Then please do a if(-1) printf("Wow I'm positive"); and fasten your seatbelts.... C takes everything != 0 as true.Wilterdink
@Deniz Acay: I've never heard of mathematicians accepting 0 as positive (and I used to be a math professor). Mathematicians use the word "nonnegative" all the time. They wouldn't need the word if it meant the same thing as "negative".Alfonsoalfonzo
Looks like i understood this "non-negative" word as "positive", so thanks for correction :)Edmond
U
3

Maybe I am missing something and I don't understand the quesiton but isn't this just

if(value >= 0)
{
}
else
{
}
Uird answered 17/6, 2010 at 21:20 Comment(0)
W
3
-(void) tellTheSign:(int)aNumber
{
   printf("The number is zero!\n");
   int test = 1/aNumber;
   printf("No wait... it is positive!\n");
   int test2 = 1/(aNumber - abs(aNumber));
   printf("Sorry again, it is negative!\n");
}

;-)

Seriously though, just use

if (x < 0) {
// ...
} else if (x == 0) {
// ...
} else {
// ...
}

Don't overdo methods ans properties and helper functions for trivial things.

Wilterdink answered 17/6, 2010 at 21:51 Comment(0)
C
2

In Swift

var value = 5
if value.signum() == 1 {
   print("Positive value")
} else if value.signum() == -1 {
   print("Negative value")
} else if value.signum() == 0 {
   print("Zero value")
}
Casement answered 6/6, 2019 at 12:3 Comment(7)
What is signum() here .?Mongo
It is function which returns '-1' if this value is negative and '1' if it's positive otherwise, '0'.Casement
is this native function or you did your self .?Mongo
It's inbuilt functionCasement
can you please in which type this support because I working in objective-c means it Float, Int, Decimal , NSNumber .?Mongo
var direct we can you in swift but objc we need to specify the type first hot you understand that please tell me so I can do this thing Thank You :)Mongo
Find here so help you developer.apple.com/documentation/swift/int64/2886319-signumCasement

© 2022 - 2024 — McMap. All rights reserved.