fixes a bug in the new strLen function

This commit is contained in:
hneemann 2019-09-24 19:54:57 +02:00
parent 0f60483bcf
commit 522f6a1883
2 changed files with 19 additions and 2 deletions

View File

@ -99,9 +99,9 @@ public enum IntFormat {
public int strLen(int bits) {
switch (this) {
case dec:
return (int) Math.ceil(Math.log10(1L << bits));
return decStrLen(bits);
case decSigned:
return (int) Math.ceil(Math.log10(1L << (bits - 1))) + 1;
return decStrLen(bits - 1) + 1;
case hex:
return (bits - 1) / 4 + 3;
case bin:
@ -115,6 +115,15 @@ public enum IntFormat {
}
}
private int decStrLen(int bits) {
if (bits == 64)
return 20;
else if (bits == 63) {
return 19;
} else
return (int) Math.ceil(Math.log10(1L << bits));
}
private static final char[] DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static String toHex(Value inValue) {

View File

@ -101,11 +101,19 @@ public class IntFormatTest extends TestCase {
assertEquals(3,IntFormat.dec.strLen(8));
assertEquals(3,IntFormat.dec.strLen(9));
assertEquals(4,IntFormat.dec.strLen(10));
assertEquals(19, IntFormat.dec.strLen(60));
assertEquals(19, IntFormat.dec.strLen(61));
assertEquals(19, IntFormat.dec.strLen(62));
assertEquals(19, IntFormat.dec.strLen(63));
assertEquals(20, IntFormat.dec.strLen(64));
assertEquals(4,IntFormat.decSigned.strLen(8));
assertEquals(4,IntFormat.decSigned.strLen(9));
assertEquals(4,IntFormat.decSigned.strLen(10));
assertEquals(5,IntFormat.decSigned.strLen(11));
assertEquals(20, IntFormat.decSigned.strLen(62));
assertEquals(20, IntFormat.decSigned.strLen(63));
assertEquals(20, IntFormat.decSigned.strLen(64));
assertEquals(4,IntFormat.oct.strLen(4));
assertEquals(4,IntFormat.oct.strLen(5));