473,657 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Weird numerical behavior regarding zero

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?
Anyway, this does have some very annoying consequences, since this
expression:
x / -0 yields -inf (x is positive).

Which brings me to my second question. Shouldn't division by zero make
my computer go up in flames or so? Because, on my system (Arch Linux 0.7
i686) with my compiler (g++ 3.4.3), dividing by zero is allowed.
In fact, it yields +inf when dividing by +0 and -inf when dividing by
-0, given that the nominator (is that the english term for "Zähler"?) is
positive of course.

Any comments/ideas?

Regards,
Matthias
Jul 23 '05 #1
17 2856
"matthias_k " <no****@digital raid.com> wrote in message
news:ct******** *****@news.t-online.com...
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?
IEEE floating point requires +0 and -0 as distinct representations .
Anyway, this does have some very annoying consequences, since this
expression:
x / -0 yields -inf (x is positive).
Yes, that is what IEEE floating point requires.
Which brings me to my second question. Shouldn't division by zero make my
computer go up in flames or so? Because, on my system (Arch Linux 0.7
i686) with my compiler (g++ 3.4.3), dividing by zero is allowed.


C++ says that what happens after division by zero is undefined.

IEEE says that the result is either a floating-point trap or an
appropriately signed infinity, depending on how you have set things up.

The behavior you have described so far is completely normal according to
IEEE.
Jul 23 '05 #2
Andrew Koenig wrote:
"matthias_k " <no****@digital raid.com> wrote in message
news:ct******** *****@news.t-online.com...

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?

IEEE floating point requires +0 and -0 as distinct representations .

Anyway, this does have some very annoying consequences, since this
expression:
x / -0 yields -inf (x is positive).

Yes, that is what IEEE floating point requires.

Which brings me to my second question. Shouldn't division by zero make my
computer go up in flames or so? Because, on my system (Arch Linux 0.7
i686) with my compiler (g++ 3.4.3), dividing by zero is allowed.

C++ says that what happens after division by zero is undefined.

IEEE says that the result is either a floating-point trap or an
appropriately signed infinity, depending on how you have set things up.

The behavior you have described so far is completely normal according to
IEEE.


Thanks Andrew. That is terrible news. :)
Does that mean I have to multiply each -0 with -1 to get a sane
representation of zero?
Jul 23 '05 #3

"matthias_k " <no****@digital raid.com> wrote in message
news:ct******** *****@news.t-online.com...
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.


Depending on how you are obtaining your "signed zero" values, many different
things could be happening. You should use the constants defined in
std::numeric_li mits against your values to figure out what is happening.
For example, if the problem values results from subtraction operations, you
have to check them against the machine accuracy (e.g.
std::numeric_li mits<float>::ep silon()) to make sure they haven't lost all
significance. I guess this is what happened in your case, and the sign bit
is just staying set.
HTH,

Dave Moore
Jul 23 '05 #4
Dave Moore wrote:
"matthias_k " <no****@digital raid.com> wrote in message
news:ct******** *****@news.t-online.com...
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.

Depending on how you are obtaining your "signed zero" values, many different
things could be happening. You should use the constants defined in
std::numeric_li mits against your values to figure out what is happening.
For example, if the problem values results from subtraction operations, you
have to check them against the machine accuracy (e.g.
std::numeric_li mits<float>::ep silon()) to make sure they haven't lost all
significance. I guess this is what happened in your case, and the sign bit
is just staying set.
HTH,

Dave Moore


I am doing lots of subtraction, multiplication and division.
What do you mean by check against the machine accuracy? Does -0 imply
that it's not 0 but something really close to 0? I can't check that for
each arithmetical operation, that's too expensive.
Jul 23 '05 #5
matthias_k wrote:
Does that mean I have to multiply each -0 with -1 to get a sane
representation of zero?


No, it means that you have to rethink what you mean by zero. <g>

