473,806 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
4 5858
"Christophe r Benson-Manica" <at***@nospam.c yberspace.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)cybers pace.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.c om> 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.c om> wrote in message
news:40******** *******@mail.as b.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
2867
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. I am not sure whether this is compiler or language related though. The first problem is, that there are two representations of zero, namely +0 and -0. Does IEEE for floating points allow this? Maybe I didn't activate IEEE floating point numbers?
1
2980
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 Access 2002 (from 97) and I've found the same thing. However, I've used a Pivot Table to produce a summary across a standard distribution (-1 to 1 in increments of 0.05) and this works fine (both zeroes shown) but when I attempt to recreate this...
2
2396
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 Qty and Cost fields. Sometimes the Qty value drops below zero and this causes a negative value in the Inventory text box. How would I get the Inventory text box to display zero when the Qty * Cost
11
3520
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 special codes */ case NAN:
8
4653
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 == -0.0d) {//both x & y drops in this statement.
9
2691
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. Whan function should I use?
12
6863
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) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a))) Do the same issues involved in comparing 2 fp types for equality apply to comparing a float to zero? E.g. is if(x == 0.0) considered harmful?
27
3872
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 fails if comparing doubles of different magnitude since it's hard or not possible to find a suitable threshold
23
4382
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 non-two's-complement machine. Just wondering.) -- Hallvard
0
9719
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
9597
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
10369
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
10372
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
10110
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
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
6877
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
5546
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...
2
3851
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.