440,929 Members | 1,245 Online
Need help? Post your question and get tips & solutions from a community of 440,929 IT Pros & Developers. It's quick & easy.

# sign of a value

 P: n/a sorry for this silly little question, but whats the function to grab the sign of a value? Jul 22 '05 #1
12 Replies

 P: n/a tarmat wrote in news:jr********************************@4ax.com: 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. -- http://www.victim-prime.dsl.pipex.com/ Jul 22 '05 #2

 P: n/a On 03 Dec 2003 11:56:40 GMT, Rob Williscroft wrote: tarmat wrote in news:jr********************************@4ax.com: 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. 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. Jul 22 '05 #3

 P: n/a tarmat wrote in news:r2********************************@4ax.com: On 03 Dec 2003 11:56:40 GMT, Rob Williscroft wrote:tarmat wrote in news:jr********************************@4ax.com: 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. 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. -- http://www.victim-prime.dsl.pipex.com/ Jul 22 '05 #4

 P: n/a tarmat wrote: On 03 Dec 2003 11:56:40 GMT, Rob Williscroft wrote:tarmat wrote in news:jr********************************@4ax.com:sorry for this silly little question, but whats the function to grabthe sign of a value?bool is_negative = value < 0;Hope the answere is "silly" enough :). 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; Jul 22 '05 #5

 P: n/a >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. 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... ;-) Jul 22 '05 #6

 P: n/a "tarmat" wrote in message news:jr********************************@4ax.com... 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 Jul 22 '05 #7

 P: n/a "Howard" wrote in message news:bq********@dispatch.concentric.net... "tarmat" wrote in message news:jr********************************@4ax.com... 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. C99 has signbit() in math.h Jul 22 '05 #8

 P: n/a Dan W. wrote: 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. 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. -- Peter van Merkerk peter.van.merkerk(at)dse.nl Jul 22 '05 #9

 P: n/a tarmat wrote: On 03 Dec 2003 11:56:40 GMT, Rob Williscroft wrote:tarmat wrote in news:jr********************************@4ax.com: 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. 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. Jul 22 '05 #10

 P: n/a tarmat wrote: 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. -- Best regards, Andrey Tarasevich Jul 22 '05 #11

 P: n/a "Andrey Tarasevich" wrote in message news:vs************@news.supernews.com... tarmat wrote: 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. 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. Jul 22 '05 #12

 P: n/a "Rob Williscroft" escribió en el mensaje news:Xn**********************************@195.129. 110.204... tarmat wrote in news:r2********************************@4ax.com: On 03 Dec 2003 11:56:40 GMT, Rob Williscroft wrote:tarmat wrote in news:jr********************************@4ax.com: 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. 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. -- http://www.victim-prime.dsl.pipex.com/ ----- Original Message ----- From: "Rolf Magnus" Newsgroups: comp.lang.c++ Sent: Wednesday, December 03, 2003 5:27 PM Subject: Re: sign of a value tarmat wrote: On 03 Dec 2003 11:56:40 GMT, Rob Williscroft wrote:tarmat wrote in news:jr********************************@4ax.com: 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. 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. I think a better mathemathical-sense, "classic" definition for the "sign function" should be: template 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++ Jul 22 '05 #13

### This discussion thread is closed

Replies have been disabled for this discussion.