473,396 Members | 2,085 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,396 software developers and data experts.

Doubles and zero/negative zero

How does one determine whether a double is equal to -0.0000...? Will
fabs() from math.h return 0.000... on -0.0000...?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
4 5817
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:cb**********@chessie.cirr.com...
How does one determine whether a double is equal to -0.0000...?
You have to use a C99 function such as signbit or copysign to
see the sign bit, because -0 == +0.
Will
fabs() from math.h return 0.000... on -0.0000...?


That's what you get with IEEE conformance, but otherwise all bets
are off.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #2


Christopher Benson-Manica wrote:
How does one determine whether a double is equal to -0.0000...? Will
fabs() from math.h return 0.000... on -0.0000...?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


One way would be:

if( (Number < 0) &&( Number > -0.00001))
You get to figure out how many zeros work for you
Nov 14 '05 #3
Neil Kurzman <ns*@mail.asb.com> wrote:
Christopher Benson-Manica wrote:
How does one determine whether a double is equal to -0.0000...? Will
fabs() from math.h return 0.000... on -0.0000...?


One way would be:

if( (Number < 0) &&( Number > -0.00001))


And, erm... who guarantees that -0.000 < 0?

Richard
Nov 14 '05 #4

"Neil Kurzman" <ns*@mail.asb.com> wrote in message
news:40***************@mail.asb.com...
Christopher Benson-Manica wrote:
How does one determine whether a double is equal to -0.0000...? Will
fabs() from math.h return 0.000... on -0.0000...?

One way would be:

if( (Number < 0) &&( Number > -0.00001))
You get to figure out how many zeros work for you


That won't work, since (Number<0) will be false for Number=-0. The -0
floating point bit pattern is all 0 bits except the sign bit, which is
turned on. The only time the sign of zero matters is when determining which
side of 0 you're on if you're on a singularity at 0 (called a branch cut).
The IEEE floating point arithmetic routines carefully keep track of -0's,
but to test for it you'll need to check directly for the sign bit, which is
easy enough with:

#include <math.h>

if (signbit(x)) ...

fabs, if implemented correctly, will convert -0 to 0.

Here's a brief introduction to IEEE arithmetic:
www.digitalmars.com/ctg/ctgNumerics.html

Here are some math functions, and what they do with -0:
www.digitalmars.com/rtl/math.html

-Walter
www.digitalmars.com free C/C++/D compilers

Nov 14 '05 #5

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

Similar topics

17
by: matthias_k | last post by:
Hi, I am currently implementing a solver for linear optimization problems using the revised simplex method and I stumbled accross some strange behavior regarding the treatment of the number 0....
1
by: Joe Harris | last post by:
I've searched everywhere but can't find a KB article or forum message about this one... Excel Pivot Tables differentiate between +0 (zero) and -0 (negative zero)and now that I've moved over to...
2
by: GGerard | last post by:
Hello I have an Inventory text box in a datasheet that has the Control Source property set to =* and the Format property set to Currency. The datasheet is linked to the Stock table that has the...
11
by: Martin | last post by:
In Plauger's THE STANDARD C LIBRARY (1992) there is the source code for fabs.c (p140). /* fabs function */ #include "xmath.h" double (fabs)(double x) { switch (_Dtest(&x)) { /* test for...
8
by: Avin Patel | last post by:
Hi, Does the Double has the facilty to define -0. If it has, "==" or System.Math.Sign() doesn't able to differentiate between -0 & 0. i.e. Double x = -0.0d Double y = 0.0d if(value ==...
9
by: itportal | last post by:
Hello, This will probably be a simple question. How can I round float/doubles to a specific digit after the comma/dot? For example we have float i = 1.234567. I want to round it to 1.234....
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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,...

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.