开发者

How do I check if a number is positive or negative in C#?

开发者 https://www.devze.com 2023-01-23 07:33 出处:网络
How do I check if a number is positiv开发者_StackOverflowe or negative in C#?bool positive = number > 0;

How do I check if a number is positiv开发者_StackOverflowe or negative in C#?


bool positive = number > 0;
bool negative = number < 0;


Of course no-one's actually given the correct answer,

num != 0   // num is positive *or* negative!


OVERKILL!

public static class AwesomeExtensions
{
    public static bool IsPositive(this int number)
    {
        return number > 0;
    }

    public static bool IsNegative(this int number)
    {
        return number < 0;
    }

    public static bool IsZero(this int number)
    {
        return number == 0;
    }

    public static bool IsAwesome(this int number)
    {
        return IsNegative(number) && IsPositive(number) && IsZero(number);
    }
}


The Math.Sign method is one way to go. It will return -1 for negative numbers, 1 for positive numbers, and 0 for values equal to zero (i.e. zero has no sign). Double and single precision variables will cause an exception (ArithmeticException) to be thrown if they equal NaN.


num < 0 // number is negative


This is the industry standard:

int is_negative(float num)
{
  char *p = (char*) malloc(20);
  sprintf(p, "%f", num);
  return p[0] == '-';
}


You youngins and your fancy less-than signs.

Back in my day we had to use Math.abs(num) != num //number is negative !


    public static bool IsPositive<T>(T value)
        where T : struct, IComparable<T>
    {
        return value.CompareTo(default(T)) > 0;
    }


Native programmer's version. Behaviour is correct for little-endian systems.

bool IsPositive(int number)
{
   bool result = false;
   IntPtr memory = IntPtr.Zero;
   try
   {
       memory = Marshal.AllocHGlobal(4);
       if (memory == IntPtr.Zero)
           throw new OutOfMemoryException();

       Marshal.WriteInt32(memory, number);

       result = (Marshal.ReadByte(memory, 3) & 0x80) == 0;
   }
   finally
   {
       if (memory != IntPtr.Zero)
           Marshal.FreeHGlobal(memory);
   }
   return result;
}

Do not ever use this.


if (num < 0) {
  //negative
}
if (num > 0) {
  //positive
}
if (num == 0) {
  //neither positive or negative,
}

or use "else ifs"


You just have to compare if the value & its absolute value are equal:

if (value == Math.abs(value))
    return "Positif"
else return "Negatif"


For a 32-bit signed integer, such as System.Int32, aka int in C#:

bool isNegative = (num & (1 << 31)) != 0;


public static bool IsNegative<T>(T value)
   where T : struct, IComparable<T>
{
    return value.CompareTo(default(T)) < 0;
}


bool isNegative(int n) {
  int i;
  for (i = 0; i <= Int32.MaxValue; i++) {
    if (n == i) 
      return false;
  }
  return true;
}


int j = num * -1;

if (j - num  == j)
{
     // Num is equal to zero
}
else if (j < i)
{
      // Num is positive
}
else if (j > i) 
{
     // Num is negative
}


This code takes advantage of SIMD instructions to improve performance.

public static bool IsPositive(int n)
{
  var v = new Vector<int>(n);
  var result = Vector.GreaterThanAll(v, Vector<int>.Zero);
  return result;
}


First parameter is stored in EAX register and result also.

function IsNegative(ANum: Integer): LongBool; assembler;
asm
   and eax, $80000000
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消