Detecting and adjusting for negative zero

谁说胖子不能爱 提交于 2019-12-01 06:05:52

Well, a generic suggestion when using doubles is remembering they are not exact. Thus, if equality is important - using some tolerance factor is usually advised.

In your case:

if (|r - 0.0| >= EPSILON)

where EPSILON is your tolerance factor, will yield true if r is not 0.0, with at least EPSILON interval.

Jerry Coffin

On some older systems (i.e. pre-IEE754) you may find equality checks against 0 fail for negative-0:

if (a == 0.0) // when a==-0.0, fails

you can work around this by adding 0.0 to a value before the comparison:

if ((a+0.0) == 0.0) // when a == -0.0, succeeds

I would caution, however, that combinations of hardware/software that really require this are quite unusual. The last time I had to do it was on a Control Data mainframe. Even there, it only arose under somewhat unusual circumstances: the Fortran compiler allowed negative zeros to be generated, and knew to compensate for them in comparisons. The Pascal compiler generated code to turn negative zeros into normal zeros as part of a computation.

Therefore, if you wrote a routine in Fortran and called it from Pascal, you could run into this problem, and prevent it as above by adding 0.0 before doing a comparison.

I'm gonna put pretty good odds that your problem doesn't really stem from comparisons with negative zero though. All reasonably modern hardware of which I'm aware handles this entirely automatically, so software never has to consider it at all.

Presumably you meant something like if (r2==-0.0). Nonetheless, both negative 0 and positive 0 will compare equal. For all intents and purposes there's no difference between the two. You probably don't need to have a special case for negative 0. Your comparison r >= 0 should be true for either negative or positive 0.

Consider:

#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    double const x = -2.78269e-07;

    printf( "printf: x=%f\n", x );
    cout << "cout: x=" << x << endl;
}

With result (using Visual C++ 11.0):

[D:\dev\test]
> cl foo.cpp
foo.cpp

[D:\dev\test]
> foo
printf: x=-0.000000
cout: x=-2.78269e-007

[D:\dev\test]
> _

This seems awfully similar to the mysterious result in the question.

It's my considered opinion that it quacks like the question's result, looks like the question's result and waddles like the question's result.

So, I believe that the not shown computation code produced the value 2.78269e-007.


So, in conclusion, it was apparently only the described behavior, about “negative zero” comparing unequal to zero, that was non-standard. In reality there is apparently no negative zero, just a very small negative value. Which with the given output format is presented as all zero digits with a minus sign in front.

It's just a very small negative number that the console couldn't print it for you
You can try checking for it depending on your precision so you can replace it with a pure zero.

std::cout << std::setprecision(7) << (abs(value) < 0.0000005f ? 0 : value);

Notice how I added 7 places to the floating precision as I specified with std::setprecision().

It depends on how accurately you want to print your floats/double.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!