473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

sign of a value

sorry for this silly little question, but whats the function to grab
the sign of a value?
Jul 22 '05 #1
12 18195
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
On 03 Dec 2003 11:56:40 GMT, Rob Williscroft
<rt*@freenet.REMOVE.co.uk> 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
tarmat wrote in news:r2********************************@4ax.com:
On 03 Dec 2003 11:56:40 GMT, Rob Williscroft
<rt*@freenet.REMOVE.co.uk> 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
tarmat wrote:
On 03 Dec 2003 11:56:40 GMT, Rob Williscroft
<rt*@freenet.REMOVE.co.uk> 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 :).


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

"tarmat" <ta****@btopenworld.com> 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

"Howard" <al*****@hotmail.com> wrote in message news:bq********@dispatch.concentric.net...

"tarmat" <ta****@btopenworld.com> 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
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
tarmat wrote:
On 03 Dec 2003 11:56:40 GMT, Rob Williscroft
<rt*@freenet.REMOVE.co.uk> 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
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

"Andrey Tarasevich" <an**************@hotmail.com> 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

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> 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
<rt*@freenet.REMOVE.co.uk> 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" <ra******@t-online.de>
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
<rt*@freenet.REMOVE.co.uk> 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 <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++
Jul 22 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: cooldv | last post by:
i know how to replace the sign " when SUBMITTING a form in asp by this code: message = Replace(usermessage, "'", "''"). My problem is DISPLAYING data in an asp FORM, from an an access database,...
4
by: Dave | last post by:
I'm working on a program that will be parsing a protocol. My basic storage element type is an array of characters or char*. The reason I am using char* is because many of the socket and stream...
0
by: Emil Georgiev | last post by:
Hell I have a Web Custom Control project in ASP.NET. I'm using a subclassing technique to add functionality in HyperLink web server control. I want to create a property "BrowserWindow" of my...
6
by: Jason Heyes | last post by:
Does a function exist in the standard library to compute the sign of an integer? Example: int sign(int v) { return v > 0 ? 1 : (v < 0 ? -1 : 0); } Thanks.
4
by: Kun | last post by:
i have an html/cgi input that takes in values to a mysql database, however, if i stick in $20 instead of 20, it crashes the program because of the extra $ sign. I was wondering if anyone has a...
10
by: craigslist.jg | last post by:
let's say I have the following text: "4,300 value / 2,000,001 something else, 3,400, 500" I'm trying to come up with a regexp which will insert a $ sign at the begining of every number. After...
7
by: schaefer.mp | last post by:
To compute the absolute value of a negative base raised to a fractional exponent such as: z = (-3)^4.5 you can compute the real and imaginary parts and then convert to the polar form to get...
11
by: =?ISO-8859-1?Q?Konrad_M=FChler?= | last post by:
Hi, a simple question: Which standard function in c++ gives me the sign of a number? Thanks Konrad
3
by: sophia.agnes | last post by:
Dear all, I was going through the book "C a software engineering approach by darnell & Margolis" there was a section named sign preserving vs value preserving it is as follows sign...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.