The problem is that for negative values of x, 1/x is also negative. As x
approaches zero from below, 1/x becomes a larger and larger negative
value. The limit of 1/x as x approaches zero from below is negative
infinity. Similarly, for positive valus of y, 1/y is also positive. As y
approaches zero from above, 1/y becomes a larger and larger positive
value. The limit of 1/y as y approaches zero from above is positive
infinity. If you don't distinguish between negative and positive zeroes
you end up with the limits of both of those expressions being the same.
While it's possible to do math that way, it's far more intuitive to have
negative and positive infinity as two distinct values, and that in turn
means that you need two distinct zeroes, one positive and one negative.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #6
matthias_k wrote:

[ ... ]
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?
The C and C++ standards allow this. The IEEE floating point standards
_require_ it, if memory serves at all.
Anyway, this does have some very annoying consequences, since this
expression:
x / -0 yields -inf (x is positive).

Which brings me to my second question. Shouldn't division by zero make my computer go up in flames or so?
According to C it gives undefined behavior -- which can include
returning a value it considers reasonable. C99 added a number of
functions for controlling how floating point is done. AFAIK, they
haven't officially been adopted into C++ yet, but your compiler may
include them anyway -- if you have an fenv.h, chances are you can use
its functions from C++ as well as from C. I could be wrong, but at
first glance I'd expect FE_OVERFLOW to apply to division by 0.
Because, on my system (Arch Linux 0.7
i686) with my compiler (g++ 3.4.3), dividing by zero is allowed.
In fact, it yields +inf when dividing by +0 and -inf when dividing by -0, given that the nominator (is that the english term for "Zähler"?) is positive of course.


"Numerator" , is the word you seem to be looking for. In any case, as I
said above, the C++ standard simply makes division by 0 undefined
behavior, so anthing is possible.

The usual way to deal with negative zeros is not by multiplication, but
by addition -- a negative zero plus a positive zero gives a positive
zero, so to remove negative zeros, you add 0.0 to anything that might
have resulted in a negative zero.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #7
matthias_k wrote:

Depending on how you are obtaining your "signed zero" values, many different
things could be happening. You should use the constants defined in
std::numeric_li mits against your values to figure out what is happening.
For example, if the problem values results from subtraction operations, you
have to check them against the machine accuracy (e.g.
std::numeric_li mits<float>::ep silon()) to make sure they haven't lost all
significance. I guess this is what happened in your case, and the sign bit
is just staying set.
HTH,

Dave Moore


I am doing lots of subtraction, multiplication and division.
What do you mean by check against the machine accuracy? Does -0 imply
that it's not 0 but something really close to 0? I can't check that for
each arithmetical operation, that's too expensive.


Taking your comments into account I would say that lots
of surprises are waiting for you.

First of all, you really shouldn't divide by 0. No matter if
IEEE allows it or not. Simply don't do it. If you are bound
to divide by 0, then something strange has happend and your
program has to react on that.

What Dave means. How do you know that something is exactly
+0 or -0. In programmers real life you can't distinguish between
them, because you don't know if the sign is correct or not.
If eg. the result of a subtraction or addition happens to be
-0 then you never know if the sign is indeed '-' or if it
(mathematically ) should be '+'. This is because of small round
of errors which happen all the time in floating point arithmetic.
So the best you can do is:
do the arithmetic and before you you perform the division,
check if you don't divide by a number that is small enough
to be assumed to be 0. If you figure out that you are going
to do that, consider it to be an error and react to it.
Do *not* depend on the result to be +inf or -inf, those
results will be wrong on most of the cases anyway.

In a computer 2 * 0.1 does *not* equal 0.2, but something that
is very close to 0.2 but not exactly 0.2
You need to take that into account with every floating point
operation you make.

And no: checking for division by 0.0 is *never* too expensive
no matter how many operations you have to perform. There is only
one exception: If you can proove that your division will never
be executed with something that is even close to 0.0. In all
other cases: Do the check, but do it correct:

if( fabs( some_number ) < some_epsilon ) {
// some_number is close to 0.0
// treat it as 0.0 and react to it
}
else
op = 5.0 / some_number;

