473,883 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18246
tarmat wrote in news:jr******** *************** *********@4ax.c om:
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.RE MOVE.co.uk> wrote:
tarmat wrote in news:jr******** *************** *********@4ax.c om:
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.c om:
On 03 Dec 2003 11:56:40 GMT, Rob Williscroft
<rt*@freenet.RE MOVE.co.uk> wrote:
tarmat wrote in news:jr******** *************** *********@4ax.c om:
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.RE MOVE.co.uk> wrote:

tarmat wrote in news:jr******** *************** *********@4ax.c om:

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****@btopenw orld.com> wrote in message
news:jr******** *************** *********@4ax.c om...
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*****@hotmai l.com> wrote in message news:bq******** @dispatch.conce ntric.net...

"tarmat" <ta****@btopenw orld.com> wrote in message
news:jr******** *************** *********@4ax.c om...
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.merke rk(at)dse.nl
Jul 22 '05 #9
tarmat wrote:
On 03 Dec 2003 11:56:40 GMT, Rob Williscroft
<rt*@freenet.RE MOVE.co.uk> wrote:
tarmat wrote in news:jr******** *************** *********@4ax.c om:
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

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

Similar topics

9
2377
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, when the data already contains a " sign problem is like this: access database .... to update on the internet .... a *dataupdate.asp* page ..... On this page, the data gets displayed in a form where i
4
3721
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 functions I'll be using take char* as a parameter. What I am seeing with my program is that when bit 7 is true, my integers are being sign extended. Below is a program that should demonstrate my problem. I am encountering this problem in a...
0
1524
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 class "NewWinHyperLink" which will contain child properties. When I use this custom control in testing web application these child properties don't appear with 'plus sign' in visual studio designer. I want to make my parent property to have plus...
6
52678
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
11663
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 quick regular expression in python to remove the $-sign if it is present in the input.
10
2089
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 running my regexp, the result should be: "$4,300 value / $2,000,001 something else, $3,400, $500"
7
2386
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 the correct value: real_part = ( 3^-4.5 ) * cos( -4.5 * pi ) imag_part = ( 3^-4.5 ) * sin( -4.5 * pi )
11
45277
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
3313
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 preserving rule:- when signed and un signed objects meet in an expression, the result is
0
9948
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9798
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10767
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10864
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9588
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7980
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.