提交 3eb9388e 编写于 作者: B bpb

6469160: (fmt) general (%g) formatting of zero (0.0) with precision 0 or 1...

6469160: (fmt) general (%g) formatting of zero (0.0) with precision 0 or 1 throws ArrayOutOfBoundsException
Summary: For zero value ensure than an unpadded zero character is passed to Formatter.addZeros()
Reviewed-by: iris, darcy
Contributed-by: NBrian Burkhalter <brian.burkhalter@oracle.com>
上级 5d472edf
...@@ -3297,18 +3297,29 @@ public final class Formatter implements Closeable, Flushable { ...@@ -3297,18 +3297,29 @@ public final class Formatter implements Closeable, Flushable {
else if (precision == 0) else if (precision == 0)
prec = 1; prec = 1;
FormattedFloatingDecimal fd char[] exp;
char[] mant;
int expRounded;
if (value == 0.0) {
exp = null;
mant = new char[] {'0'};
expRounded = 0;
} else {
FormattedFloatingDecimal fd
= FormattedFloatingDecimal.valueOf(value, prec, = FormattedFloatingDecimal.valueOf(value, prec,
FormattedFloatingDecimal.Form.GENERAL); FormattedFloatingDecimal.Form.GENERAL);
exp = fd.getExponent();
mant = fd.getMantissa();
expRounded = fd.getExponentRounded();
}
char[] exp = fd.getExponent();
if (exp != null) { if (exp != null) {
prec -= 1; prec -= 1;
} else { } else {
prec = prec - (value == 0 ? 0 : fd.getExponentRounded()) - 1; prec -= expRounded + 1;
} }
char[] mant = addZeros(fd.getMantissa(), prec); mant = addZeros(mant, prec);
// If the precision is zero and the '#' flag is set, add the // If the precision is zero and the '#' flag is set, add the
// requested decimal point. // requested decimal point.
if (f.contains(Flags.ALTERNATE) && (prec == 0)) if (f.contains(Flags.ALTERNATE) && (prec == 0))
......
...@@ -1045,7 +1045,7 @@ public class FloatingDecimal{ ...@@ -1045,7 +1045,7 @@ public class FloatingDecimal{
this.nDigits = n; this.nDigits = n;
} }
/* /**
* Takes a FloatingDecimal, which we presumably just scanned in, * Takes a FloatingDecimal, which we presumably just scanned in,
* and finds out what its value is, as a double. * and finds out what its value is, as a double.
* *
......
...@@ -1177,6 +1177,13 @@ public class Basic$Type$ extends Basic { ...@@ -1177,6 +1177,13 @@ public class Basic$Type$ extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099); test("%3.0g", "1e-05", 0.0000099);
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
* @summary Unit test for formatter * @summary Unit test for formatter
* @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937 * @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937
* 5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122 * 5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122
* 6344623 6369500 6534606 6282094 6286592 6476425 5063507 * 6344623 6369500 6534606 6282094 6286592 6476425 5063507 6469160
* *
* @run shell/timeout=240 Basic.sh * @run shell/timeout=240 Basic.sh
*/ */
......
...@@ -1177,6 +1177,13 @@ public class BasicBigDecimal extends Basic { ...@@ -1177,6 +1177,13 @@ public class BasicBigDecimal extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099); test("%3.0g", "1e-05", 0.0000099);
......
...@@ -1113,6 +1113,15 @@ public class BasicDouble extends Basic { ...@@ -1113,6 +1113,15 @@ public class BasicDouble extends Basic {
...@@ -1168,6 +1177,13 @@ public class BasicDouble extends Basic { ...@@ -1168,6 +1177,13 @@ public class BasicDouble extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099); test("%3.0g", "1e-05", 0.0000099);
......
...@@ -1113,6 +1113,15 @@ public class BasicDoubleObject extends Basic { ...@@ -1113,6 +1113,15 @@ public class BasicDoubleObject extends Basic {
...@@ -1168,6 +1177,13 @@ public class BasicDoubleObject extends Basic { ...@@ -1168,6 +1177,13 @@ public class BasicDoubleObject extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099); test("%3.0g", "1e-05", 0.0000099);
......
...@@ -1096,6 +1096,15 @@ public class BasicFloat extends Basic { ...@@ -1096,6 +1096,15 @@ public class BasicFloat extends Basic {
...@@ -1168,6 +1177,13 @@ public class BasicFloat extends Basic { ...@@ -1168,6 +1177,13 @@ public class BasicFloat extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099); test("%3.0g", "1e-05", 0.0000099);
......
...@@ -1129,6 +1129,15 @@ public class BasicFloatObject extends Basic { ...@@ -1129,6 +1129,15 @@ public class BasicFloatObject extends Basic {
...@@ -1168,6 +1177,13 @@ public class BasicFloatObject extends Basic { ...@@ -1168,6 +1177,13 @@ public class BasicFloatObject extends Basic {
test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "1.00E-05", recip(create(100000.0)));
test("%.3G", "-1.00E-05", recip(create(-100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0)));
test("%.1g", "-0", -0.0);
test("%3.0g", " -0", -0.0);
test("%.1g", "0", 0.0);
test("%3.0g", " 0", 0.0);
test("%.1g", "0", +0.0);
test("%3.0g", " 0", +0.0);
test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-06", 0.000001);
test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.00001);
test("%3.0g", "1e-05", 0.0000099); test("%3.0g", "1e-05", 0.0000099);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册