Skip to content

Commit 855e53c

Browse files
committed
fix exception in Bits
1 parent 3e5e20c commit 855e53c

4 files changed

Lines changed: 19 additions & 4 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tasks.withType(JavaCompile) { options.encoding = "UTF-8" }
2626

2727
group = 'com.github.myibu'
2828
archivesBaseName = "algorithm-java"
29-
version = "1.0.3"
29+
version = "1.0.4"
3030

3131
repositories {
3232
mavenCentral()

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Reference to: [HoffmanAndGolombCoding.pdf](./docs/HoffmanAndGolombCoding.pdf)
5757
<dependency>
5858
<groupId>com.github.myibu</groupId>
5959
<artifactId>algorithm-java</artifactId>
60-
<version>1.0.3</version>
60+
<version>1.0.4</version>
6161
</dependency>
6262
```
6363

src/main/java/com/github/myibu/algorithm/data/Bits.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,21 +751,23 @@ public static double decodeDecimalValue(Bits value) {
751751

752752
public static float decodeFloatValue(Bits value) {
753753
if (value.length() != 32) throw new IllegalArgumentException("float value must be 32 bits");
754+
if (Bits.ofString("00000000000000000000000000000000").equals(value)) return 0.0f;
754755
Bits bits = value.clone();
755756
int S = bits.get(0).value();
756757
int E = bits.subBits(1, 9).toInt() - 127;
757758
Bits M = Bits.ofOne().append(bits.subBits(9, 32));
758-
float floatValue = (float) (decodeIntValue(M.subBits(0, 1 + E)) + decodeDecimalValue(M.subBits(1 + E, M.length())));
759+
float floatValue = (float)((1 + decodeDecimalValue(M.subBits(1, M.length())))* pow(2, E));
759760
return (S == 0 ? 1 : -1) * floatValue;
760761
}
761762

762763
public static double decodeDoubleValue(Bits value) {
763764
if (value.length() != 64) throw new IllegalArgumentException("double value must be 64 bits");
765+
if (Bits.ofString("0000000000000000000000000000000000000000000000000000000000000000").equals(value)) return 0.0;
764766
Bits bits = value.clone();
765767
int S = bits.get(0).value();
766768
int E = bits.subBits(1, 12).toInt() - 1023;
767769
Bits M = Bits.ofOne().append(bits.subBits(12, 64));
768-
double doubleValue = decodeIntValue(M.subBits(0, 1 + E)) + decodeDecimalValue(M.subBits(1 + E, M.length()));
770+
double doubleValue = (1 + decodeDecimalValue(M.subBits(1, M.length())))* pow(2, E);
769771
return (S == 0 ? 1 : -1) * doubleValue;
770772
}
771773
}

src/test/java/com/github/myibu/algorithm/AlgorithmTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,23 @@ public void testBitsEncoder() {
350350
Assert.assertEquals(Bits.ofString("01000001100011010000000000000000"), Bits.Encoder.encodeFloatValue(17.625f));
351351
Assert.assertEquals(Bits.ofString("01000000110000000000000000000000"), Bits.Encoder.encodeFloatValue(6.0f));
352352
Assert.assertEquals(Bits.ofString("00000000000000000000000000000000"), Bits.Encoder.encodeFloatValue(0.0f));
353+
Assert.assertEquals(Bits.ofString("10111111011111111111100101110010"), Bits.Encoder.encodeFloatValue(-0.9999f));
353354

354355
Assert.assertEquals(Bits.ofString("0100000000000010000000000000000000000000000000000000000000000000"), Bits.Encoder.encodeDoubleValue(2.25));
355356
Assert.assertEquals(Bits.ofString("0011111111010110110010001011010000111001010110000001000001100010"), Bits.Encoder.encodeDoubleValue(0.356));
356357
Assert.assertEquals(Bits.ofString("0100000000101001100100111111011111001110110110010001011010000111"), Bits.Encoder.encodeDoubleValue(12.789));
357358
Assert.assertEquals(Bits.ofString("1100000000101001100100111111011111001110110110010001011010000111"), Bits.Encoder.encodeDoubleValue(-12.789));
359+
Assert.assertEquals(Bits.ofString("1100000011000101101100111010101010101010101010101010101010101011"), Bits.Encoder.encodeDoubleValue(-11111.3333333333333333));
360+
361+
Assert.assertEquals(0.625f, Bits.Decoder.decodeFloatValue(Bits.ofString("00111111001000000000000000000000")), 0);
362+
Assert.assertEquals(0.0f, Bits.Decoder.decodeFloatValue(Bits.ofString("00000000000000000000000000000000")), 0);
363+
Assert.assertEquals(0.4f, Bits.Decoder.decodeFloatValue(Bits.ofString("00111110110011001100110011001101")), 0);
364+
Assert.assertEquals(-0.9999f, Bits.Decoder.decodeFloatValue(Bits.ofString("10111111011111111111100101110010")), 0);
365+
366+
Assert.assertEquals(2.25, Bits.Decoder.decodeDoubleValue(Bits.ofString("0100000000000010000000000000000000000000000000000000000000000000")), 0);
367+
Assert.assertEquals(0.356, Bits.Decoder.decodeDoubleValue(Bits.ofString("0011111111010110110010001011010000111001010110000001000001100010")), 0);
368+
Assert.assertEquals(12.789, Bits.Decoder.decodeDoubleValue(Bits.ofString("0100000000101001100100111111011111001110110110010001011010000111")), 0);
369+
Assert.assertEquals(-12.789, Bits.Decoder.decodeDoubleValue(Bits.ofString("1100000000101001100100111111011111001110110110010001011010000111")), 0);
370+
Assert.assertEquals(-11111.3333333333333333, Bits.Decoder.decodeDoubleValue(Bits.ofString("1100000011000101101100111010101010101010101010101010101010101011")), 0);
358371
}
359372
}

0 commit comments

Comments
 (0)