Jak sprawdzić, czy liczba jest dodatnia czy ujemna w C #?
Jak sprawdzić, czy liczba jest dodatnia czy ujemna w C #?
Odpowiedzi:
bool positive = number > 0;
bool negative = number < 0;
Oczywiście nikt nie udzielił poprawnej odpowiedzi,
num != 0 // num is positive *or* negative!
is positive or is negativenieis (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);
}
}
ISignDeterminatorza pomocą SignDeterminatorFactory.
int?! W jakiej magicznej krainie C # pracujesz?
IsImaginary.
Metoda Math.Sign to jedna droga. Zwróci -1 dla liczb ujemnych, 1 dla liczb dodatnich i 0 dla wartości równych zero (tzn. Zero nie ma znaku). Zmienne o podwójnej i pojedynczej precyzji spowodują zgłoszenie wyjątku (wyjątek Arithmetic ), jeśli są równe NaN.
Math.Sign(ponieważ wyraźnie określa możliwe wartości zwrotu.)
num < 0 // number is negative
To jest standard branżowy:
int is_negative(float num)
{
char *p = (char*) malloc(20);
sprintf(p, "%f", num);
return p[0] == '-';
}
Wy, młodzi, i wasze fantazyjne mniej niż znaki.
W moich czasach musieliśmy używać Math.abs(num) != num //number is negative!
OverflowExceptionjeśli numjest MinValuedla każdego typu jest przekazywana ( Int16, Int32, Int64). Wyniki są jeszcze gorsze dla zmiennoprzecinkowych, gdzie mogą być one również NaN, ponieważ NaN != NaN.
public static bool IsPositive<T>(T value)
where T : struct, IComparable<T>
{
return value.CompareTo(default(T)) > 0;
}
Wersja natywnego programisty. Zachowanie jest poprawne dla systemów 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;
}
Nigdy tego nie używaj.
IsPositiveChecker, IsPositiveCheckerInterface, IsPositiveCheckerFactory, i IsPositiveCheckerFactoryInterface, choć.
result = (Marshal.ReadByte(memory, 3) & 0x80) == 0;. Ponadto powinieneś mieć return result;gdzieś tam na końcu, aby rzeczywiście zwrócił wynik.
if (num < 0) {
//negative
}
if (num > 0) {
//positive
}
if (num == 0) {
//neither positive or negative,
}
lub użyj „else ifs”
Dla 32-bitowej liczby całkowitej ze znakiem, takiej jak System.Int32aka intw języku C #:
bool isNegative = (num & (1 << 31)) != 0;
Musisz tylko porównać, czy wartość i jej wartość bezwzględna są równe:
if (value == Math.abs(value))
return "Positif"
else return "Negatif"
Pierwszy parametr jest przechowywany w rejestrze EAX i wynik również.
function IsNegative(ANum: Integer): LongBool; assembler;
asm
and eax, $80000000
end;