tags:

views:

257

answers:

8

I know this will really turn out to be simple, but my brain is just not working. I need a function in C# that will return -1 if the integer passed to the function has a negative sign, return 1 if the integer has a positive sign and return 0 if the number passed is 0. So for example:

int Sign=SignFunction(-82); // Should return -1
int Sign2=SignFunction(197); // Should return 1
int Sign3=SignFunction(0);   // Should return 0
+3  A: 
public int SignFunction(int number) 
{
    return number.CompareTo(0);
}
Anna Lear
See my comment to Anthony Pegram's answer. It's an implementation detail that Int32's CompareTo function returns -1, 1 or 0 - not a specified behaviour. It could change between frameworks. Use Math.Sign.
James Hart
+2  A: 
public int SignFunction( int input )
{
    if( input < 0 ) return -1;
    if( input > 0 ) return 1;
    return 0;
}
BioBuckyBall
+1 for not using the API
Falmarri
@Falmarri LOL I guess one could argue this is portable to another implementation of C# that might not include the .NET Framework.
AaronLS
Indeed this function is a standard FPU operation, so I don't think someone could be made an implementation without it ;-) This also implies that this implementation is way slower than API.
mbq
A: 
public int Sign(int number)
{
    if(number==0)
       return 0;
    return number/Math.Abs(number);
}
Gregoire
oops accidentally edited your solution!
Vivin Paliath
it is ok I had done a big mistake ;)
Gregoire
Same problem as Josh above - int.MinValue fails.
James Hart
+1  A: 
public int SignFunction(int number) {
  return (number > 0) ? 
             1 : (number < 0) ? 
                    -1 : number;
}
Vivin Paliath
Why the downvote, I wonder.
Vivin Paliath
@Vivin: I didn't downvote you, but I think this is a perfect example of doing something the "clever" way with no benefit. That is: compare this to the answer provided by [Lucas Heneks](http://stackoverflow.com/questions/3259509/a-function-to-return-a-sign/3259528#3259528). Not a programmer in the world wouldn't understand his version. Yours might cause some confusion (it shouldn't, but it might). They perform the same. See the argument?
Dan Tao
@Dan I agree with you *in principle* about doing things too cleverly. But I beg to differ about ternary expressions. I don't think they are too hard to understand, IMHO :). But yes, I can see how this is not as easy to understand as a straightforward solution.
Vivin Paliath
+3  A: 
return input.CompareTo(0);
Anthony Pegram
+1 since CompareTo is more or less implemented the same way as Math.Sign. Math.Sign has hardcoded the zero ;)
Mikael Svenson
@Mikael: CompareTo suggests a very different usage, though. Math.Sign is much better at telling the programmers following you about your original intent.
Reed Copsey
Strictly, IComparable.CompareTo only requires that the value it returns be negative, zero or positive to indicate the result of the comparison - it doesn't mandate that the result be -1, 0 or 1. Int16's implementation of CompareTo for shorts returns (int) x - (int) y for x.CompareTo(y).In fact, to handle the result of CompareTo, you might want to pass it to Math.Sign to get a constrained value.
James Hart
+23  A: 

This is already in the framework. Just use Math.Sign...

int sign = Math.Sign(-82); // Should return -1
int sign2 = Math.Sign(197); // Should return 1
int sign3 = Math.Sign(0);   // Should return 0

In addition, it will work with:

int sign4 = Math.Sign(-5.2); // Double value, returns -1
int sign5 = Math.Sign(0.0m); // Decimal, returns 0
// ....
Reed Copsey
+1 DRY/DRTW....
Vivin Paliath
a perfectly good existing wheel
seanb
haha one of those functions I never needed to use, so never knew it existed. Thanks!
icemanind
Just out of curiosity, what would Math.Sign(0.01m) return?
icemanind
@icemandind: 1 - Any positive number (no matter how small), with decimal, returns +1. See: http://msdn.microsoft.com/en-us/library/ak21zcty.aspx
Reed Copsey
+6  A: 
int sign = Math.Sign(number);

It already exists.

Tesserex
A: 

If Math.Sign did not exist, I would do this:

return x == 0 ? 0 : x / Math.Abs(x);
Josh
I suspect that Sign(int.MinValue) should return -1, rather than throwing an OverflowException, though. so maybe you shouldn't do that.(to be clear: Math.Abs(int.MinValue) would be -int.MinValue, which is int.MaxValue+1... oops)
James Hart
Kind of inefficient and hard to read...
AtliB
Thanks James, I didn't know that was how Abs is implemented.AtliB what are you smoking?
Josh