Yanıtlar:
bool positive = number > 0;
bool negative = number < 0;
Tabii ki hiç kimseye aslında doğru cevap verilmedi,
num != 0 // num is positive *or* negative!
is positive or is negativedeğilis (positive or negative)
AŞIRI YÜKLEME!
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);
}
}
ISignDeterminatorkullanarak uygulayan bir sınıfı başlatmalıdır SignDeterminatorFactory.
intmi ?! Hangi büyülü C # ülkesinde çalışıyorsunuz?
IsImaginary.
Math.Sign yöntemi gitmek için bir yoldur. Negatif sayılar için -1, pozitif sayılar için 1 ve sıfıra eşit değerler için 0 döndürür (yani sıfırın işareti yoktur). Çift ve tek duyarlıklı değişkenler, NaN'ye eşitlerse bir istisnanın ( ArithmeticException ) atılmasına neden olur.
Math.Sign(dönüş olası değerlerini açıkça tanımladığı için) dönüş değerine eşitlik karşılaştırması yapıyor olmalı .
num < 0 // number is negative
Bu endüstri standardıdır:
int is_negative(float num)
{
char *p = (char*) malloc(20);
sprintf(p, "%f", num);
return p[0] == '-';
}
Sen gençlerin ve süslülerin daha az.
Geri dönünce kullanmak zorunda kaldık Math.abs(num) != num //number is negative!
OverflowExceptioneğer numolduğunu MinValuegeçirilen ne olursa olsun tipi için ( Int16, Int32, Int64). O NaNzamandan beri olabileceği yerde kayan nokta değerleri için sonuçlar daha da kötüdür NaN != NaN.
public static bool IsPositive<T>(T value)
where T : struct, IComparable<T>
{
return value.CompareTo(default(T)) > 0;
}
Yerel programcının sürümü. Küçük endian sistemler için davranış doğrudur.
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;
}
Bunu asla kullanmayın.
IsPositiveChecker, IsPositiveCheckerInterface, IsPositiveCheckerFactory, ve IsPositiveCheckerFactoryInterface, gerçi.
result = (Marshal.ReadByte(memory, 3) & 0x80) == 0;. Ayrıca, return result;sonunda bir yere sahip olmalısınız, böylece sonuç gerçekten geri dönecektir.
if (num < 0) {
//negative
}
if (num > 0) {
//positive
}
if (num == 0) {
//neither positive or negative,
}
veya "else ifs" kullanın
C # olarak System.Int32da bilinen 32 bit işaretli tam sayı için int:
bool isNegative = (num & (1 << 31)) != 0;
Değerin ve mutlak değerinin eşit olup olmadığını karşılaştırmanız yeterlidir:
if (value == Math.abs(value))
return "Positif"
else return "Negatif"
İlk parametre EAX kaydında saklanır ve sonuç da elde edilir.
function IsNegative(ANum: Integer): LongBool; assembler;
asm
and eax, $80000000
end;