How do I check if a number is positive or negative in C#?
First parameter is stored in EAX register and result also.
function IsNegative(ANum: Integer): LongBool; assembler;
asm
and eax, $80000000
end;
You just have to compare if the value & its absolute value are equal:
if (value == Math.abs(value))
return "Positif"
else return "Negatif"
bool positive = number > 0;
bool negative = number < 0;
This is the industry standard:
int is_negative(float num)
{
char *p = (char*) malloc(20);
sprintf(p, "%f", num);
return p[0] == '-';
}
Of course no-one's actually given the correct answer,
num != 0 // num is positive *or* negative!
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.