PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/gofrontend/libgo/go/math/big/floatconv_test.go

http://github.com/axw/llgo
Go | 573 lines | 426 code | 70 blank | 77 comment | 36 complexity | d38bc545390daaacea5abc209fb552b3 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  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. package big
  5. import (
  6. "fmt"
  7. "math"
  8. "strconv"
  9. "testing"
  10. )
  11. func TestFloatSetFloat64String(t *testing.T) {
  12. inf := math.Inf(0)
  13. nan := math.NaN()
  14. for _, test := range []struct {
  15. s string
  16. x float64 // NaNs represent invalid inputs
  17. }{
  18. // basics
  19. {"0", 0},
  20. {"-0", -0},
  21. {"+0", 0},
  22. {"1", 1},
  23. {"-1", -1},
  24. {"+1", 1},
  25. {"1.234", 1.234},
  26. {"-1.234", -1.234},
  27. {"+1.234", 1.234},
  28. {".1", 0.1},
  29. {"1.", 1},
  30. {"+1.", 1},
  31. // various zeros
  32. {"0e100", 0},
  33. {"-0e+100", 0},
  34. {"+0e-100", 0},
  35. {"0E100", 0},
  36. {"-0E+100", 0},
  37. {"+0E-100", 0},
  38. // various decimal exponent formats
  39. {"1.e10", 1e10},
  40. {"1e+10", 1e10},
  41. {"+1e-10", 1e-10},
  42. {"1E10", 1e10},
  43. {"1.E+10", 1e10},
  44. {"+1E-10", 1e-10},
  45. // infinities
  46. {"Inf", inf},
  47. {"+Inf", inf},
  48. {"-Inf", -inf},
  49. {"inf", inf},
  50. {"+inf", inf},
  51. {"-inf", -inf},
  52. // invalid numbers
  53. {"", nan},
  54. {"-", nan},
  55. {"0x", nan},
  56. {"0e", nan},
  57. {"1.2ef", nan},
  58. {"2..3", nan},
  59. {"123..", nan},
  60. {"infinity", nan},
  61. {"foobar", nan},
  62. // misc decimal values
  63. {"3.14159265", 3.14159265},
  64. {"-687436.79457e-245", -687436.79457e-245},
  65. {"-687436.79457E245", -687436.79457e245},
  66. {".0000000000000000000000000000000000000001", 1e-40},
  67. {"+10000000000000000000000000000000000000000e-0", 1e40},
  68. // decimal mantissa, binary exponent
  69. {"0p0", 0},
  70. {"-0p0", -0},
  71. {"1p10", 1 << 10},
  72. {"1p+10", 1 << 10},
  73. {"+1p-10", 1.0 / (1 << 10)},
  74. {"1024p-12", 0.25},
  75. {"-1p10", -1024},
  76. {"1.5p1", 3},
  77. // binary mantissa, decimal exponent
  78. {"0b0", 0},
  79. {"-0b0", -0},
  80. {"0b0e+10", 0},
  81. {"-0b0e-10", -0},
  82. {"0b1010", 10},
  83. {"0B1010E2", 1000},
  84. {"0b.1", 0.5},
  85. {"0b.001", 0.125},
  86. {"0b.001e3", 125},
  87. // binary mantissa, binary exponent
  88. {"0b0p+10", 0},
  89. {"-0b0p-10", -0},
  90. {"0b.1010p4", 10},
  91. {"0b1p-1", 0.5},
  92. {"0b001p-3", 0.125},
  93. {"0b.001p3", 1},
  94. {"0b0.01p2", 1},
  95. // hexadecimal mantissa and exponent
  96. {"0x0", 0},
  97. {"-0x0", -0},
  98. {"0x0p+10", 0},
  99. {"-0x0p-10", -0},
  100. {"0xff", 255},
  101. {"0X.8p1", 1},
  102. {"-0X0.00008p16", -0.5},
  103. {"0x0.0000000000001p-1022", math.SmallestNonzeroFloat64},
  104. {"0x1.fffffffffffffp1023", math.MaxFloat64},
  105. } {
  106. var x Float
  107. x.SetPrec(53)
  108. _, ok := x.SetString(test.s)
  109. if math.IsNaN(test.x) {
  110. // test.s is invalid
  111. if ok {
  112. t.Errorf("%s: want parse error", test.s)
  113. }
  114. continue
  115. }
  116. // test.s is valid
  117. if !ok {
  118. t.Errorf("%s: got parse error", test.s)
  119. continue
  120. }
  121. f, _ := x.Float64()
  122. want := new(Float).SetFloat64(test.x)
  123. if x.Cmp(want) != 0 {
  124. t.Errorf("%s: got %s (%v); want %v", test.s, &x, f, test.x)
  125. }
  126. }
  127. }
  128. const (
  129. below1e23 = 99999999999999974834176
  130. above1e23 = 100000000000000008388608
  131. )
  132. func TestFloat64Text(t *testing.T) {
  133. for _, test := range []struct {
  134. x float64
  135. format byte
  136. prec int
  137. want string
  138. }{
  139. {0, 'f', 0, "0"},
  140. {math.Copysign(0, -1), 'f', 0, "-0"},
  141. {1, 'f', 0, "1"},
  142. {-1, 'f', 0, "-1"},
  143. {0.001, 'e', 0, "1e-03"},
  144. {0.459, 'e', 0, "5e-01"},
  145. {1.459, 'e', 0, "1e+00"},
  146. {2.459, 'e', 1, "2.5e+00"},
  147. {3.459, 'e', 2, "3.46e+00"},
  148. {4.459, 'e', 3, "4.459e+00"},
  149. {5.459, 'e', 4, "5.4590e+00"},
  150. {0.001, 'f', 0, "0"},
  151. {0.459, 'f', 0, "0"},
  152. {1.459, 'f', 0, "1"},
  153. {2.459, 'f', 1, "2.5"},
  154. {3.459, 'f', 2, "3.46"},
  155. {4.459, 'f', 3, "4.459"},
  156. {5.459, 'f', 4, "5.4590"},
  157. {0, 'b', 0, "0"},
  158. {math.Copysign(0, -1), 'b', 0, "-0"},
  159. {1.0, 'b', 0, "4503599627370496p-52"},
  160. {-1.0, 'b', 0, "-4503599627370496p-52"},
  161. {4503599627370496, 'b', 0, "4503599627370496p+0"},
  162. {0, 'p', 0, "0"},
  163. {math.Copysign(0, -1), 'p', 0, "-0"},
  164. {1024.0, 'p', 0, "0x.8p+11"},
  165. {-1024.0, 'p', 0, "-0x.8p+11"},
  166. // all test cases below from strconv/ftoa_test.go
  167. {1, 'e', 5, "1.00000e+00"},
  168. {1, 'f', 5, "1.00000"},
  169. {1, 'g', 5, "1"},
  170. // {1, 'g', -1, "1"},
  171. // {20, 'g', -1, "20"},
  172. // {1234567.8, 'g', -1, "1.2345678e+06"},
  173. // {200000, 'g', -1, "200000"},
  174. // {2000000, 'g', -1, "2e+06"},
  175. // g conversion and zero suppression
  176. {400, 'g', 2, "4e+02"},
  177. {40, 'g', 2, "40"},
  178. {4, 'g', 2, "4"},
  179. {.4, 'g', 2, "0.4"},
  180. {.04, 'g', 2, "0.04"},
  181. {.004, 'g', 2, "0.004"},
  182. {.0004, 'g', 2, "0.0004"},
  183. {.00004, 'g', 2, "4e-05"},
  184. {.000004, 'g', 2, "4e-06"},
  185. {0, 'e', 5, "0.00000e+00"},
  186. {0, 'f', 5, "0.00000"},
  187. {0, 'g', 5, "0"},
  188. // {0, 'g', -1, "0"},
  189. {-1, 'e', 5, "-1.00000e+00"},
  190. {-1, 'f', 5, "-1.00000"},
  191. {-1, 'g', 5, "-1"},
  192. // {-1, 'g', -1, "-1"},
  193. {12, 'e', 5, "1.20000e+01"},
  194. {12, 'f', 5, "12.00000"},
  195. {12, 'g', 5, "12"},
  196. // {12, 'g', -1, "12"},
  197. {123456700, 'e', 5, "1.23457e+08"},
  198. {123456700, 'f', 5, "123456700.00000"},
  199. {123456700, 'g', 5, "1.2346e+08"},
  200. // {123456700, 'g', -1, "1.234567e+08"},
  201. {1.2345e6, 'e', 5, "1.23450e+06"},
  202. {1.2345e6, 'f', 5, "1234500.00000"},
  203. {1.2345e6, 'g', 5, "1.2345e+06"},
  204. {1e23, 'e', 17, "9.99999999999999916e+22"},
  205. {1e23, 'f', 17, "99999999999999991611392.00000000000000000"},
  206. {1e23, 'g', 17, "9.9999999999999992e+22"},
  207. // {1e23, 'e', -1, "1e+23"},
  208. // {1e23, 'f', -1, "100000000000000000000000"},
  209. // {1e23, 'g', -1, "1e+23"},
  210. {below1e23, 'e', 17, "9.99999999999999748e+22"},
  211. {below1e23, 'f', 17, "99999999999999974834176.00000000000000000"},
  212. {below1e23, 'g', 17, "9.9999999999999975e+22"},
  213. // {below1e23, 'e', -1, "9.999999999999997e+22"},
  214. // {below1e23, 'f', -1, "99999999999999970000000"},
  215. // {below1e23, 'g', -1, "9.999999999999997e+22"},
  216. {above1e23, 'e', 17, "1.00000000000000008e+23"},
  217. {above1e23, 'f', 17, "100000000000000008388608.00000000000000000"},
  218. // {above1e23, 'g', 17, "1.0000000000000001e+23"},
  219. // {above1e23, 'e', -1, "1.0000000000000001e+23"},
  220. // {above1e23, 'f', -1, "100000000000000010000000"},
  221. // {above1e23, 'g', -1, "1.0000000000000001e+23"},
  222. // {fdiv(5e-304, 1e20), 'g', -1, "5e-324"},
  223. // {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"},
  224. // {32, 'g', -1, "32"},
  225. // {32, 'g', 0, "3e+01"},
  226. // {100, 'x', -1, "%x"},
  227. // {math.NaN(), 'g', -1, "NaN"},
  228. // {-math.NaN(), 'g', -1, "NaN"},
  229. {math.Inf(0), 'g', -1, "+Inf"},
  230. {math.Inf(-1), 'g', -1, "-Inf"},
  231. {-math.Inf(0), 'g', -1, "-Inf"},
  232. {-1, 'b', -1, "-4503599627370496p-52"},
  233. // fixed bugs
  234. {0.9, 'f', 1, "0.9"},
  235. {0.09, 'f', 1, "0.1"},
  236. {0.0999, 'f', 1, "0.1"},
  237. {0.05, 'f', 1, "0.1"},
  238. {0.05, 'f', 0, "0"},
  239. {0.5, 'f', 1, "0.5"},
  240. {0.5, 'f', 0, "0"},
  241. {1.5, 'f', 0, "2"},
  242. // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
  243. // {2.2250738585072012e-308, 'g', -1, "2.2250738585072014e-308"},
  244. // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
  245. // {2.2250738585072011e-308, 'g', -1, "2.225073858507201e-308"},
  246. // Issue 2625.
  247. {383260575764816448, 'f', 0, "383260575764816448"},
  248. // {383260575764816448, 'g', -1, "3.8326057576481645e+17"},
  249. } {
  250. f := new(Float).SetFloat64(test.x)
  251. got := f.Text(test.format, test.prec)
  252. if got != test.want {
  253. t.Errorf("%v: got %s; want %s", test, got, test.want)
  254. }
  255. if test.format == 'b' && test.x == 0 {
  256. continue // 'b' format in strconv.Float requires knowledge of bias for 0.0
  257. }
  258. if test.format == 'p' {
  259. continue // 'p' format not supported in strconv.Format
  260. }
  261. // verify that Float format matches strconv format
  262. want := strconv.FormatFloat(test.x, test.format, test.prec, 64)
  263. if got != want {
  264. t.Errorf("%v: got %s; want %s (strconv)", test, got, want)
  265. }
  266. }
  267. }
  268. func TestFloatText(t *testing.T) {
  269. for _, test := range []struct {
  270. x string
  271. prec uint
  272. format byte
  273. digits int
  274. want string
  275. }{
  276. {"0", 10, 'f', 0, "0"},
  277. {"-0", 10, 'f', 0, "-0"},
  278. {"1", 10, 'f', 0, "1"},
  279. {"-1", 10, 'f', 0, "-1"},
  280. {"1.459", 100, 'e', 0, "1e+00"},
  281. {"2.459", 100, 'e', 1, "2.5e+00"},
  282. {"3.459", 100, 'e', 2, "3.46e+00"},
  283. {"4.459", 100, 'e', 3, "4.459e+00"},
  284. {"5.459", 100, 'e', 4, "5.4590e+00"},
  285. {"1.459", 100, 'E', 0, "1E+00"},
  286. {"2.459", 100, 'E', 1, "2.5E+00"},
  287. {"3.459", 100, 'E', 2, "3.46E+00"},
  288. {"4.459", 100, 'E', 3, "4.459E+00"},
  289. {"5.459", 100, 'E', 4, "5.4590E+00"},
  290. {"1.459", 100, 'f', 0, "1"},
  291. {"2.459", 100, 'f', 1, "2.5"},
  292. {"3.459", 100, 'f', 2, "3.46"},
  293. {"4.459", 100, 'f', 3, "4.459"},
  294. {"5.459", 100, 'f', 4, "5.4590"},
  295. {"1.459", 100, 'g', 0, "1"},
  296. {"2.459", 100, 'g', 1, "2"},
  297. {"3.459", 100, 'g', 2, "3.5"},
  298. {"4.459", 100, 'g', 3, "4.46"},
  299. {"5.459", 100, 'g', 4, "5.459"},
  300. {"1459", 53, 'g', 0, "1e+03"},
  301. {"2459", 53, 'g', 1, "2e+03"},
  302. {"3459", 53, 'g', 2, "3.5e+03"},
  303. {"4459", 53, 'g', 3, "4.46e+03"},
  304. {"5459", 53, 'g', 4, "5459"},
  305. {"1459", 53, 'G', 0, "1E+03"},
  306. {"2459", 53, 'G', 1, "2E+03"},
  307. {"3459", 53, 'G', 2, "3.5E+03"},
  308. {"4459", 53, 'G', 3, "4.46E+03"},
  309. {"5459", 53, 'G', 4, "5459"},
  310. {"3", 10, 'e', 40, "3.0000000000000000000000000000000000000000e+00"},
  311. {"3", 10, 'f', 40, "3.0000000000000000000000000000000000000000"},
  312. {"3", 10, 'g', 40, "3"},
  313. {"3e40", 100, 'e', 40, "3.0000000000000000000000000000000000000000e+40"},
  314. {"3e40", 100, 'f', 4, "30000000000000000000000000000000000000000.0000"},
  315. {"3e40", 100, 'g', 40, "3e+40"},
  316. // make sure "stupid" exponents don't stall the machine
  317. {"1e1000000", 64, 'p', 0, "0x.88b3a28a05eade3ap+3321929"},
  318. {"1e1000000000", 64, 'p', 0, "0x.ecc5f45aa573d3p+1538481529"},
  319. {"1e-1000000", 64, 'p', 0, "0x.efb4542cc8ca418ap-3321928"},
  320. {"1e-1000000000", 64, 'p', 0, "0x.8a64dd983a4c7dabp-1538481528"},
  321. // TODO(gri) need tests for actual large Floats
  322. {"0", 53, 'b', 0, "0"},
  323. {"-0", 53, 'b', 0, "-0"},
  324. {"1.0", 53, 'b', 0, "4503599627370496p-52"},
  325. {"-1.0", 53, 'b', 0, "-4503599627370496p-52"},
  326. {"4503599627370496", 53, 'b', 0, "4503599627370496p+0"},
  327. // issue 9939
  328. {"3", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
  329. {"03", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
  330. {"3.", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
  331. {"3.0", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
  332. {"3.00", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
  333. {"3.000", 350, 'b', 0, "1720123961992553633708115671476565205597423741876210842803191629540192157066363606052513914832594264915968p-348"},
  334. {"3", 350, 'p', 0, "0x.cp+2"},
  335. {"03", 350, 'p', 0, "0x.cp+2"},
  336. {"3.", 350, 'p', 0, "0x.cp+2"},
  337. {"3.0", 350, 'p', 0, "0x.cp+2"},
  338. {"3.00", 350, 'p', 0, "0x.cp+2"},
  339. {"3.000", 350, 'p', 0, "0x.cp+2"},
  340. {"0", 64, 'p', 0, "0"},
  341. {"-0", 64, 'p', 0, "-0"},
  342. {"1024.0", 64, 'p', 0, "0x.8p+11"},
  343. {"-1024.0", 64, 'p', 0, "-0x.8p+11"},
  344. // unsupported format
  345. {"3.14", 64, 'x', 0, "%x"},
  346. {"-3.14", 64, 'x', 0, "%x"},
  347. } {
  348. f, _, err := ParseFloat(test.x, 0, test.prec, ToNearestEven)
  349. if err != nil {
  350. t.Errorf("%v: %s", test, err)
  351. continue
  352. }
  353. got := f.Text(test.format, test.digits)
  354. if got != test.want {
  355. t.Errorf("%v: got %s; want %s", test, got, test.want)
  356. }
  357. // compare with strconv.FormatFloat output if possible
  358. // ('p' format is not supported by strconv.FormatFloat,
  359. // and its output for 0.0 prints a biased exponent value
  360. // as in 0p-1074 which makes no sense to emulate here)
  361. if test.prec == 53 && test.format != 'p' && f.Sign() != 0 {
  362. f64, acc := f.Float64()
  363. if acc != Exact {
  364. t.Errorf("%v: expected exact conversion to float64", test)
  365. continue
  366. }
  367. got := strconv.FormatFloat(f64, test.format, test.digits, 64)
  368. if got != test.want {
  369. t.Errorf("%v: got %s; want %s", test, got, test.want)
  370. }
  371. }
  372. }
  373. }
  374. func TestFloatFormat(t *testing.T) {
  375. for _, test := range []struct {
  376. format string
  377. value interface{} // float32, float64, or string (== 512bit *Float)
  378. want string
  379. }{
  380. // TODO(gri) uncomment the disabled 'g'/'G' formats
  381. // below once (*Float).Text supports prec < 0
  382. // from fmt/fmt_test.go
  383. {"%+.3e", 0.0, "+0.000e+00"},
  384. {"%+.3e", 1.0, "+1.000e+00"},
  385. {"%+.3f", -1.0, "-1.000"},
  386. {"%+.3F", -1.0, "-1.000"},
  387. {"%+.3F", float32(-1.0), "-1.000"},
  388. {"%+07.2f", 1.0, "+001.00"},
  389. {"%+07.2f", -1.0, "-001.00"},
  390. {"%+10.2f", +1.0, " +1.00"},
  391. {"%+10.2f", -1.0, " -1.00"},
  392. {"% .3E", -1.0, "-1.000E+00"},
  393. {"% .3e", 1.0, " 1.000e+00"},
  394. {"%+.3g", 0.0, "+0"},
  395. {"%+.3g", 1.0, "+1"},
  396. {"%+.3g", -1.0, "-1"},
  397. {"% .3g", -1.0, "-1"},
  398. {"% .3g", 1.0, " 1"},
  399. {"%b", float32(1.0), "8388608p-23"},
  400. {"%b", 1.0, "4503599627370496p-52"},
  401. // from fmt/fmt_test.go: old test/fmt_test.go
  402. {"%e", 1.0, "1.000000e+00"},
  403. {"%e", 1234.5678e3, "1.234568e+06"},
  404. {"%e", 1234.5678e-8, "1.234568e-05"},
  405. {"%e", -7.0, "-7.000000e+00"},
  406. {"%e", -1e-9, "-1.000000e-09"},
  407. {"%f", 1234.5678e3, "1234567.800000"},
  408. {"%f", 1234.5678e-8, "0.000012"},
  409. {"%f", -7.0, "-7.000000"},
  410. {"%f", -1e-9, "-0.000000"},
  411. // {"%g", 1234.5678e3, "1.2345678e+06"},
  412. // {"%g", float32(1234.5678e3), "1.2345678e+06"},
  413. // {"%g", 1234.5678e-8, "1.2345678e-05"},
  414. {"%g", -7.0, "-7"},
  415. {"%g", -1e-9, "-1e-09"},
  416. {"%g", float32(-1e-9), "-1e-09"},
  417. {"%E", 1.0, "1.000000E+00"},
  418. {"%E", 1234.5678e3, "1.234568E+06"},
  419. {"%E", 1234.5678e-8, "1.234568E-05"},
  420. {"%E", -7.0, "-7.000000E+00"},
  421. {"%E", -1e-9, "-1.000000E-09"},
  422. // {"%G", 1234.5678e3, "1.2345678E+06"},
  423. // {"%G", float32(1234.5678e3), "1.2345678E+06"},
  424. // {"%G", 1234.5678e-8, "1.2345678E-05"},
  425. {"%G", -7.0, "-7"},
  426. {"%G", -1e-9, "-1E-09"},
  427. {"%G", float32(-1e-9), "-1E-09"},
  428. {"%20.6e", 1.2345e3, " 1.234500e+03"},
  429. {"%20.6e", 1.2345e-3, " 1.234500e-03"},
  430. {"%20e", 1.2345e3, " 1.234500e+03"},
  431. {"%20e", 1.2345e-3, " 1.234500e-03"},
  432. {"%20.8e", 1.2345e3, " 1.23450000e+03"},
  433. {"%20f", 1.23456789e3, " 1234.567890"},
  434. {"%20f", 1.23456789e-3, " 0.001235"},
  435. {"%20f", 12345678901.23456789, " 12345678901.234568"},
  436. {"%-20f", 1.23456789e3, "1234.567890 "},
  437. {"%20.8f", 1.23456789e3, " 1234.56789000"},
  438. {"%20.8f", 1.23456789e-3, " 0.00123457"},
  439. // {"%g", 1.23456789e3, "1234.56789"},
  440. // {"%g", 1.23456789e-3, "0.00123456789"},
  441. // {"%g", 1.23456789e20, "1.23456789e+20"},
  442. {"%20e", math.Inf(1), " +Inf"},
  443. {"%-20f", math.Inf(-1), "-Inf "},
  444. // from fmt/fmt_test.go: comparison of padding rules with C printf
  445. {"%.2f", 1.0, "1.00"},
  446. {"%.2f", -1.0, "-1.00"},
  447. {"% .2f", 1.0, " 1.00"},
  448. {"% .2f", -1.0, "-1.00"},
  449. {"%+.2f", 1.0, "+1.00"},
  450. {"%+.2f", -1.0, "-1.00"},
  451. {"%7.2f", 1.0, " 1.00"},
  452. {"%7.2f", -1.0, " -1.00"},
  453. {"% 7.2f", 1.0, " 1.00"},
  454. {"% 7.2f", -1.0, " -1.00"},
  455. {"%+7.2f", 1.0, " +1.00"},
  456. {"%+7.2f", -1.0, " -1.00"},
  457. {"%07.2f", 1.0, "0001.00"},
  458. {"%07.2f", -1.0, "-001.00"},
  459. {"% 07.2f", 1.0, " 001.00"},
  460. {"% 07.2f", -1.0, "-001.00"},
  461. {"%+07.2f", 1.0, "+001.00"},
  462. {"%+07.2f", -1.0, "-001.00"},
  463. // from fmt/fmt_test.go: zero padding does not apply to infinities
  464. {"%020f", math.Inf(-1), " -Inf"},
  465. {"%020f", math.Inf(+1), " +Inf"},
  466. {"% 020f", math.Inf(-1), " -Inf"},
  467. {"% 020f", math.Inf(+1), " Inf"},
  468. {"%+020f", math.Inf(-1), " -Inf"},
  469. {"%+020f", math.Inf(+1), " +Inf"},
  470. {"%20f", -1.0, " -1.000000"},
  471. // handle %v like %g
  472. {"%v", 0.0, "0"},
  473. {"%v", -7.0, "-7"},
  474. {"%v", -1e-9, "-1e-09"},
  475. {"%v", float32(-1e-9), "-1e-09"},
  476. {"%010v", 0.0, "0000000000"},
  477. {"%010v", 0.0, "0000000000"},
  478. // *Float cases
  479. {"%.20f", "1e-20", "0.00000000000000000001"},
  480. {"%.20f", "-1e-20", "-0.00000000000000000001"},
  481. {"%30.20f", "-1e-20", " -0.00000000000000000001"},
  482. {"%030.20f", "-1e-20", "-00000000.00000000000000000001"},
  483. {"%030.20f", "+1e-20", "000000000.00000000000000000001"},
  484. {"% 030.20f", "+1e-20", " 00000000.00000000000000000001"},
  485. // erroneous formats
  486. {"%s", 1.0, "%!s(*big.Float=1)"},
  487. } {
  488. value := new(Float)
  489. switch v := test.value.(type) {
  490. case float32:
  491. value.SetPrec(24).SetFloat64(float64(v))
  492. case float64:
  493. value.SetPrec(53).SetFloat64(v)
  494. case string:
  495. value.SetPrec(512).Parse(v, 0)
  496. default:
  497. t.Fatalf("unsupported test value: %v (%T)", v, v)
  498. }
  499. if got := fmt.Sprintf(test.format, value); got != test.want {
  500. t.Errorf("%v: got %q; want %q", test, got, test.want)
  501. }
  502. }
  503. }