Risposte:
bool positive = number > 0;
bool negative = number < 0;
Ovviamente a nessuno è stata data la risposta corretta,
num != 0 // num is positive *or* negative!
is positive or is negativenonis (positive or negative)
ECCESSIVO!
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);
}
}
ISignDeterminatorusando a SignDeterminatorFactory.
int?! In quale terra magica di C # stai lavorando?
IsImaginary.
Il metodo Math.Sign è un modo per procedere. Restituirà -1 per i numeri negativi, 1 per i numeri positivi e 0 per i valori pari a zero (ovvero zero non ha segno). Le variabili di precisione doppie e singole causeranno un'eccezione ( ArithmeticException ) se uguagliano NaN.
Math.Sign(dal momento che definisce esplicitamente i possibili valori di ritorno.)
num < 0 // number is negative
Questo è lo standard del settore:
int is_negative(float num)
{
char *p = (char*) malloc(20);
sprintf(p, "%f", num);
return p[0] == '-';
}
Voi giovani e la vostra fantasia non è altro che segni.
Ai miei tempi dovevamo usare Math.abs(num) != num //number is negative!
OverflowExceptionse numè MinValueper qualunque tipo è passato in ( Int16, Int32, Int64). I risultati sono anche peggiori per i valori in virgola mobile, dove potrebbero anche essere NaN, da allora NaN != NaN.
public static bool IsPositive<T>(T value)
where T : struct, IComparable<T>
{
return value.CompareTo(default(T)) > 0;
}
Versione del programmatore nativo. Il comportamento è corretto per i sistemi little-endian.
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;
}
Non usarlo mai.
IsPositiveChecker, IsPositiveCheckerInterface, IsPositiveCheckerFactory, e IsPositiveCheckerFactoryInterface, però.
result = (Marshal.ReadByte(memory, 3) & 0x80) == 0;invece averlo . Inoltre, dovresti avere un return result;posto lì alla fine in modo che restituisca effettivamente il risultato.
if (num < 0) {
//negative
}
if (num > 0) {
//positive
}
if (num == 0) {
//neither positive or negative,
}
o usa "else ifs"
Per un numero intero con System.Int32segno inta 32 bit, ad esempio aka in C #:
bool isNegative = (num & (1 << 31)) != 0;
Devi solo confrontare se il valore e il suo valore assoluto sono uguali:
if (value == Math.abs(value))
return "Positif"
else return "Negatif"
Il primo parametro è memorizzato nel registro EAX e anche il risultato.
function IsNegative(ANum: Integer): LongBool; assembler;
asm
and eax, $80000000
end;