Respostas:
bool positive = number > 0;
bool negative = number < 0;
Claro que ninguém recebeu a resposta correta,
num != 0 // num is positive *or* negative!
is positive or is negativenãois (positive or negative)
EXAGERO!
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?! Em que terra mágica do C # você está trabalhando?
IsImaginary.
O método Math.Sign é um caminho a percorrer. Ele retornará -1 para números negativos, 1 para números positivos e 0 para valores iguais a zero (ou seja, zero não tem sinal). Variáveis de precisão duplas e únicas farão com que uma exceção ( ArithmeticException ) seja lançada se elas forem iguais a NaN.
Math.Sign(uma vez que define explicitamente possíveis valores de retorno.)
num < 0 // number is negative
Este é o padrão da indústria:
int is_negative(float num)
{
char *p = (char*) malloc(20);
sprintf(p, "%f", num);
return p[0] == '-';
}
Vocês jovens e seus extravagantes signos.
Voltar no meu dia, tivemos que usar Math.abs(num) != num //number is negative!
OverflowExceptionse numé MinValuepara qualquer tipo é passado ( Int16, Int32, Int64). Os resultados são ainda piores para valores de ponto flutuante, onde também poderiam estar NaN, desde então NaN != NaN.
public static bool IsPositive<T>(T value)
where T : struct, IComparable<T>
{
return value.CompareTo(default(T)) > 0;
}
Versão do programador nativo. O comportamento é correto para sistemas 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;
}
Nunca use isso.
IsPositiveChecker, IsPositiveCheckerInterface, IsPositiveCheckerFactory, e IsPositiveCheckerFactoryInterface, no entanto.
result = (Marshal.ReadByte(memory, 3) & 0x80) == 0;. Além disso, você deve ter um return result;lugar no final para que ele retorne o resultado.
if (num < 0) {
//negative
}
if (num > 0) {
//positive
}
if (num == 0) {
//neither positive or negative,
}
ou use "else ifs"
Para um número inteiro assinado de 32 bits, como System.Int32também conhecido intem C #:
bool isNegative = (num & (1 << 31)) != 0;
Você apenas precisa comparar se o valor e seu valor absoluto são iguais:
if (value == Math.abs(value))
return "Positif"
else return "Negatif"
O primeiro parâmetro é armazenado no registro EAX e o resultado também.
function IsNegative(ANum: Integer): LongBool; assembler;
asm
and eax, $80000000
end;