classlib: fix support of positive/negative zeros in Float/Double.toHexString (#830)

This commit is contained in:
Ivan Hetman 2023-10-27 17:52:33 +03:00 committed by GitHub
parent 1061ad76b6
commit fc49094d96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 2 deletions

View File

@ -293,9 +293,13 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
int sz = 0;
long bits = doubleToLongBits(d);
boolean subNormal = false;
boolean negative = (bits & (1L << 63)) != 0;
int exp = (int) ((bits >>> 52) & 0x7FF) - 1023;
long mantissa = bits & 0xFFFFFFFFFFFFFL;
if (exp == -1023) {
if (mantissa == 0) {
return negative ? "-0x0.0p0" : "0x0.0p0";
}
++exp;
subNormal = true;
}
@ -314,7 +318,7 @@ public class TDouble extends TNumber implements TComparable<TDouble> {
buffer[sz++] = subNormal ? '0' : '1';
buffer[sz++] = 'x';
buffer[sz++] = '0';
if ((bits & (1L << 63)) != 0) {
if (negative) {
buffer[sz++] = '-';
}
int half = sz / 2;

View File

@ -289,9 +289,13 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
int sz = 0;
int bits = floatToIntBits(f);
boolean subNormal = false;
boolean negative = (bits & (1 << 31)) != 0;
int exp = ((bits >>> 23) & 0xFF) - 127;
int mantissa = (bits & 0x7FFFFF) << 1;
if (exp == -127) {
if (mantissa == 0) {
return negative ? "-0x0.0p0" : "0x0.0p0";
}
++exp;
subNormal = true;
}
@ -310,7 +314,7 @@ public class TFloat extends TNumber implements TComparable<TFloat> {
buffer[sz++] = subNormal ? '0' : '1';
buffer[sz++] = 'x';
buffer[sz++] = '0';
if ((bits & (1L << 31)) != 0) {
if (negative) {
buffer[sz++] = '-';
}
int half = sz / 2;

View File

@ -131,6 +131,8 @@ public class DoubleTest {
assertEquals("0x1.0p-1022", Double.toHexString(0x1.0p-1022));
assertEquals("0x0.8p-1022", Double.toHexString(0x0.8p-1022));
assertEquals("0x0.001p-1022", Double.toHexString(0x0.001p-1022));
assertEquals("0x0.0p0", Double.toHexString(0.0D));
assertEquals("-0x0.0p0", Double.toHexString(-0.0D));
}
@Test

View File

@ -112,6 +112,8 @@ public class FloatTest {
assertEquals("0x1.0p-2", Float.toHexString(0.25f));
assertEquals("0x1.0p-126", Float.toHexString((float) Math.pow(2, -126)));
assertEquals("0x0.001p-126", Float.toHexString(0x0.001p-126f));
assertEquals("0x0.0p0", Float.toHexString(0.0F));
assertEquals("-0x0.0p0", Float.toHexString(-0.0F));
}
@Test