Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue 24 #25

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/com/upokecenter/cbor/CBORUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -1517,14 +1517,16 @@ public static int DoubleToHalfPrecisionIfSameValue(long bits) {
int newmant = ((int)(mant >> 42));
return ((mant & ((1L << 42) - 1)) == 0) ? (sign | 0x7c00 | newmant) :
-1;
} else if (exp == 0 && mant == 0) { // positive or negative zero always fits in half precision
return sign;
} else if (sexp >= 31) { // overflow
return -1;
} else if (sexp < -10) { // underflow
return -1;
} else if (sexp > 0) { // normal
return ((mant & ((1L << 42) - 1)) == 0) ? (sign | (sexp << 10) |
RoundedShift(mant, 42)) : -1;
} else { // subnormal and zero
} else { // subnormal but nonzero
int rs = RoundedShift(mant | (1L << 52), 42 - (sexp - 1));
// System.out.println("mant=" + mant + " rs=" + (rs));
return sexp == -10 && rs == 0 ? -1 :
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/upokecenter/test/CBORNumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,43 @@ public void TestIsNaN() {
}
}

@Test
public void TestEncodingZeros() {
TestCommon.CompareTestEqual(ToCN(0.0), ToCN(-0.0).Abs());
TestCommon.CompareTestEqual(ToCN(0.0f), ToCN(-0.0f).Abs());

if (!CBORObject.FromObject(0.0).AsNumber().CanFitInSingle()) {
Assert.fail();
}
if (!CBORObject.FromObject(-0.0).AsNumber().CanFitInSingle()) {
Assert.fail();
}
if (!CBORObject.FromObject(0.0f).AsNumber().CanFitInSingle()) {
Assert.fail();
}
if (!CBORObject.FromObject(-0.0f).AsNumber().CanFitInSingle()) {
Assert.fail();
}

ToObjectTest.TestToFromObjectRoundTrip(0.0);
ToObjectTest.TestToFromObjectRoundTrip(0.0f);
ToObjectTest.TestToFromObjectRoundTrip(-0.0);
ToObjectTest.TestToFromObjectRoundTrip(-0.0f);

TestCommon.CompareTestEqual(ToCN(0.0), CBORObject.FromObject(-0.0).AsNumber().Negate());
TestCommon.CompareTestEqual(ToCN(-0.0), CBORObject.FromObject(0.0).AsNumber().Negate());
TestCommon.CompareTestEqual(ToCN(0.0f), CBORObject.FromObject(-0.0f).AsNumber().Negate());
TestCommon.CompareTestEqual(ToCN(-0.0f), CBORObject.FromObject(0.0f).AsNumber().Negate());

assert (CBORObject.FromObject(1.0f).EncodeToBytes().length == 3);
assert (CBORObject.FromObject(1.0).EncodeToBytes().length == 3);
assert (CBORObject.FromObject(0.0f).EncodeToBytes().length == 3);
assert (CBORObject.FromObject(-0.0f).EncodeToBytes().length == 3);
assert (CBORObject.FromObject(0.0).EncodeToBytes().length == 3);
assert (CBORObject.FromObject(-0.0).EncodeToBytes().length == 3);
}


@Test
public void TestNegate() {
// not implemented yet
Expand Down