Is it possible to check if a float
is a positive zero (0.0) or a negative zero (-0.0)?
是否可以检查一个浮点数是正值(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?
我已经将浮点数转换为字符串,并检查第一个字符是否是“-”,但是还有其他方法吗?
7 个解决方案
#1
78
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 = +∞,但1 / -0。0f = -∞。通过简单的比较,很容易找出是哪一个,所以你得到:
if (1 / x > 0)
// +0 here
else
// -0 here
(this assumes that x
can only be one of the two zeroes)
(假设x只能是两个0中的一个)
#2
35
You can use Float.floatToIntBits
to convert it to an int
and look at the bit pattern:
您可以使用浮动。floatToIntBits转换为int并查看位模式:
float f = -0.0f;
if (Float.floatToIntBits(f) == 0x80000000) {
System.out.println("Negative zero");
}
#3
11
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);
#4
7
The approach used by Math.min
is similar to what Jesper proposes but a little clearer:
数学使用的方法。min类似于Jesper的提议,但更清楚一点:
private static int negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
float f = -0.0f;
boolean isNegativeZero = (Float.floatToRawIntBits(f) == negativeZeroFloatBits);
#5
7
Double.equals
distinguishes ±0.0 in Java. (There's also Float.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!
我有点惊讶没有人提到这些,因为它们在我看来比迄今为止给出的任何方法都要清楚!
#6
5
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)时,它使用的符号位与负int相同。
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
}
#7
0
For negative:
负面:
new Double(-0.0).equals(new Double(value));
For positive:
积极的:
new Double(0.0).equals(new Double(value));
#1
78
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 = +∞,但1 / -0。0f = -∞。通过简单的比较,很容易找出是哪一个,所以你得到:
if (1 / x > 0)
// +0 here
else
// -0 here
(this assumes that x
can only be one of the two zeroes)
(假设x只能是两个0中的一个)
#2
35
You can use Float.floatToIntBits
to convert it to an int
and look at the bit pattern:
您可以使用浮动。floatToIntBits转换为int并查看位模式:
float f = -0.0f;
if (Float.floatToIntBits(f) == 0x80000000) {
System.out.println("Negative zero");
}
#3
11
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);
#4
7
The approach used by Math.min
is similar to what Jesper proposes but a little clearer:
数学使用的方法。min类似于Jesper的提议,但更清楚一点:
private static int negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
float f = -0.0f;
boolean isNegativeZero = (Float.floatToRawIntBits(f) == negativeZeroFloatBits);
#5
7
Double.equals
distinguishes ±0.0 in Java. (There's also Float.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!
我有点惊讶没有人提到这些,因为它们在我看来比迄今为止给出的任何方法都要清楚!
#6
5
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)时,它使用的符号位与负int相同。
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
}
#7
0
For negative:
负面:
new Double(-0.0).equals(new Double(value));
For positive:
积极的:
new Double(0.0).equals(new Double(value));