Skip to content

Commit

Permalink
Fix incorrect formats for 0 and rounding very small values
Browse files Browse the repository at this point in the history
  • Loading branch information
Tisawesomeness committed Sep 19, 2023
1 parent b3c4a57 commit 049d05d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ public static long clamp(long val, long low, long high) {
}

/**
* Computes the largest mantissa {@code n} such that {@code d = x * 10^n} for some {@code 1 <= x < 10}.
* <br>In other words, computes the largest exponent {@code n} such that {@code 10^n < d}.
* Computes the largest mantissa {@code n} such that {@code d = x * 10^n} for some {@code 1 <= abs(x) < 10}.
* <br>In other words, computes the largest exponent {@code n} such that {@code 10^n < abs(d)}.
* @param d input number
* @return {@code n}
* @return {@code n}, or 0 if the input is 0
*/
public static int mantissa(double d) {
return (int) Math.floor(StrictMath.log10(d));
if (d == 0.0) {
return 0;
}
return (int) Math.floor(StrictMath.log10(Math.abs(d)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ private String addApproxSymbolIfNotExact(NumberFormat format, double d) {
if (!includeApproximationSymbol || format.getMaximumFractionDigits() > -Double.MIN_EXPONENT) {
return original;
}
format.setMaximumFractionDigits(format.getMaximumFractionDigits() + 1);
String withOneMoreDigit = format.format(d);
return original.equals(withOneMoreDigit) ? original : "~" + original;
format.setMaximumFractionDigits(-Double.MIN_EXPONENT);
String withAllDigits = format.format(d);
return original.equals(withAllDigits) ? original : "~" + original;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void testClampBad() {

@ParameterizedTest
@CsvSource({
"0.0, 0",
"0.000001, -6",
"0.01, -2",
"0.1, -1",
Expand All @@ -46,7 +47,17 @@ public void testClampBad() {
"1.1, 0",
"10.0, 1",
"100.0, 2",
"1000000.0, 6"
"1000000.0, 6",
"-0.0, 0",
"-0.000001, -6",
"-0.01, -2",
"-0.1, -1",
"-0.9, -1",
"-1.0, 0",
"-1.1, 0",
"-10.0, 1",
"-100.0, 2",
"-1000000.0, 6"
})
public void testMantissa(double input, int expected) {
assertThat(Mth.mantissa(input))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public void testMinMaxExponentError() {
@CsvSource({
"123.456, '123.456'",
"0.1, '0.1'",
"2.0, '2'"
"2.0, '2'",
"0.0, '0'"
})
public void testFormat(double input, String expected) {
HumanDecimalFormat format = HumanDecimalFormat.builder().build();
Expand All @@ -56,7 +57,8 @@ public void testFormat(double input, String expected) {
"-4, 2, 0.000123, '0.000123'",
"-10, 10, 123000.0, '123000'",
"-4, 4, 123000.0, '1.23e5'",
"-2, 5, 123000.0, '123000'"
"-2, 5, 123000.0, '123000'",
"0, 0, 0.0, '0'"
})
public void testFormatScientific(int minDigits, int maxDigits, double input, String expected) {
HumanDecimalFormat format = HumanDecimalFormat.builder()
Expand All @@ -74,7 +76,8 @@ public void testFormatScientific(int minDigits, int maxDigits, double input, Str
"0, 0, 123.4567, '123'",
"3, 5, 123.4567, '123.4567'",
"5, 5, 123.4567, '123.45670'",
"8, 10, 123.4567, '123.45670000'"
"8, 10, 123.4567, '123.45670000'",
"2, 10, 0.0, '0.00'"
})
public void testFormatFractionDigits(int minDigits, int maxDigits, double input, String expected) {
HumanDecimalFormat format = HumanDecimalFormat.builder()
Expand All @@ -100,7 +103,8 @@ public void testFormatGrouping(double input, String expected) {
@CsvSource({
"4, 12.3456, '12.3456'",
"3, 12.3456, '~12.345'",
"0, 12.3456, '~12'"
"0, 12.3456, '~12'",
"1, 0.00001, '~0'"
})
public void testFormatApprox(int minDigits, double input, String expected) {
HumanDecimalFormat format = HumanDecimalFormat.builder()
Expand Down

0 comments on commit 049d05d

Please sign in to comment.