Java 如何检查零是正数还是负数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22409102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if a zero is positive or negative?
提问by e4lime
Is it possible to check if a float
is a positive zero (0.0) or a negative zero (-0.0)?
是否可以检查 afloat
是正零 (0.0) 还是负零 (-0.0)?
I've converted the float
to a String
and checked if the first char
is a '-'
, but are there any other ways?
我已将 the 转换float
为 aString
并检查第一个char
是否为 a '-'
,但还有其他方法吗?
采纳答案by harold
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:
是的,除以它。1 / +0.0f
是+Infinity
,但是1 / -0.0f
是-Infinity
。通过简单的比较很容易找出它是哪一个,因此您会得到:
if (1 / x > 0)
// +0 here
else
// -0 here
(this assumes that x
can only be one of the two zeroes)
(这假设x
只能是两个零之一)
回答by Jesper
You can use Float.floatToIntBits
to convert it to an int
and look at the bit pattern:
您可以使用Float.floatToIntBits
将其转换为 anint
并查看位模式:
float f = -0.0f;
if (Float.floatToIntBits(f) == 0x80000000) {
System.out.println("Negative zero");
}
回答by andre baresel
Definitly not the best aproach. Checkout the function
绝对不是最好的方法。签出功能
Float.floatToRawIntBits(f);
Doku:
独行:
/**
* Returns a representation of the specified floating-point value
* according to the IEEE 754 floating-point "single format" bit
* layout, preserving Not-a-Number (NaN) values.
*
* <p>Bit 31 (the bit that is selected by the mask
* {@code 0x80000000}) represents the sign of the floating-point
* number.
...
public static native int floatToRawIntBits(float value);
回答by assylias
The approach used by Math.min
is similar to what Jesper proposes but a little clearer:
使用的方法Math.min
类似于 Jesper 提出的方法,但更清晰一点:
private static int negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
float f = -0.0f;
boolean isNegativeZero = (Float.floatToRawIntBits(f) == negativeZeroFloatBits);
回答by Kip
When a float is negative (including -0.0
and -inf
), it uses the same sign bit as a negative int. This means you can compare the integer representation to 0
, eliminating the need to know or compute the integer representation of -0.0
:
当浮点数为负数(包括-0.0
和-inf
)时,它使用与负整数相同的符号位。这意味着您可以将整数表示与 进行比较0
,从而无需了解或计算 的整数表示-0.0
:
if(f == 0.0) {
if(Float.floatToIntBits(f) < 0) {
//negative zero
} else {
//positive zero
}
}
That has an extra branch over the accepted answer, but I think it's more readable without a hex constant.
这在接受的答案上有一个额外的分支,但我认为它在没有十六进制常量的情况下更具可读性。
If your goal is just to treat -0 as a negative number, you could leave out the outer if
statement:
如果您的目标只是将 -0 视为负数,则可以省略外部if
语句:
if(Float.floatToIntBits(f) < 0) {
//any negative float, including -0.0 and -inf
} else {
//any non-negative float, including +0.0, +inf, and NaN
}
回答by Reuben Thomas
Double.equals
distinguishes ±0.0 in Java. (There's also Float.equals
.)
Double.equals
在 Java 中区分 ±0.0。(还有Float.equals
。)
I'm a bit surprised no-one has mentioned these, as they seem to me clearer than any method given so far!
我有点惊讶没有人提到这些,因为在我看来它们比迄今为止给出的任何方法都更清晰!
回答by Tridib
For negative:
对于否定:
new Double(-0.0).equals(new Double(value));
For positive:
对于阳性:
new Double(0.0).equals(new Double(value));