How can I test for negative zero?
Here's a grotty hack way of doing it:
private static readonly long NegativeZeroBits =
BitConverter.DoubleToInt64Bits(-0.0);
public static bool IsNegativeZero(double x)
{
return BitConverter.DoubleToInt64Bits(x) == NegativeZeroBits;
}
Basically that's testing for the exact bit pattern of -0.0, but without having to hardcode it.
How can negative zero be tested for in PHP?
@njuffa suggested if (1 / $x === -INF)
, which works but throws a division by zero warning.
@Manu-sh modified it to do the same thing without the warning by using the new "power of" operator: **
<?php
function is_negative_zero($x) {
return $x ** -1 === -INF;
}
echo is_negative_zero(0.0)? "Yes": "No";
echo PHP_EOL;
echo is_negative_zero(-0.0)? "Yes": "No";
echo PHP_EOL;
Output:
No
Yes
How can I differentiate between zero and negative zero?
This is similar to a Codewars Kata that I completed. The trick is to divide by the number that you want to check is negative zero, like this:
int isPositiveZero(double a) {
return 1/a == 1/0.0;
}
The division gives -infinity
if a
is negative zero, and infinity
if a
is zero.
If you want to do it your way, try this:
(*((long *)&a) & 0x8000000000000000)
Note that this violates the strict aliasing rule, which causes undefined behavior. This means that your program could do anything from printing the right result to making your computer sprout wings and fly away.
If you have math.h
available and don't have an aversion to using macros from the standard C library, then this solution (using signbit
) will also work (and be much more portable):
#include <math.h>
int isPositiveZero(double a) {
return a == 0.0 && !signbit(a);
}
How do I check if a zero is positive or negative?
Yes, divide by it. 1 / +0.0f
is +Infinity
, but 1 / -0.0f
is -Infinity
. It's easy to find out which one it is with a simple comparison, so you get:
if (1 / x > 0)
// +0 here
else
// -0 here
(this assumes that x
can only be one of the two zeroes)
How to make jest not distinguish between negative zero and positive zero?
You can slightly sidestep the issue by doing your logic inside of the expect
function:
expect(0).toEqual(-0); // fails
expect(0 === -0).toEqual(true); // ugly, but it works!
How to handle rounding to negative zero in Python docstring tests
You can print the rounded array plus 0.0
to eliminate the -0
:
A = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])
Q = orth(A)
Q[0,1] = -1e-16 # simulate a small floating point deviation
print(np.array2string(Q.round(8)+0.0, precision=8, suppress_small=True))
#[[-0.70710678 0. ]
# [ 0. 1. ]
# [-0.70710678 0. ]]
So your doc string should be:
>>> Q = orth(A)
>>> print(np.array2string(Q.round(8)+0.0, precision=8, suppress_small=True)) # guarantee non-negative zeros
[[-0.70710678 0. ]
[ 0. 1. ]
[-0.70710678 0. ]]
How do I seperate negative zero from positive zero?
There are “negative zero” and “positive zero” floating point numbers.
You can disambiguate them by checking the .sign
property, but they
(intentionally) compare as equal:
let neg: CGFloat = -0.0
let pos: CGFloat = +0.0
print(neg.sign) // minus
print(pos.sign) // plus
print(neg == pos) // true
if (neg == pos && neg.sign == pos.sign) {
// This is not executed.
}
Note that you have to use floating point literals ("-0.0" or "+0.0")
in the initialization. The integer literals ("+0", "-0") are equal
and converted to positive zero.
Detecting and adjusting for negative zero
Well, a generic suggestion when using double
s 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.
Related Topics
How to Sort a List<T> by Multiple T.Attributes
How to Write a Comment to an Xml File When Using the Xmlserializer
Modify Xml Existing Content in C#
Check If Datetime Instance Falls in Between Other Two Datetime Objects
How to Use Dependency Injection in a .Net Core Actionfilterattribute
How to Set Chrome Preferences Using Selenium Webdriver .Net Binding
C# Regex: Checking for "A-Z" and "A-Z"
How to Hide Public Methods from Intellisense
Entity Framework Db-First, Implement Inheritance
Using Sse in C# Is It Possible
Are .Net Switch Statements Hashed or Indexed
Invoking Powershell Cmdlets from C#
Combobox.Selectedtext Doesn't Give Me the Selectedtext
Print HTML Document from Windows Service Without Print Dialog
How to Connect to a Ms Access File (Mdb) Using C#
How to Terminate a Thread in C#