답변:
bool positive = number > 0;
bool negative = number < 0;
물론 아무도 정답을 얻지 못했습니다.
num != 0 // num is positive *or* negative!
is positive or is negative하지is (positive or negative)
지나침!
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);
}
}
ISignDeterminator사용하여 구현하는 클래스를 인스턴스화해야 합니다 SignDeterminatorFactory.
int?! C #의 어떤 마법의 땅에서 일하고 있습니까?
IsImaginary.
Math.Sign 방법은 이동 할 수있는 한 가지 방법입니다. 음수의 경우 -1, 양수의 경우 1, 0과 같은 값의 경우 0을 반환합니다 (예 : 0에는 부호가 없음). 배정 밀도 및 단 정밀도 변수는 NaN과 동일한 경우 예외 ( ArithmeticException )가 발생합니다.
Math.Sign가능한 반환 값을 명시 적으로 정의하기 때문에 의 반환 값에 대해 동등 비교를 수행해야 합니다.
num < 0 // number is negative
이것은 업계 표준입니다.
int is_negative(float num)
{
char *p = (char*) malloc(20);
sprintf(p, "%f", num);
return p[0] == '-';
}
네이티브 프로그래머 버전. 리틀 엔디안 시스템의 경우 동작이 올바른 것입니다.
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;
}
이것을 사용하지 마십시오.
IsPositiveChecker, IsPositiveCheckerInterface, IsPositiveCheckerFactory,와 IsPositiveCheckerFactoryInterface, 그래도.
result = (Marshal.ReadByte(memory, 3) & 0x80) == 0;대신 사용해야 합니다. 또한 return result;마지막에 어딘가에 있어야 실제로 결과를 반환합니다.