Skip to content

Commit 32c80cb

Browse files
andyphillips404rozza
authored andcommitted
Update to FloatCodec to support negative numbers (#400)
in Java, the most negative number for a float is -Float.MAX_VALUE, not Float.MIN_VALUE. The original code would not parse any negative numbers are smaller than Float.MIN_VALUE. Float.MIN_VALUE is the smallest positive number. JAVA-2515
1 parent 1840712 commit 32c80cb

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

bson/src/main/org/bson/codecs/FloatCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void encode(final BsonWriter writer, final Float value, final EncoderCont
3636
@Override
3737
public Float decode(final BsonReader reader, final DecoderContext decoderContext) {
3838
double value = reader.readDouble();
39-
if (value < Float.MIN_VALUE || value > Float.MAX_VALUE) {
39+
if (value < -Float.MAX_VALUE || value > Float.MAX_VALUE) {
4040
throw new BsonInvalidOperationException(format("%s can not be converted into a Float.", value));
4141
}
4242
return (float) value;

bson/src/test/unit/org/bson/codecs/FloatCodecTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ public void shouldRoundTripFloatValues() {
3636
roundTrip(new Document("a", new Float(1)));
3737
}
3838

39+
@Test
40+
public void shouldRoundTripNegativeFloatValues() {
41+
roundTrip(new Document("a", new Float(-1)));
42+
}
43+
3944
@Test(expected = BsonInvalidOperationException.class)
4045
public void shouldErrorDecodingOutsideMinRange() {
41-
roundTrip(new Document("a", Double.MIN_VALUE));
46+
roundTrip(new Document("a", -Double.MAX_VALUE));
4247
}
4348

4449
@Test(expected = BsonInvalidOperationException.class)

0 commit comments

Comments
 (0)