github.com/x04/go/src@v0.0.0-20200202162449-3d481ceb3525/strconv/ftoa_test.go (about) 1 // Copyright 2009 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 strconv_test 6 7 import ( 8 "github.com/x04/go/src/math" 9 "github.com/x04/go/src/math/rand" 10 . "github.com/x04/go/src/strconv" 11 "github.com/x04/go/src/testing" 12 ) 13 14 type ftoaTest struct { 15 f float64 16 fmt byte 17 prec int 18 s string 19 } 20 21 func fdiv(a, b float64) float64 { return a / b } 22 23 const ( 24 below1e23 = 99999999999999974834176 25 above1e23 = 100000000000000008388608 26 ) 27 28 var ftoatests = []ftoaTest{ 29 {1, 'e', 5, "1.00000e+00"}, 30 {1, 'f', 5, "1.00000"}, 31 {1, 'g', 5, "1"}, 32 {1, 'g', -1, "1"}, 33 {1, 'x', -1, "0x1p+00"}, 34 {1, 'x', 5, "0x1.00000p+00"}, 35 {20, 'g', -1, "20"}, 36 {20, 'x', -1, "0x1.4p+04"}, 37 {1234567.8, 'g', -1, "1.2345678e+06"}, 38 {1234567.8, 'x', -1, "0x1.2d687cccccccdp+20"}, 39 {200000, 'g', -1, "200000"}, 40 {200000, 'x', -1, "0x1.86ap+17"}, 41 {200000, 'X', -1, "0X1.86AP+17"}, 42 {2000000, 'g', -1, "2e+06"}, 43 44 // g conversion and zero suppression 45 {400, 'g', 2, "4e+02"}, 46 {40, 'g', 2, "40"}, 47 {4, 'g', 2, "4"}, 48 {.4, 'g', 2, "0.4"}, 49 {.04, 'g', 2, "0.04"}, 50 {.004, 'g', 2, "0.004"}, 51 {.0004, 'g', 2, "0.0004"}, 52 {.00004, 'g', 2, "4e-05"}, 53 {.000004, 'g', 2, "4e-06"}, 54 55 {0, 'e', 5, "0.00000e+00"}, 56 {0, 'f', 5, "0.00000"}, 57 {0, 'g', 5, "0"}, 58 {0, 'g', -1, "0"}, 59 {0, 'x', 5, "0x0.00000p+00"}, 60 61 {-1, 'e', 5, "-1.00000e+00"}, 62 {-1, 'f', 5, "-1.00000"}, 63 {-1, 'g', 5, "-1"}, 64 {-1, 'g', -1, "-1"}, 65 66 {12, 'e', 5, "1.20000e+01"}, 67 {12, 'f', 5, "12.00000"}, 68 {12, 'g', 5, "12"}, 69 {12, 'g', -1, "12"}, 70 71 {123456700, 'e', 5, "1.23457e+08"}, 72 {123456700, 'f', 5, "123456700.00000"}, 73 {123456700, 'g', 5, "1.2346e+08"}, 74 {123456700, 'g', -1, "1.234567e+08"}, 75 76 {1.2345e6, 'e', 5, "1.23450e+06"}, 77 {1.2345e6, 'f', 5, "1234500.00000"}, 78 {1.2345e6, 'g', 5, "1.2345e+06"}, 79 80 {1e23, 'e', 17, "9.99999999999999916e+22"}, 81 {1e23, 'f', 17, "99999999999999991611392.00000000000000000"}, 82 {1e23, 'g', 17, "9.9999999999999992e+22"}, 83 84 {1e23, 'e', -1, "1e+23"}, 85 {1e23, 'f', -1, "100000000000000000000000"}, 86 {1e23, 'g', -1, "1e+23"}, 87 88 {below1e23, 'e', 17, "9.99999999999999748e+22"}, 89 {below1e23, 'f', 17, "99999999999999974834176.00000000000000000"}, 90 {below1e23, 'g', 17, "9.9999999999999975e+22"}, 91 92 {below1e23, 'e', -1, "9.999999999999997e+22"}, 93 {below1e23, 'f', -1, "99999999999999970000000"}, 94 {below1e23, 'g', -1, "9.999999999999997e+22"}, 95 96 {above1e23, 'e', 17, "1.00000000000000008e+23"}, 97 {above1e23, 'f', 17, "100000000000000008388608.00000000000000000"}, 98 {above1e23, 'g', 17, "1.0000000000000001e+23"}, 99 100 {above1e23, 'e', -1, "1.0000000000000001e+23"}, 101 {above1e23, 'f', -1, "100000000000000010000000"}, 102 {above1e23, 'g', -1, "1.0000000000000001e+23"}, 103 104 {fdiv(5e-304, 1e20), 'g', -1, "5e-324"}, // avoid constant arithmetic 105 {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, // avoid constant arithmetic 106 107 {32, 'g', -1, "32"}, 108 {32, 'g', 0, "3e+01"}, 109 110 {100, 'x', -1, "0x1.9p+06"}, 111 {100, 'y', -1, "%y"}, 112 113 {math.NaN(), 'g', -1, "NaN"}, 114 {-math.NaN(), 'g', -1, "NaN"}, 115 {math.Inf(0), 'g', -1, "+Inf"}, 116 {math.Inf(-1), 'g', -1, "-Inf"}, 117 {-math.Inf(0), 'g', -1, "-Inf"}, 118 119 {-1, 'b', -1, "-4503599627370496p-52"}, 120 121 // fixed bugs 122 {0.9, 'f', 1, "0.9"}, 123 {0.09, 'f', 1, "0.1"}, 124 {0.0999, 'f', 1, "0.1"}, 125 {0.05, 'f', 1, "0.1"}, 126 {0.05, 'f', 0, "0"}, 127 {0.5, 'f', 1, "0.5"}, 128 {0.5, 'f', 0, "0"}, 129 {1.5, 'f', 0, "2"}, 130 131 // https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ 132 {2.2250738585072012e-308, 'g', -1, "2.2250738585072014e-308"}, 133 // https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ 134 {2.2250738585072011e-308, 'g', -1, "2.225073858507201e-308"}, 135 136 // Issue 2625. 137 {383260575764816448, 'f', 0, "383260575764816448"}, 138 {383260575764816448, 'g', -1, "3.8326057576481645e+17"}, 139 140 // Issue 29491. 141 {498484681984085570, 'f', -1, "498484681984085570"}, 142 {-5.8339553793802237e+23, 'g', -1, "-5.8339553793802237e+23"}, 143 144 // rounding 145 {2.275555555555555, 'x', -1, "0x1.23456789abcdep+01"}, 146 {2.275555555555555, 'x', 0, "0x1p+01"}, 147 {2.275555555555555, 'x', 2, "0x1.23p+01"}, 148 {2.275555555555555, 'x', 16, "0x1.23456789abcde000p+01"}, 149 {2.275555555555555, 'x', 21, "0x1.23456789abcde00000000p+01"}, 150 {2.2755555510520935, 'x', -1, "0x1.2345678p+01"}, 151 {2.2755555510520935, 'x', 6, "0x1.234568p+01"}, 152 {2.275555431842804, 'x', -1, "0x1.2345668p+01"}, 153 {2.275555431842804, 'x', 6, "0x1.234566p+01"}, 154 {3.999969482421875, 'x', -1, "0x1.ffffp+01"}, 155 {3.999969482421875, 'x', 4, "0x1.ffffp+01"}, 156 {3.999969482421875, 'x', 3, "0x1.000p+02"}, 157 {3.999969482421875, 'x', 2, "0x1.00p+02"}, 158 {3.999969482421875, 'x', 1, "0x1.0p+02"}, 159 {3.999969482421875, 'x', 0, "0x1p+02"}, 160 } 161 162 func TestFtoa(t *testing.T) { 163 for i := 0; i < len(ftoatests); i++ { 164 test := &ftoatests[i] 165 s := FormatFloat(test.f, test.fmt, test.prec, 64) 166 if s != test.s { 167 t.Error("testN=64", test.f, string(test.fmt), test.prec, "want", test.s, "got", s) 168 } 169 x := AppendFloat([]byte("abc"), test.f, test.fmt, test.prec, 64) 170 if string(x) != "abc"+test.s { 171 t.Error("AppendFloat testN=64", test.f, string(test.fmt), test.prec, "want", "abc"+test.s, "got", string(x)) 172 } 173 if float64(float32(test.f)) == test.f && test.fmt != 'b' { 174 s := FormatFloat(test.f, test.fmt, test.prec, 32) 175 if s != test.s { 176 t.Error("testN=32", test.f, string(test.fmt), test.prec, "want", test.s, "got", s) 177 } 178 x := AppendFloat([]byte("abc"), test.f, test.fmt, test.prec, 32) 179 if string(x) != "abc"+test.s { 180 t.Error("AppendFloat testN=32", test.f, string(test.fmt), test.prec, "want", "abc"+test.s, "got", string(x)) 181 } 182 } 183 } 184 } 185 186 func TestFtoaRandom(t *testing.T) { 187 N := int(1e4) 188 if testing.Short() { 189 N = 100 190 } 191 t.Logf("testing %d random numbers with fast and slow FormatFloat", N) 192 for i := 0; i < N; i++ { 193 bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32()) 194 x := math.Float64frombits(bits) 195 196 shortFast := FormatFloat(x, 'g', -1, 64) 197 SetOptimize(false) 198 shortSlow := FormatFloat(x, 'g', -1, 64) 199 SetOptimize(true) 200 if shortSlow != shortFast { 201 t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow) 202 } 203 204 prec := rand.Intn(12) + 5 205 shortFast = FormatFloat(x, 'e', prec, 64) 206 SetOptimize(false) 207 shortSlow = FormatFloat(x, 'e', prec, 64) 208 SetOptimize(true) 209 if shortSlow != shortFast { 210 t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow) 211 } 212 } 213 } 214 215 var ftoaBenches = []struct { 216 name string 217 float float64 218 fmt byte 219 prec int 220 bitSize int 221 }{ 222 {"Decimal", 33909, 'g', -1, 64}, 223 {"Float", 339.7784, 'g', -1, 64}, 224 {"Exp", -5.09e75, 'g', -1, 64}, 225 {"NegExp", -5.11e-95, 'g', -1, 64}, 226 227 {"Big", 123456789123456789123456789, 'g', -1, 64}, 228 {"BinaryExp", -1, 'b', -1, 64}, 229 230 {"32Integer", 33909, 'g', -1, 32}, 231 {"32ExactFraction", 3.375, 'g', -1, 32}, 232 {"32Point", 339.7784, 'g', -1, 32}, 233 {"32Exp", -5.09e25, 'g', -1, 32}, 234 {"32NegExp", -5.11e-25, 'g', -1, 32}, 235 236 {"64Fixed1", 123456, 'e', 3, 64}, 237 {"64Fixed2", 123.456, 'e', 3, 64}, 238 {"64Fixed3", 1.23456e+78, 'e', 3, 64}, 239 {"64Fixed4", 1.23456e-78, 'e', 3, 64}, 240 241 // Trigger slow path (see issue #15672). 242 {"Slowpath64", 622666234635.3213e-320, 'e', -1, 64}, 243 } 244 245 func BenchmarkFormatFloat(b *testing.B) { 246 for _, c := range ftoaBenches { 247 b.Run(c.name, func(b *testing.B) { 248 for i := 0; i < b.N; i++ { 249 FormatFloat(c.float, c.fmt, c.prec, c.bitSize) 250 } 251 }) 252 } 253 } 254 255 func BenchmarkAppendFloat(b *testing.B) { 256 dst := make([]byte, 30) 257 for _, c := range ftoaBenches { 258 b.Run(c.name, func(b *testing.B) { 259 for i := 0; i < b.N; i++ { 260 AppendFloat(dst[:0], c.float, c.fmt, c.prec, c.bitSize) 261 } 262 }) 263 } 264 }