Skip to content

Commit

Permalink
math/big: add %x float format
Browse files Browse the repository at this point in the history
big.Float already had %p for printing hex format,
but that format normalizes differently from fmt's %x
and ignores precision entirely.

This CL adds %x to big.Float, matching fmt's behavior:
the verb is spelled 'x' not 'p', the mantissa is normalized
to [1, 2), and precision is respected.

See golang.org/design/19308-number-literals for background.

For #29008.

Change-Id: I9c1b9612107094856797e5b0b584c556c1914895
Reviewed-on: https://go-review.googlesource.com/c/160249
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
rsc committed Feb 26, 2019
1 parent 1e58bb1 commit 675503c
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 102 deletions.
229 changes: 140 additions & 89 deletions src/math/big/floatconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func TestFloat64Text(t *testing.T) {
{32, 'g', -1, "32"},
{32, 'g', 0, "3e+01"},

// {100, 'x', -1, "%x"},
{100, 'x', -1, "0x1.9p+06"},

// {math.NaN(), 'g', -1, "NaN"}, // Float doesn't support NaNs
// {-math.NaN(), 'g', -1, "NaN"}, // Float doesn't support NaNs
Expand Down Expand Up @@ -339,115 +339,166 @@ func actualPrec(x float64) uint {
}

func TestFloatText(t *testing.T) {
const defaultRound = ^RoundingMode(0)

for _, test := range []struct {
x string
round RoundingMode
prec uint
format byte
digits int
want string
}{
{"0", 10, 'f', 0, "0"},
{"-0", 10, 'f', 0, "-0"},
{"1", 10, 'f', 0, "1"},
{"-1", 10, 'f', 0, "-1"},

{"1.459", 100, 'e', 0, "1e+00"},
{"2.459", 100, 'e', 1, "2.5e+00"},
{"3.459", 100, 'e', 2, "3.46e+00"},
{"4.459", 100, 'e', 3, "4.459e+00"},
{"5.459", 100, 'e', 4, "5.4590e+00"},

{"1.459", 100, 'E', 0, "1E+00"},
{"2.459", 100, 'E', 1, "2.5E+00"},
{"3.459", 100, 'E', 2, "3.46E+00"},
{"4.459", 100, 'E', 3, "4.459E+00"},
{"5.459", 100, 'E', 4, "5.4590E+00"},

{"1.459", 100, 'f', 0, "1"},
{"2.459", 100, 'f', 1, "2.5"},
{"3.459", 100, 'f', 2, "3.46"},
{"4.459", 100, 'f', 3, "4.459"},
{"5.459", 100, 'f', 4, "5.4590"},

{"1.459", 100, 'g', 0, "1"},
{"2.459", 100, 'g', 1, "2"},
{"3.459", 100, 'g', 2, "3.5"},
{"4.459", 100, 'g', 3, "4.46"},
{"5.459", 100, 'g', 4, "5.459"},

{"1459", 53, 'g', 0, "1e+03"},
{"2459", 53, 'g', 1, "2e+03"},
{"3459", 53, 'g', 2, "3.5e+03"},
{"4459", 53, 'g', 3, "4.46e+03"},
{"5459", 53, 'g', 4, "5459"},

{"1459", 53, 'G', 0, "1E+03"},
{"2459", 53, 'G', 1, "2E+03"},
{"3459", 53, 'G', 2, "3.5E+03"},
{"4459", 53, 'G', 3, "4.46E+03"},
{"5459", 53, 'G', 4, "5459"},

{"3", 10, 'e', 40, "3.0000000000000000000000000000000000000000e+00"},
{"3", 10, 'f', 40, "3.0000000000000000000000000000000000000000"},
{"3", 10, 'g', 40, "3"},

{"3e40", 100, 'e', 40, "3.0000000000000000000000000000000000000000e+40"},
{"3e40", 100, 'f', 4, "30000000000000000000000000000000000000000.0000"},
{"3e40", 100, 'g', 40, "3e+40"},
{"0", defaultRound, 10, 'f', 0, "0"},
{"-0", defaultRound, 10, 'f', 0, "-0"},
{"1", defaultRound, 10, 'f', 0, "1"},
{"-1", defaultRound, 10, 'f', 0, "-1"},

{"1.459", defaultRound, 100, 'e', 0, "1e+00"},
{"2.459", defaultRound, 100, 'e', 1, "2.5e+00"},
{"3.459", defaultRound, 100, 'e', 2, "3.46e+00"},
{"4.459", defaultRound, 100, 'e', 3, "4.459e+00"},
{"5.459", defaultRound, 100, 'e', 4, "5.4590e+00"},

{"1.459", defaultRound, 100, 'E', 0, "1E+00"},
{"2.459", defaultRound, 100, 'E', 1, "2.5E+00"},
{"3.459", defaultRound, 100, 'E', 2, "3.46E+00"},
{"4.459", defaultRound, 100, 'E', 3, "4.459E+00"},
{"5.459", defaultRound, 100, 'E', 4, "5.4590E+00"},

{"1.459", defaultRound, 100, 'f', 0, "1"},
{"2.459", defaultRound, 100, 'f', 1, "2.5"},
{"3.459", defaultRound, 100, 'f', 2, "3.46"},
{"4.459", defaultRound, 100, 'f', 3, "4.459"},
{"5.459", defaultRound, 100, 'f', 4, "5.4590"},

{"1.459", defaultRound, 100, 'g', 0, "1"},
{"2.459", defaultRound, 100, 'g', 1, "2"},
{"3.459", defaultRound, 100, 'g', 2, "3.5"},
{"4.459", defaultRound, 100, 'g', 3, "4.46"},
{"5.459", defaultRound, 100, 'g', 4, "5.459"},

{"1459", defaultRound, 53, 'g', 0, "1e+03"},
{"2459", defaultRound, 53, 'g', 1, "2e+03"},
{"3459", defaultRound, 53, 'g', 2, "3.5e+03"},
{"4459", defaultRound, 53, 'g', 3, "4.46e+03"},
{"5459", defaultRound, 53, 'g', 4, "5459"},

{"1459", defaultRound, 53, 'G', 0, "1E+03"},
{"2459", defaultRound, 53, 'G', 1, "2E+03"},
{"3459", defaultRound, 53, 'G', 2, "3.5E+03"},
{"4459", defaultRound, 53, 'G', 3, "4.46E+03"},
{"5459", defaultRound, 53, 'G', 4, "5459"},

{"3", defaultRound, 10, 'e', 40, "3.0000000000000000000000000000000000000000e+00"},
{"3", defaultRound, 10, 'f', 40, "3.0000000000000000000000000000000000000000"},
{"3", defaultRound, 10, 'g', 40, "3"},

{"3e40", defaultRound, 100, 'e', 40, "3.0000000000000000000000000000000000000000e+40"},
{"3e40", defaultRound, 100, 'f', 4, "30000000000000000000000000000000000000000.0000"},
{"3e40", defaultRound, 100, 'g', 40, "3e+40"},

// make sure "stupid" exponents don't stall the machine
{"1e1000000", 64, 'p', 0, "0x.88b3a28a05eade3ap+3321929"},
{"1e646456992", 64, 'p', 0, "0x.e883a0c5c8c7c42ap+2147483644"},
{"1e646456993", 64, 'p', 0, "+Inf"},
{"1e1000000000", 64, 'p', 0, "+Inf"},
{"1e-1000000", 64, 'p', 0, "0x.efb4542cc8ca418ap-3321928"},
{"1e-646456993", 64, 'p', 0, "0x.e17c8956983d9d59p-2147483647"},
{"1e-646456994", 64, 'p', 0, "0"},
{"1e-1000000000", 64, 'p', 0, "0"},
{"1e1000000", defaultRound, 64, 'p', 0, "0x.88b3a28a05eade3ap+3321929"},
{"1e646456992", defaultRound, 64, 'p', 0, "0x.e883a0c5c8c7c42ap+2147483644"},
{"1e646456993", defaultRound, 64, 'p', 0, "+Inf"},
{"1e1000000000", defaultRound, 64, 'p', 0, "+Inf"},
{"1e-1000000", defaultRound, 64, 'p', 0, "0x.efb4542cc8ca418ap-3321928"},
{"1e-646456993", defaultRound, 64, 'p', 0, "0x.e17c8956983d9d59p-2147483647"},
{"1e-646456994", defaultRound, 64, 'p', 0, "0"},
{"1e-1000000000", defaultRound, 64, 'p', 0, "0"},

// minimum and maximum values
{"1p2147483646", 64, 'p', 0, "0x.8p+2147483647"},
{"0x.8p2147483647", 64, 'p', 0, "0x.8p+2147483647"},
{"0x.8p-2147483647", 64, 'p', 0, "0x.8p-2147483647"},
{"1p-2147483649", 64, 'p', 0, "0x.8p-2147483648"},
{"1p2147483646", defaultRound, 64, 'p', 0, "0x.8p+2147483647"},
{"0x.8p2147483647", defaultRound, 64, 'p', 0, "0x.8p+2147483647"},
{"0x.8p-2147483647", defaultRound, 64, 'p', 0, "0x.8p-2147483647"},
{"1p-2147483649", defaultRound, 64, 'p', 0, "0x.8p-2147483648"},

// TODO(gri) need tests for actual large Floats

{"0", 53, 'b', 0, "0"},
{"-0", 53, 'b', 0, "-0"},
{"1.0", 53, 'b', 0, "4503599627370496p-52"},
{"-1.0", 53, 'b', 0, "-4503599627370496p-52"},
{"4503599627370496", 53, 'b', 0, "4503599627370496p+0"},
{"0", defaultRound, 53, 'b', 0, "0"},
{"-0", defaultRound, 53, 'b', 0, "-0"},
{"1.0", defaultRound, 53, 'b', 0, "4503599627370496p-52"},
{"-1.0", defaultRound, 53, 'b', 0, "-4503599627370496p-52"},
{"4503599627370496", defaultRound, 53, 'b', 0, "4503599627370496p+0"},

// issue 9939
{"3", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"03", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"3.", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"3.0", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"3.00", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"3.000", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},

{"3", 350, 'p', 0, "0x.cp+2"},
{"03", 350, 'p', 0, "0x.cp+2"},
{"3.", 350, 'p', 0, "0x.cp+2"},
{"3.0", 350, 'p', 0, "0x.cp+2"},
{"3.00", 350, 'p', 0, "0x.cp+2"},
{"3.000", 350, 'p', 0, "0x.cp+2"},

{"0", 64, 'p', 0, "0"},
{"-0", 64, 'p', 0, "-0"},
{"1024.0", 64, 'p', 0, "0x.8p+11"},
{"-1024.0", 64, 'p', 0, "-0x.8p+11"},

// unsupported format
//{"3.14", 64, 'x', 0, "%x"},
//{"-3.14", 64, 'x', 0, "%x"},
{"3", defaultRound, 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"03", defaultRound, 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"3.", defaultRound, 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"3.0", defaultRound, 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"3.00", defaultRound, 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
{"3.000", defaultRound, 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},

{"3", defaultRound, 350, 'p', 0, "0x.cp+2"},
{"03", defaultRound, 350, 'p', 0, "0x.cp+2"},
{"3.", defaultRound, 350, 'p', 0, "0x.cp+2"},
{"3.0", defaultRound, 350, 'p', 0, "0x.cp+2"},
{"3.00", defaultRound, 350, 'p', 0, "0x.cp+2"},
{"3.000", defaultRound, 350, 'p', 0, "0x.cp+2"},

{"0", defaultRound, 64, 'p', 0, "0"},
{"-0", defaultRound, 64, 'p', 0, "-0"},
{"1024.0", defaultRound, 64, 'p', 0, "0x.8p+11"},
{"-1024.0", defaultRound, 64, 'p', 0, "-0x.8p+11"},

{"0", defaultRound, 64, 'x', -1, "0x0p+00"},
{"0", defaultRound, 64, 'x', 0, "0x0p+00"},
{"0", defaultRound, 64, 'x', 1, "0x0.0p+00"},
{"0", defaultRound, 64, 'x', 5, "0x0.00000p+00"},
{"3.25", defaultRound, 64, 'x', 0, "0x1p+02"},
{"-3.25", defaultRound, 64, 'x', 0, "-0x1p+02"},
{"3.25", defaultRound, 64, 'x', 1, "0x1.ap+01"},
{"-3.25", defaultRound, 64, 'x', 1, "-0x1.ap+01"},
{"3.25", defaultRound, 64, 'x', -1, "0x1.ap+01"},
{"-3.25", defaultRound, 64, 'x', -1, "-0x1.ap+01"},
{"1024.0", defaultRound, 64, 'x', 0, "0x1p+10"},
{"-1024.0", defaultRound, 64, 'x', 0, "-0x1p+10"},
{"1024.0", defaultRound, 64, 'x', 5, "0x1.00000p+10"},
{"8191.0", defaultRound, 53, 'x', -1, "0x1.fffp+12"},
{"8191.5", defaultRound, 53, 'x', -1, "0x1.fff8p+12"},
{"8191.53125", defaultRound, 53, 'x', -1, "0x1.fff88p+12"},
{"8191.53125", defaultRound, 53, 'x', 4, "0x1.fff8p+12"},
{"8191.53125", defaultRound, 53, 'x', 3, "0x1.000p+13"},
{"8191.53125", defaultRound, 53, 'x', 0, "0x1p+13"},
{"8191.533203125", defaultRound, 53, 'x', -1, "0x1.fff888p+12"},
{"8191.533203125", defaultRound, 53, 'x', 5, "0x1.fff88p+12"},
{"8191.533203125", defaultRound, 53, 'x', 4, "0x1.fff9p+12"},

{"8191.53125", defaultRound, 53, 'x', -1, "0x1.fff88p+12"},
{"8191.53125", ToNearestEven, 53, 'x', 5, "0x1.fff88p+12"},
{"8191.53125", ToNearestAway, 53, 'x', 5, "0x1.fff88p+12"},
{"8191.53125", ToZero, 53, 'x', 5, "0x1.fff88p+12"},
{"8191.53125", AwayFromZero, 53, 'x', 5, "0x1.fff88p+12"},
{"8191.53125", ToNegativeInf, 53, 'x', 5, "0x1.fff88p+12"},
{"8191.53125", ToPositiveInf, 53, 'x', 5, "0x1.fff88p+12"},

{"8191.53125", defaultRound, 53, 'x', 4, "0x1.fff8p+12"},
{"8191.53125", defaultRound, 53, 'x', 3, "0x1.000p+13"},
{"8191.53125", defaultRound, 53, 'x', 0, "0x1p+13"},
{"8191.533203125", defaultRound, 53, 'x', -1, "0x1.fff888p+12"},
{"8191.533203125", defaultRound, 53, 'x', 6, "0x1.fff888p+12"},
{"8191.533203125", defaultRound, 53, 'x', 5, "0x1.fff88p+12"},
{"8191.533203125", defaultRound, 53, 'x', 4, "0x1.fff9p+12"},

{"8191.53125", ToNearestEven, 53, 'x', 4, "0x1.fff8p+12"},
{"8191.53125", ToNearestAway, 53, 'x', 4, "0x1.fff9p+12"},
{"8191.53125", ToZero, 53, 'x', 4, "0x1.fff8p+12"},
{"8191.53125", ToZero, 53, 'x', 2, "0x1.ffp+12"},
{"8191.53125", AwayFromZero, 53, 'x', 4, "0x1.fff9p+12"},
{"8191.53125", ToNegativeInf, 53, 'x', 4, "0x1.fff8p+12"},
{"-8191.53125", ToNegativeInf, 53, 'x', 4, "-0x1.fff9p+12"},
{"8191.53125", ToPositiveInf, 53, 'x', 4, "0x1.fff9p+12"},
{"-8191.53125", ToPositiveInf, 53, 'x', 4, "-0x1.fff8p+12"},
} {
f, _, err := ParseFloat(test.x, 0, test.prec, ToNearestEven)
if err != nil {
t.Errorf("%v: %s", test, err)
continue
}
if test.round != defaultRound {
f.SetMode(test.round)
}

got := f.Text(test.format, test.digits)
if got != test.want {
Expand All @@ -458,7 +509,7 @@ func TestFloatText(t *testing.T) {
// ('p' format is not supported by strconv.FormatFloat,
// and its output for 0.0 prints a biased exponent value
// as in 0p-1074 which makes no sense to emulate here)
if test.prec == 53 && test.format != 'p' && f.Sign() != 0 {
if test.prec == 53 && test.format != 'p' && f.Sign() != 0 && (test.round == ToNearestEven || test.round == defaultRound) {
f64, acc := f.Float64()
if acc != Exact {
t.Errorf("%v: expected exact conversion to float64", test)
Expand Down
Loading

0 comments on commit 675503c

Please sign in to comment.