473,804 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reliably determine sign value of double in C90/C89

Hello.

I want to be able to portably determine whether or not a given double
value is negative.

Under C99, I do:

if (signbit(d)) negative = 1;

Or, if the implementation doesn't provide signbit(), I do:

union real {
unsigned long long n;
double d;
};

if ((real.n >63) & 1) negative = 1;

However, what if I am to support an implementation that only provides
C89 (no long long, no signbit())?

Anybody got a reliable, portable method of handling this?

thanks,
mc

Apr 27 '07 #1
27 6080
ar**********@go oglemail.com wrote, On 27/04/07 21:48:
Hello.

I want to be able to portably determine whether or not a given double
value is negative.

Under C99, I do:

if (signbit(d)) negative = 1;
<snip>
However, what if I am to support an implementation that only provides
C89 (no long long, no signbit())?

Anybody got a reliable, portable method of handling this?
What is wrong with
if (d < 0)
?

It won't detect negative 0, but is that important to you?
--
Flash Gordon
Apr 27 '07 #2
On Apr 27, 9:53 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
>
Anybody got a reliable, portable method of handling this?

What is wrong with
if (d < 0)
?

It won't detect negative 0, but is that important to you?
Yes, unfortunately it is.

mc

Apr 27 '07 #3
In article <11************ **********@b40g 2000prd.googleg roups.com>,
<ar**********@g ooglemail.comwr ote:
>Hello.

I want to be able to portably determine whether or not a given double
value is negative.
[...]
>Anybody got a reliable, portable method of handling this?
Why isn't "double_val <0", possibly combined with tests for magic values
like NaN that will or won't pass this test when they shoudn't or should,
reliable or portable enough?

(I am not a numerical analyst; there may be a valid reason why not.
But if you're asking C programmers who aren't numerical analysts how to
do this, you should be able to tell us why the obvious solution doesn't
meet your standards.)
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca

Surprise your compiler. Write better code than it asks you to.
--Keith Thompson in comp.lang.c
Apr 27 '07 #4
<ar**********@g ooglemail.comha scritto nel messaggio
news:11******** **************@ b40g2000prd.goo glegroups.com.. .
Hello.

I want to be able to portably determine whether or not a given double
value is negative.

Under C99, I do:

if (signbit(d)) negative = 1;

Or, if the implementation doesn't provide signbit(), I do:

union real {
unsigned long long n;
double d;
};

if ((real.n >63) & 1) negative = 1;

However, what if I am to support an implementation that only provides
C89 (no long long, no signbit())?

Anybody got a reliable, portable method of handling this?
if (d < 0) negative = 1;
Apr 27 '07 #5

<ar**********@g ooglemail.comha scritto nel messaggio
news:11******** **************@ s33g2000prh.goo glegroups.com.. .
On Apr 27, 9:53 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
>>
Anybody got a reliable, portable method of handling this?

What is wrong with
if (d < 0)
?

It won't detect negative 0, but is that important to you?

Yes, unfortunately it is.
Then use if (d != fabs(d))
Apr 27 '07 #6
On Apr 27, 10:18 pm, "Army1987" <please....@for .itwrote:
>
Then use if (d != fabs(d))
Ah! Yes, that's the one. Very smart.

thanks,
MC

Apr 27 '07 #7
Army1987 wrote On 04/27/07 17:18,:
<ar**********@g ooglemail.comha scritto nel messaggio
news:11******** **************@ s33g2000prh.goo glegroups.com.. .
>>On Apr 27, 9:53 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
>>>>Anybody got a reliable, portable method of handling this?

What is wrong with
if (d < 0)
?

It won't detect negative 0, but is that important to you?

Yes, unfortunately it is.


Then use if (d != fabs(d))
Won't work; -0 is equal to +0, and there are problems
with NaNs and infinities, too, if they exist. That's
probably why signbit() exists, and I think it's the best
solution to artifact's problem.

Suggestion: Write the code to use signbit(), and use
the system-provided signbit() if one exists. For platforms
where it doesn't, provide your own platform-specific code
to implement it. 100% portability can't be guaranteed; in
particular, the kludge with a union will not always work.
(The sign bit may not be in the position where you expect
it, `long' and `double' may not be the same size, ...)

--
Er*********@sun .com
Apr 27 '07 #8
On Apr 27, 10:01 pm, dj3va...@caffei ne.csclub.uwate rloo.ca (Dave
Vandervies) wrote:
>
Anybody got a reliable, portable method of handling this?

Why isn't "double_val <0", possibly combined with tests for magic values
like NaN that will or won't pass this test when they shoudn't or should,
reliable or portable enough?

(I am not a numerical analyst; there may be a valid reason why not.
But if you're asking C programmers who aren't numerical analysts how to
do this, you should be able to tell us why the obvious solution doesn't
meet your standards.)
The simple answer is that I need to be able to detect negative zero
(after
doing all appropriate checks for NaN, inf, -inf etc.

MC

Apr 27 '07 #9
ar**********@go oglemail.com wrote:
Hello.

I want to be able to portably determine whether or not a given double
value is negative.
inline int negativep(doubl e x) { return x < 0; }
Under C99, I do:

if (signbit(d)) negative = 1;
see above.
>
Or, if the implementation doesn't provide signbit(), I do:

union real {
unsigned long long n;
double d;
};

if ((real.n >63) & 1) negative = 1;
see above.
>
However, what if I am to support an implementation that only provides
C89 (no long long, no signbit())?
see above.
>
Anybody got a reliable, portable method of handling this?
see above.
Apr 27 '07 #10

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

Similar topics

17
6157
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
3
3125
by: Weston C | last post by:
I'm coding up a small little script that's supposed to be used to display the number of hours until the US Financial markets open/close. Naturally, this involves getting the current time on the eastern seaboard, which, upon reflection, seems a little more difficult than I'd thought. This is largely because of daylight savings time. It's easy to grab UTC/GMT time using javascript and subtract 4/5 hours. The thing that doesn't seem...
3
28239
by: Ken Durden | last post by:
Is it possible to force positive values to have the + sign prefixed on them? double f1 = 1024.2; double f2 = -1024.2; string.Format( "{0:F}", f1 ); // +1024.2 string.Format( "{0:F}", f2 ); // -1024.2 I was hoping for something either in the string.Format prefix, or
5
8819
by: MathNewbie | last post by:
Hi, I'm trying to do get my head around some, probably basic, math calculations. I checked some .net math libs, but the only thing I learn from studying those is humility ;-). Anyway, i have an array of points on a basic 2 dimensional axis. They are all points of a sine shaped line. (not a perfect sine though, as it's actually tide movement data that is roughly looking as a sine).
14
1627
by: dascandy | last post by:
Hi, I was wondering, is it possible to determine whether a string can be modified (const char *) by the application or whether it's located in what's commonly .rodata? Regards, Peter
7
2383
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 )
4
2436
by: =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= | last post by:
In schema, several attributes have "special" values that start with a hash sign, e.g. #all (for final) and ##any, ##targetNamespace, ##local (for namespace). What is the rational for having the hash sign there? Wouldn't "all" have worked just as fine for fullDerivationSet? Regards, Martin
0
9706
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
9584
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
10583
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
10337
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...
0
9160
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...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.