sign of a value

T

tarmat

sorry for this silly little question, but whats the function to grab
the sign of a value?
 
R

Rob Williscroft

tarmat wrote in
sorry for this silly little question, but whats the function to grab
the sign of a value?

bool is_negative = value < 0;

Hope the answere is "silly" enough :).

Rob.
 
T

tarmat

tarmat wrote in

bool is_negative = value < 0;

Hope the answere is "silly" enough :).

Rob.


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.
 
G

Grumble

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.

That would be '<' which you can read as "is smaller than".

Note: it is an operator, not a function.

Perhaps what you want is:

(value < 0) ? -1 : 1;
 
R

Rob Williscroft

tarmat wrote in
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.

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.
 
D

Dan W.

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.

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... ;-)
 
H

Howard

tarmat said:
sorry for this silly little question, but whats the function to grab
the sign of a value?

I don't think there is a standard function in C++ to do that. You can
always write one. It's pretty simple, but depends upon what you want it to
return...? If I recall my BASIC programming, it returned -1 for negative
numbers, 0 for 0, and 1 for positive numbers. (correct???) Is that what
you want? One way to get the "sign" this way is this: sign = (value ==
0 )? 0 : value/abs(value); Or if you want zero to return 1 as if it were
positive, then: sign = (value < 0) ? -1 : 1;

-Howard
 
P

Peter van Merkerk

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... ;-)

Chances are that your compiler already generates optimal code for this
test.
 
R

Rolf Magnus

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.

You can write such a function yourself if you really think you need one.
Then it can have any name you like.
 
A

Andrey Tarasevich

tarmat said:
sorry for this silly little question, but whats the function to grab
the sign of a value?

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.
 
J

jeffc

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.

Are you implying there are multiple functions he can call that will return
those various types? I'm sure if a sign function were available, any of
those would be acceptable to him.
 
L

Ludoviko++

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.

----- Original Message -----
From: "Rolf Magnus" <[email protected]>
Newsgroups: comp.lang.c++
Sent: Wednesday, December 03, 2003 5:27 PM
Subject: Re: sign of a value

You can write such a function yourself if you really think you need one.
Then it can have any name you like.

I think a better mathemathical-sense, "classic" definition for the "sign
function" should be:

template <class T>
int sign(T value)
{
if (value == 0)
return 0;
else if (value > 0)
return 1;
else
return -1;
}

IThe previous solution is fine excepto for the definition of "sign number",
which is the number divided by its positive, equivalent, value (which for
reals would be abs() ). But a function of this kind is not very reliable
because what is the sign of a complex? or the sign of a date, for example?

I use the standard definition
sgn(x) = x / abs(x)
, except for x == 0, only for plain old data types. Any other thing should
overload its own "sign" function if there is a meaning to it (I can't think
on sign of a person...).

Hope it helps.


L++
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top