T
tarmat
sorry for this silly little question, but whats the function to grab
the sign of a value?
the sign of a value?
sorry for this silly little question, but whats the function to grab
the sign of a value?
tarmat wrote in
bool is_negative = value < 0;
Hope the answere is "silly" enough .
Rob.
tarmat said:nope that's nowhere near silly enought Rob. It doesn't answer my
question either. I want to know the name of the function that returns
the sign of a value.
nope that's nowhere near silly enought Rob. It doesn't answer my
question either. I want to know the name of the function that returns
the sign of a value.
template said:bool sign_of_value( T const &value )
{
return value < 0;
}
With luck (Ok all warning's on and a decient compiler),
unsigned u = 0;
bool b = sign_of_value( u );
should produce a compile time warning.
tarmat said:sorry for this silly little question, but whats the function to grab
the sign of a value?
C99 has signbit() in math.hHoward said:I don't think there is a standard function in C++ to do that.
Dan said:I think there may be an issue if T is float, double...
I'd almost be inclined to do this one in assembler, just test the darn
bit and be done with it... ( assuming that floats are IEEE, and
assuming 2's complement... ;-)
tarmat said:nope that's nowhere near silly enought Rob. It doesn't answer my
question either. I want to know the name of the function that returns
the sign of a value.
tarmat said:sorry for this silly little question, but whats the function to grab
the sign of a value?
Andrey Tarasevich said:What is "sign of a value"? What do you want this function to return?
Characters '+' or '-'? Boolean value? Integers '-1', '0', '+1'? Clarify
your question. It is too vague the way it is now.
Rob Williscroft said:tarmat wrote in
Ok
template < typename T >
bool sign_of_value( T const &value )
{
return value < 0;
}
With luck (Ok all warning's on and a decient compiler),
unsigned u = 0;
bool b = sign_of_value( u );
should produce a compile time warning.
BTW, why do you think there is a "the" function that returns the sign
of a value, assuming it were called sgn( type ) then calling it would
be sgn(x) (6 chars), compared to (x<0) (5 (or maybe 3) chars). So it
has to have a 1 char name for there to be any benefit in defining such
a thing.
Rob.
You can write such a function yourself if you really think you need one.
Then it can have any name you like.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.