github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/src/math/big/floatconv_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package big 6 7 import ( 8 "fmt" 9 "math" 10 "strconv" 11 "testing" 12 ) 13 14 func TestFloatSetFloat64String(t *testing.T) { 15 inf := math.Inf(0) 16 nan := math.NaN() 17 18 for _, test := range []struct { 19 s string 20 x float64 // NaNs represent invalid inputs 21 }{ 22 // basics 23 {"0", 0}, 24 {"-0", -0}, 25 {"+0", 0}, 26 {"1", 1}, 27 {"-1", -1}, 28 {"+1", 1}, 29 {"1.234", 1.234}, 30 {"-1.234", -1.234}, 31 {"+1.234", 1.234}, 32 {".1", 0.1}, 33 {"1.", 1}, 34 {"+1.", 1}, 35 36 // various zeros 37 {"0e100", 0}, 38 {"-0e+100", 0}, 39 {"+0e-100", 0}, 40 {"0E100", 0}, 41 {"-0E+100", 0}, 42 {"+0E-100", 0}, 43 44 // various decimal exponent formats 45 {"1.e10", 1e10}, 46 {"1e+10", 1e10}, 47 {"+1e-10", 1e-10}, 48 {"1E10", 1e10}, 49 {"1.E+10", 1e10}, 50 {"+1E-10", 1e-10}, 51 52 // infinities 53 {"Inf", inf}, 54 {"+Inf", inf}, 55 {"-Inf", -inf}, 56 {"inf", inf}, 57 {"+inf", inf}, 58 {"-inf", -inf}, 59 60 // invalid numbers 61 {"", nan}, 62 {"-", nan}, 63 {"0x", nan}, 64 {"0e", nan}, 65 {"1.2ef", nan}, 66 {"2..3", nan}, 67 {"123..", nan}, 68 {"infinity", nan}, 69 {"foobar", nan}, 70 71 // misc decimal values 72 {"3.14159265", 3.14159265}, 73 {"-687436.79457e-245", -687436.79457e-245}, 74 {"-687436.79457E245", -687436.79457e245}, 75 {".0000000000000000000000000000000000000001", 1e-40}, 76 {"+10000000000000000000000000000000000000000e-0", 1e40}, 77 78 // decimal mantissa, binary exponent 79 {"0p0", 0}, 80 {"-0p0", -0}, 81 {"1p10", 1 << 10}, 82 {"1p+10", 1 << 10}, 83 {"+1p-10", 1.0 / (1 << 10)}, 84 {"1024p-12", 0.25}, 85 {"-1p10", -1024}, 86 {"1.5p1", 3}, 87 88 // binary mantissa, decimal exponent 89 {"0b0", 0}, 90 {"-0b0", -0}, 91 {"0b0e+10", 0}, 92 {"-0b0e-10", -0}, 93 {"0b1010", 10}, 94 {"0B1010E2", 1000}, 95 {"0b.1", 0.5}, 96 {"0b.001", 0.125}, 97 {"0b.001e3", 125}, 98 99 // binary mantissa, binary exponent 100 {"0b0p+10", 0}, 101 {"-0b0p-10", -0}, 102 {"0b.1010p4", 10}, 103 {"0b1p-1", 0.5}, 104 {"0b001p-3", 0.125}, 105 {"0b.001p3", 1}, 106 {"0b0.01p2", 1}, 107 108 // hexadecimal mantissa and exponent 109 {"0x0", 0}, 110 {"-0x0", -0}, 111 {"0x0p+10", 0}, 112 {"-0x0p-10", -0}, 113 {"0xff", 255}, 114 {"0X.8p1", 1}, 115 {"-0X0.00008p16", -0.5}, 116 {"0x0.0000000000001p-1022", math.SmallestNonzeroFloat64}, 117 {"0x1.fffffffffffffp1023", math.MaxFloat64}, 118 } { 119 var x Float 120 x.SetPrec(53) 121 _, ok := x.SetString(test.s) 122 if math.IsNaN(test.x) { 123 // test.s is invalid 124 if ok { 125 t.Errorf("%s: want parse error", test.s) 126 } 127 continue 128 } 129 // test.s is valid 130 if !ok { 131 t.Errorf("%s: got parse error", test.s) 132 continue 133 } 134 f, _ := x.Float64() 135 want := new(Float).SetFloat64(test.x) 136 if x.Cmp(want) != 0 { 137 t.Errorf("%s: got %s (%v); want %v", test.s, &x, f, test.x) 138 } 139 } 140 } 141 142 func fdiv(a, b float64) float64 { return a / b } 143 144 const ( 145 below1e23 = 99999999999999974834176 146 above1e23 = 100000000000000008388608 147 ) 148 149 func TestFloat64Text(t *testing.T) { 150 for _, test := range []struct { 151 x float64 152 format byte 153 prec int 154 want string 155 }{ 156 {0, 'f', 0, "0"}, 157 {math.Copysign(0, -1), 'f', 0, "-0"}, 158 {1, 'f', 0, "1"}, 159 {-1, 'f', 0, "-1"}, 160 161 {0.001, 'e', 0, "1e-03"}, 162 {0.459, 'e', 0, "5e-01"}, 163 {1.459, 'e', 0, "1e+00"}, 164 {2.459, 'e', 1, "2.5e+00"}, 165 {3.459, 'e', 2, "3.46e+00"}, 166 {4.459, 'e', 3, "4.459e+00"}, 167 {5.459, 'e', 4, "5.4590e+00"}, 168 169 {0.001, 'f', 0, "0"}, 170 {0.459, 'f', 0, "0"}, 171 {1.459, 'f', 0, "1"}, 172 {2.459, 'f', 1, "2.5"}, 173 {3.459, 'f', 2, "3.46"}, 174 {4.459, 'f', 3, "4.459"}, 175 {5.459, 'f', 4, "5.4590"}, 176 177 {0, 'b', 0, "0"}, 178 {math.Copysign(0, -1), 'b', 0, "-0"}, 179 {1.0, 'b', 0, "4503599627370496p-52"}, 180 {-1.0, 'b', 0, "-4503599627370496p-52"}, 181 {4503599627370496, 'b', 0, "4503599627370496p+0"}, 182 183 {0, 'p', 0, "0"}, 184 {math.Copysign(0, -1), 'p', 0, "-0"}, 185 {1024.0, 'p', 0, "0x.8p+11"}, 186 {-1024.0, 'p', 0, "-0x.8p+11"}, 187 188 // all test cases below from strconv/ftoa_test.go 189 {1, 'e', 5, "1.00000e+00"}, 190 {1, 'f', 5, "1.00000"}, 191 {1, 'g', 5, "1"}, 192 {1, 'g', -1, "1"}, 193 {20, 'g', -1, "20"}, 194 {1234567.8, 'g', -1, "1.2345678e+06"}, 195 {200000, 'g', -1, "200000"}, 196 {2000000, 'g', -1, "2e+06"}, 197 198 // g conversion and zero suppression 199 {400, 'g', 2, "4e+02"}, 200 {40, 'g', 2, "40"}, 201 {4, 'g', 2, "4"}, 202 {.4, 'g', 2, "0.4"}, 203 {.04, 'g', 2, "0.04"}, 204 {.004, 'g', 2, "0.004"}, 205 {.0004, 'g', 2, "0.0004"}, 206 {.00004, 'g', 2, "4e-05"}, 207 {.000004, 'g', 2, "4e-06"}, 208 209 {0, 'e', 5, "0.00000e+00"}, 210 {0, 'f', 5, "0.00000"}, 211 {0, 'g', 5, "0"}, 212 {0, 'g', -1, "0"}, 213 214 {-1, 'e', 5, "-1.00000e+00"}, 215 {-1, 'f', 5, "-1.00000"}, 216 {-1, 'g', 5, "-1"}, 217 {-1, 'g', -1, "-1"}, 218 219 {12, 'e', 5, "1.20000e+01"}, 220 {12, 'f', 5, "12.00000"}, 221 {12, 'g', 5, "12"}, 222 {12, 'g', -1, "12"}, 223 224 {123456700, 'e', 5, "1.23457e+08"}, 225 {123456700, 'f', 5, "123456700.00000"}, 226 {123456700, 'g', 5, "1.2346e+08"}, 227 {123456700, 'g', -1, "1.234567e+08"}, 228 229 {1.2345e6, 'e', 5, "1.23450e+06"}, 230 {1.2345e6, 'f', 5, "1234500.00000"}, 231 {1.2345e6, 'g', 5, "1.2345e+06"}, 232 233 {1e23, 'e', 17, "9.99999999999999916e+22"}, 234 {1e23, 'f', 17, "99999999999999991611392.00000000000000000"}, 235 {1e23, 'g', 17, "9.9999999999999992e+22"}, 236 237 {1e23, 'e', -1, "1e+23"}, 238 {1e23, 'f', -1, "100000000000000000000000"}, 239 {1e23, 'g', -1, "1e+23"}, 240 241 {below1e23, 'e', 17, "9.99999999999999748e+22"}, 242 {below1e23, 'f', 17, "99999999999999974834176.00000000000000000"}, 243 {below1e23, 'g', 17, "9.9999999999999975e+22"}, 244 245 {below1e23, 'e', -1, "9.999999999999997e+22"}, 246 {below1e23, 'f', -1, "99999999999999970000000"}, 247 {below1e23, 'g', -1, "9.999999999999997e+22"}, 248 249 {above1e23, 'e', 17, "1.00000000000000008e+23"}, 250 {above1e23, 'f', 17, "100000000000000008388608.00000000000000000"}, 251 {above1e23, 'g', 17, "1.0000000000000001e+23"}, 252 253 {above1e23, 'e', -1, "1.0000000000000001e+23"}, 254 {above1e23, 'f', -1, "100000000000000010000000"}, 255 {above1e23, 'g', -1, "1.0000000000000001e+23"}, 256 257 {5e-304 / 1e20, 'g', -1, "5e-324"}, 258 {-5e-304 / 1e20, 'g', -1, "-5e-324"}, 259 {fdiv(5e-304, 1e20), 'g', -1, "5e-324"}, // avoid constant arithmetic 260 {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, // avoid constant arithmetic 261 262 {32, 'g', -1, "32"}, 263 {32, 'g', 0, "3e+01"}, 264 265 {100, 'x', -1, "%x"}, 266 267 // {math.NaN(), 'g', -1, "NaN"}, // Float doesn't support NaNs 268 // {-math.NaN(), 'g', -1, "NaN"}, // Float doesn't support NaNs 269 {math.Inf(0), 'g', -1, "+Inf"}, 270 {math.Inf(-1), 'g', -1, "-Inf"}, 271 {-math.Inf(0), 'g', -1, "-Inf"}, 272 273 {-1, 'b', -1, "-4503599627370496p-52"}, 274 275 // fixed bugs 276 {0.9, 'f', 1, "0.9"}, 277 {0.09, 'f', 1, "0.1"}, 278 {0.0999, 'f', 1, "0.1"}, 279 {0.05, 'f', 1, "0.1"}, 280 {0.05, 'f', 0, "0"}, 281 {0.5, 'f', 1, "0.5"}, 282 {0.5, 'f', 0, "0"}, 283 {1.5, 'f', 0, "2"}, 284 285 // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ 286 {2.2250738585072012e-308, 'g', -1, "2.2250738585072014e-308"}, 287 // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ 288 {2.2250738585072011e-308, 'g', -1, "2.225073858507201e-308"}, 289 290 // Issue 2625. 291 {383260575764816448, 'f', 0, "383260575764816448"}, 292 {383260575764816448, 'g', -1, "3.8326057576481645e+17"}, 293 } { 294 // The test cases are from the strconv package which tests float64 values. 295 // When formatting values with prec = -1 (shortest representation), 296 // the actually available mantissa precision matters. 297 // For denormalized values, that precision is < 53 (SetFloat64 default). 298 // Compute and set the actual precision explicitly. 299 f := new(Float).SetPrec(actualPrec(test.x)).SetFloat64(test.x) 300 got := f.Text(test.format, test.prec) 301 if got != test.want { 302 t.Errorf("%v: got %s; want %s", test, got, test.want) 303 continue 304 } 305 306 if test.format == 'b' && test.x == 0 { 307 continue // 'b' format in strconv.Float requires knowledge of bias for 0.0 308 } 309 if test.format == 'p' { 310 continue // 'p' format not supported in strconv.Format 311 } 312 313 // verify that Float format matches strconv format 314 want := strconv.FormatFloat(test.x, test.format, test.prec, 64) 315 if got != want { 316 t.Errorf("%v: got %s; want %s (strconv)", test, got, want) 317 } 318 } 319 } 320 321 // actualPrec returns the number of actually used mantissa bits. 322 func actualPrec(x float64) uint { 323 if bits := math.Float64bits(x); x != 0 && bits&(0x7ff<<52) == 0 { 324 // x is denormalized 325 return 64 - nlz64(bits&(1<<52-1)) 326 } 327 return 53 328 } 329 330 func TestFloatText(t *testing.T) { 331 for _, test := range []struct { 332 x string 333 prec uint 334 format byte 335 digits int 336 want string 337 }{ 338 {"0", 10, 'f', 0, "0"}, 339 {"-0", 10, 'f', 0, "-0"}, 340 {"1", 10, 'f', 0, "1"}, 341 {"-1", 10, 'f', 0, "-1"}, 342 343 {"1.459", 100, 'e', 0, "1e+00"}, 344 {"2.459", 100, 'e', 1, "2.5e+00"}, 345 {"3.459", 100, 'e', 2, "3.46e+00"}, 346 {"4.459", 100, 'e', 3, "4.459e+00"}, 347 {"5.459", 100, 'e', 4, "5.4590e+00"}, 348 349 {"1.459", 100, 'E', 0, "1E+00"}, 350 {"2.459", 100, 'E', 1, "2.5E+00"}, 351 {"3.459", 100, 'E', 2, "3.46E+00"}, 352 {"4.459", 100, 'E', 3, "4.459E+00"}, 353 {"5.459", 100, 'E', 4, "5.4590E+00"}, 354 355 {"1.459", 100, 'f', 0, "1"}, 356 {"2.459", 100, 'f', 1, "2.5"}, 357 {"3.459", 100, 'f', 2, "3.46"}, 358 {"4.459", 100, 'f', 3, "4.459"}, 359 {"5.459", 100, 'f', 4, "5.4590"}, 360 361 {"1.459", 100, 'g', 0, "1"}, 362 {"2.459", 100, 'g', 1, "2"}, 363 {"3.459", 100, 'g', 2, "3.5"}, 364 {"4.459", 100, 'g', 3, "4.46"}, 365 {"5.459", 100, 'g', 4, "5.459"}, 366 367 {"1459", 53, 'g', 0, "1e+03"}, 368 {"2459", 53, 'g', 1, "2e+03"}, 369 {"3459", 53, 'g', 2, "3.5e+03"}, 370 {"4459", 53, 'g', 3, "4.46e+03"}, 371 {"5459", 53, 'g', 4, "5459"}, 372 373 {"1459", 53, 'G', 0, "1E+03"}, 374 {"2459", 53, 'G', 1, "2E+03"}, 375 {"3459", 53, 'G', 2, "3.5E+03"}, 376 {"4459", 53, 'G', 3, "4.46E+03"}, 377 {"5459", 53, 'G', 4, "5459"}, 378 379 {"3", 10, 'e', 40, "3.0000000000000000000000000000000000000000e+00"}, 380 {"3", 10, 'f', 40, "3.0000000000000000000000000000000000000000"}, 381 {"3", 10, 'g', 40, "3"}, 382 383 {"3e40", 100, 'e', 40, "3.0000000000000000000000000000000000000000e+40"}, 384 {"3e40", 100, 'f', 4, "30000000000000000000000000000000000000000.0000"}, 385 {"3e40", 100, 'g', 40, "3e+40"}, 386 387 // make sure "stupid" exponents don't stall the machine 388 {"1e1000000", 64, 'p', 0, "0x.88b3a28a05eade3ap+3321929"}, 389 {"1e646456992", 64, 'p', 0, "0x.e883a0c5c8c7c42ap+2147483644"}, 390 {"1e646456993", 64, 'p', 0, "+Inf"}, 391 {"1e1000000000", 64, 'p', 0, "+Inf"}, 392 {"1e-1000000", 64, 'p', 0, "0x.efb4542cc8ca418ap-3321928"}, 393 {"1e-646456993", 64, 'p', 0, "0x.e17c8956983d9d59p-2147483647"}, 394 {"1e-646456994", 64, 'p', 0, "0"}, 395 {"1e-1000000000", 64, 'p', 0, "0"}, 396 397 // minimum and maximum values 398 {"1p2147483646", 64, 'p', 0, "0x.8p+2147483647"}, 399 {"0x.8p2147483647", 64, 'p', 0, "0x.8p+2147483647"}, 400 {"0x.8p-2147483647", 64, 'p', 0, "0x.8p-2147483647"}, 401 {"1p-2147483649", 64, 'p', 0, "0x.8p-2147483648"}, 402 403 // TODO(gri) need tests for actual large Floats 404 405 {"0", 53, 'b', 0, "0"}, 406 {"-0", 53, 'b', 0, "-0"}, 407 {"1.0", 53, 'b', 0, "4503599627370496p-52"}, 408 {"-1.0", 53, 'b', 0, "-4503599627370496p-52"}, 409 {"4503599627370496", 53, 'b', 0, "4503599627370496p+0"}, 410 411 // issue 9939 412 {"3", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"}, 413 {"03", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"}, 414 {"3.", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"}, 415 {"3.0", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"}, 416 {"3.00", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"}, 417 {"3.000", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"}, 418 419 {"3", 350, 'p', 0, "0x.cp+2"}, 420 {"03", 350, 'p', 0, "0x.cp+2"}, 421 {"3.", 350, 'p', 0, "0x.cp+2"}, 422 {"3.0", 350, 'p', 0, "0x.cp+2"}, 423 {"3.00", 350, 'p', 0, "0x.cp+2"}, 424 {"3.000", 350, 'p', 0, "0x.cp+2"}, 425 426 {"0", 64, 'p', 0, "0"}, 427 {"-0", 64, 'p', 0, "-0"}, 428 {"1024.0", 64, 'p', 0, "0x.8p+11"}, 429 {"-1024.0", 64, 'p', 0, "-0x.8p+11"}, 430 431 // unsupported format 432 {"3.14", 64, 'x', 0, "%x"}, 433 {"-3.14", 64, 'x', 0, "%x"}, 434 } { 435 f, _, err := ParseFloat(test.x, 0, test.prec, ToNearestEven) 436 if err != nil { 437 t.Errorf("%v: %s", test, err) 438 continue 439 } 440 441 got := f.Text(test.format, test.digits) 442 if got != test.want { 443 t.Errorf("%v: got %s; want %s", test, got, test.want) 444 } 445 446 // compare with strconv.FormatFloat output if possible 447 // ('p' format is not supported by strconv.FormatFloat, 448 // and its output for 0.0 prints a biased exponent value 449 // as in 0p-1074 which makes no sense to emulate here) 450 if test.prec == 53 && test.format != 'p' && f.Sign() != 0 { 451 f64, acc := f.Float64() 452 if acc != Exact { 453 t.Errorf("%v: expected exact conversion to float64", test) 454 continue 455 } 456 got := strconv.FormatFloat(f64, test.format, test.digits, 64) 457 if got != test.want { 458 t.Errorf("%v: got %s; want %s", test, got, test.want) 459 } 460 } 461 } 462 } 463 464 func TestFloatFormat(t *testing.T) { 465 for _, test := range []struct { 466 format string 467 value interface{} // float32, float64, or string (== 512bit *Float) 468 want string 469 }{ 470 // from fmt/fmt_test.go 471 {"%+.3e", 0.0, "+0.000e+00"}, 472 {"%+.3e", 1.0, "+1.000e+00"}, 473 {"%+.3f", -1.0, "-1.000"}, 474 {"%+.3F", -1.0, "-1.000"}, 475 {"%+.3F", float32(-1.0), "-1.000"}, 476 {"%+07.2f", 1.0, "+001.00"}, 477 {"%+07.2f", -1.0, "-001.00"}, 478 {"%+10.2f", +1.0, " +1.00"}, 479 {"%+10.2f", -1.0, " -1.00"}, 480 {"% .3E", -1.0, "-1.000E+00"}, 481 {"% .3e", 1.0, " 1.000e+00"}, 482 {"%+.3g", 0.0, "+0"}, 483 {"%+.3g", 1.0, "+1"}, 484 {"%+.3g", -1.0, "-1"}, 485 {"% .3g", -1.0, "-1"}, 486 {"% .3g", 1.0, " 1"}, 487 {"%b", float32(1.0), "8388608p-23"}, 488 {"%b", 1.0, "4503599627370496p-52"}, 489 490 // from fmt/fmt_test.go: old test/fmt_test.go 491 {"%e", 1.0, "1.000000e+00"}, 492 {"%e", 1234.5678e3, "1.234568e+06"}, 493 {"%e", 1234.5678e-8, "1.234568e-05"}, 494 {"%e", -7.0, "-7.000000e+00"}, 495 {"%e", -1e-9, "-1.000000e-09"}, 496 {"%f", 1234.5678e3, "1234567.800000"}, 497 {"%f", 1234.5678e-8, "0.000012"}, 498 {"%f", -7.0, "-7.000000"}, 499 {"%f", -1e-9, "-0.000000"}, 500 {"%g", 1234.5678e3, "1.2345678e+06"}, 501 {"%g", float32(1234.5678e3), "1.2345678e+06"}, 502 {"%g", 1234.5678e-8, "1.2345678e-05"}, 503 {"%g", -7.0, "-7"}, 504 {"%g", -1e-9, "-1e-09"}, 505 {"%g", float32(-1e-9), "-1e-09"}, 506 {"%E", 1.0, "1.000000E+00"}, 507 {"%E", 1234.5678e3, "1.234568E+06"}, 508 {"%E", 1234.5678e-8, "1.234568E-05"}, 509 {"%E", -7.0, "-7.000000E+00"}, 510 {"%E", -1e-9, "-1.000000E-09"}, 511 {"%G", 1234.5678e3, "1.2345678E+06"}, 512 {"%G", float32(1234.5678e3), "1.2345678E+06"}, 513 {"%G", 1234.5678e-8, "1.2345678E-05"}, 514 {"%G", -7.0, "-7"}, 515 {"%G", -1e-9, "-1E-09"}, 516 {"%G", float32(-1e-9), "-1E-09"}, 517 518 {"%20.6e", 1.2345e3, " 1.234500e+03"}, 519 {"%20.6e", 1.2345e-3, " 1.234500e-03"}, 520 {"%20e", 1.2345e3, " 1.234500e+03"}, 521 {"%20e", 1.2345e-3, " 1.234500e-03"}, 522 {"%20.8e", 1.2345e3, " 1.23450000e+03"}, 523 {"%20f", 1.23456789e3, " 1234.567890"}, 524 {"%20f", 1.23456789e-3, " 0.001235"}, 525 {"%20f", 12345678901.23456789, " 12345678901.234568"}, 526 {"%-20f", 1.23456789e3, "1234.567890 "}, 527 {"%20.8f", 1.23456789e3, " 1234.56789000"}, 528 {"%20.8f", 1.23456789e-3, " 0.00123457"}, 529 {"%g", 1.23456789e3, "1234.56789"}, 530 {"%g", 1.23456789e-3, "0.00123456789"}, 531 {"%g", 1.23456789e20, "1.23456789e+20"}, 532 {"%20e", math.Inf(1), " +Inf"}, 533 {"%-20f", math.Inf(-1), "-Inf "}, 534 535 // from fmt/fmt_test.go: comparison of padding rules with C printf 536 {"%.2f", 1.0, "1.00"}, 537 {"%.2f", -1.0, "-1.00"}, 538 {"% .2f", 1.0, " 1.00"}, 539 {"% .2f", -1.0, "-1.00"}, 540 {"%+.2f", 1.0, "+1.00"}, 541 {"%+.2f", -1.0, "-1.00"}, 542 {"%7.2f", 1.0, " 1.00"}, 543 {"%7.2f", -1.0, " -1.00"}, 544 {"% 7.2f", 1.0, " 1.00"}, 545 {"% 7.2f", -1.0, " -1.00"}, 546 {"%+7.2f", 1.0, " +1.00"}, 547 {"%+7.2f", -1.0, " -1.00"}, 548 {"%07.2f", 1.0, "0001.00"}, 549 {"%07.2f", -1.0, "-001.00"}, 550 {"% 07.2f", 1.0, " 001.00"}, 551 {"% 07.2f", -1.0, "-001.00"}, 552 {"%+07.2f", 1.0, "+001.00"}, 553 {"%+07.2f", -1.0, "-001.00"}, 554 555 // from fmt/fmt_test.go: zero padding does not apply to infinities 556 {"%020f", math.Inf(-1), " -Inf"}, 557 {"%020f", math.Inf(+1), " +Inf"}, 558 {"% 020f", math.Inf(-1), " -Inf"}, 559 {"% 020f", math.Inf(+1), " Inf"}, 560 {"%+020f", math.Inf(-1), " -Inf"}, 561 {"%+020f", math.Inf(+1), " +Inf"}, 562 {"%20f", -1.0, " -1.000000"}, 563 564 // handle %v like %g 565 {"%v", 0.0, "0"}, 566 {"%v", -7.0, "-7"}, 567 {"%v", -1e-9, "-1e-09"}, 568 {"%v", float32(-1e-9), "-1e-09"}, 569 {"%010v", 0.0, "0000000000"}, 570 571 // *Float cases 572 {"%.20f", "1e-20", "0.00000000000000000001"}, 573 {"%.20f", "-1e-20", "-0.00000000000000000001"}, 574 {"%30.20f", "-1e-20", " -0.00000000000000000001"}, 575 {"%030.20f", "-1e-20", "-00000000.00000000000000000001"}, 576 {"%030.20f", "+1e-20", "000000000.00000000000000000001"}, 577 {"% 030.20f", "+1e-20", " 00000000.00000000000000000001"}, 578 579 // erroneous formats 580 {"%s", 1.0, "%!s(*big.Float=1)"}, 581 } { 582 value := new(Float) 583 switch v := test.value.(type) { 584 case float32: 585 value.SetPrec(24).SetFloat64(float64(v)) 586 case float64: 587 value.SetPrec(53).SetFloat64(v) 588 case string: 589 value.SetPrec(512).Parse(v, 0) 590 default: 591 t.Fatalf("unsupported test value: %v (%T)", v, v) 592 } 593 594 if got := fmt.Sprintf(test.format, value); got != test.want { 595 t.Errorf("%v: got %q; want %q", test, got, test.want) 596 } 597 } 598 } 599 600 func BenchmarkParseFloatSmallExp(b *testing.B) { 601 for i := 0; i < b.N; i++ { 602 for _, s := range []string{ 603 "1e0", 604 "1e-1", 605 "1e-2", 606 "1e-3", 607 "1e-4", 608 "1e-5", 609 "1e-10", 610 "1e-20", 611 "1e-50", 612 "1e1", 613 "1e2", 614 "1e3", 615 "1e4", 616 "1e5", 617 "1e10", 618 "1e20", 619 "1e50", 620 } { 621 var x Float 622 _, _, err := x.Parse(s, 0) 623 if err != nil { 624 b.Fatalf("%s: %v", s, err) 625 } 626 } 627 } 628 } 629 630 func BenchmarkParseFloatLargeExp(b *testing.B) { 631 for i := 0; i < b.N; i++ { 632 for _, s := range []string{ 633 "1e0", 634 "1e-10", 635 "1e-20", 636 "1e-30", 637 "1e-40", 638 "1e-50", 639 "1e-100", 640 "1e-500", 641 "1e-1000", 642 "1e-5000", 643 "1e-10000", 644 "1e10", 645 "1e20", 646 "1e30", 647 "1e40", 648 "1e50", 649 "1e100", 650 "1e500", 651 "1e1000", 652 "1e5000", 653 "1e10000", 654 } { 655 var x Float 656 _, _, err := x.Parse(s, 0) 657 if err != nil { 658 b.Fatalf("%s: %v", s, err) 659 } 660 } 661 } 662 }