classlib: more fixes to Math.nextUp/nextDown (#746)

This commit is contained in:
Ivan Hetman 2023-09-19 14:21:32 +03:00 committed by GitHub
parent 617ce67871
commit 154bf7abd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -387,6 +387,9 @@ public final class TMath extends TObject {
if (TDouble.isNaN(d) || d == TDouble.POSITIVE_INFINITY) {
return d;
}
if (d == 0.0d) {
return Double.MIN_VALUE;
}
long bits = TDouble.doubleToLongBits(d);
if (d < 0) {
bits--;
@ -400,6 +403,9 @@ public final class TMath extends TObject {
if (TFloat.isNaN(d) || d == TFloat.POSITIVE_INFINITY) {
return d;
}
if (d == 0) {
return Float.MIN_VALUE;
}
int bits = TFloat.floatToIntBits(d);
if (d < 0) {
bits--;
@ -417,7 +423,7 @@ public final class TMath extends TObject {
return -Double.MIN_VALUE;
}
long bits = TDouble.doubleToLongBits(d);
if (d <= 0) {
if (d < 0) {
bits++;
} else {
bits--;
@ -433,7 +439,7 @@ public final class TMath extends TObject {
return -Float.MIN_VALUE;
}
int bits = TFloat.floatToIntBits(d);
if (d <= 0) {
if (d < 0) {
bits++;
} else {
bits--;

View File

@ -106,8 +106,12 @@ public class MathTest {
public void nextWorks() {
assertEquals(Double.valueOf(-Double.MIN_VALUE), Double.valueOf(Math.nextDown(0.0)));
assertEquals(Double.valueOf(Double.MIN_VALUE), Double.valueOf(Math.nextUp(0.0)));
assertEquals(Double.valueOf(-Double.MIN_VALUE), Double.valueOf(Math.nextDown(-0.0)));
assertEquals(Double.valueOf(Double.MIN_VALUE), Double.valueOf(Math.nextUp(-0.0)));
assertEquals(Float.valueOf(-Float.MIN_VALUE), Float.valueOf(Math.nextDown(0.0f)));
assertEquals(Float.valueOf(Float.MIN_VALUE), Float.valueOf(Math.nextUp(0.0f)));
assertEquals(Float.valueOf(-Float.MIN_VALUE), Float.valueOf(Math.nextDown(-0.0f)));
assertEquals(Float.valueOf(Float.MIN_VALUE), Float.valueOf(Math.nextUp(-0.0f)));
assertEquals(Double.valueOf(0.10000000000000002), Double.valueOf(Math.nextUp(0.1)));
assertEquals(Double.valueOf(0.9999999999999999), Double.valueOf(Math.nextDown(1.0)));
assertEquals(Double.valueOf(-0.09999999999999999), Double.valueOf(Math.nextUp(-0.1)));