As for the value of some_epsilon. It mostly depends on the
history of some_number, how many 'floating point debris'
has accumulated in it. This is dependent on
a) the number of operations
b) the range of the numbers participating in those operations.

Using a value like std::numeric_li mits<float>::ep silon() isn't going
to help.

And by the way: Never use float, until you know what you
do, have the knowledge to fight that beast, are willing to fight
that beast and have a very, very, very, very good reason to use
it. In all other cases (that is 99.99% of all cases) use double.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #8
Karl Heinz Buchegger wrote:

And no: checking for division by 0.0 is *never* too expensive
no matter how many operations you have to perform.


The reason for having both forms of zero, infinities, and NaNs is so
that you don't have to check before every operation. Once an error
occurs it propogates through the subsequent calculations, so you only
have to check at the end. The underlying assumption is that errors are
rare, so not checking for them makes complicated computations much faster.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #9
Pete Becker wrote:
matthias_k wrote:
Does that mean I have to multiply each -0 with -1 to get a sane
representation of zero?


No, it means that you have to rethink what you mean by zero. <g>

[SNIP]

Just as an (off-topic) sidenote to that: (probably) that definition of
floating point zero has kept a lot of people wondering for quite some time
in Hungary. A public digital thermometer was installed at the capital, and
it was sometimes showing -0 Celsius (Centigrade) and sometimes +0. People
came up with loads of explanations as to why that thermometer made a
difference between -0 and +0 degrees, why was it important. I guess that
Forest Gump was closest to the solution when he said: for no particular
reason. :-)

--
WW aka Attila
:::
My software never has bugs. It just develops random features.
Jul 23 '05 #10

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

Similar topics

5
3836
by: mma | last post by:
I have been using the lubksb routine in Visual C++ 6.0 and noticed what looks like an error to me. The last section of the method looks like this: for(i=n;i>=1;i--) { sum=b; for(j=i+1;j<=n;j++) sum -= a*b; b=sum/a;
3
1586
by: sapnsapn | last post by:
Hi folks, I am looking at an application written in C some 10+ years ago. There is a lot of such if statements: if (my_number) {....} else {...} Here, my_number is an unsigned int. What exactly is the "if" looking for to evaluate? Is it the existence of a numerical value or a non-zero value or just any positive value? I am not a C programmer by trade and hence this question.
11
1945
by: ncf | last post by:
Ok, I've been tring to resolve this issue for some time now (~1 day which is way longer than normal for me) to no avail. I am reading a file into a list in memory, using a "%" delimited file format (which allows for comments on lines beginning with "#"), and some of the messages are not correctly copied. I believe the problem may be somewhere around the strcpy() call, but am not sure at all.
3
1903
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
1
1197
by: Bill Rapoza | last post by:
We have a method that executes an arbitrary batch file (we use it for remote upgrades of our system). When we execute this method from outside of ASP.NET (either in the foreground app or from a windows service), the method works fine. However, when we execute it from ASP.NET, the execution fails on some machines. Now, if instead of running the batch file directly, I run cmd.exe and have it run the batch file: cmd /k platformupgrade.bat
10
2155
by: alsmeirelles | last post by:
Hi all, I Have run this test: Private Sub test() Dim d As Double Dim f As Single Dim output As String d = 8888888888888.8887
16
1762
by: Bill Nguyen | last post by:
I'm running into a very weird problem regarding subtraction. Subtraction behaves as if it's an addition in the below sub txtJacoCost.Text = Format(mRackc - (mDisc + mJaEc), "0.#####0") txtWfCost.Text = Format(mRackc - mDisc + mWfEC, "0.#####0")
17
1925
by: somenath | last post by:
Hi All, I have one question regarding the bellow mentioned code #include<stdio.h> int main(void) { int x = 0; int y = 0;
10
1059
by: Pekka Laukkanen | last post by:
Hello, just noticed this: Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) on darwin Type "help", "copyright", "credits" or "license" for more information. {1: 2} {True: False} {1: False}
0
8420
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
8740
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
8617
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
7353
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
5642
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
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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.