github.com/gidoBOSSftw5731/go/src@v0.0.0-20210226122457-d24b0edbf019/strconv/extfloat.go (about) 1 // Copyright 2011 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 6 7 import ( 8 "math/bits" 9 ) 10 11 // An extFloat represents an extended floating-point number, with more 12 // precision than a float64. It does not try to save bits: the 13 // number represented by the structure is mant*(2^exp), with a negative 14 // sign if neg is true. 15 type extFloat struct { 16 mant uint64 17 exp int 18 neg bool 19 } 20 21 // Powers of ten taken from double-conversion library. 22 // https://code.google.com/p/double-conversion/ 23 const ( 24 firstPowerOfTen = -348 25 stepPowerOfTen = 8 26 ) 27 28 var smallPowersOfTen = [...]extFloat{ 29 {1 << 63, -63, false}, // 1 30 {0xa << 60, -60, false}, // 1e1 31 {0x64 << 57, -57, false}, // 1e2 32 {0x3e8 << 54, -54, false}, // 1e3 33 {0x2710 << 50, -50, false}, // 1e4 34 {0x186a0 << 47, -47, false}, // 1e5 35 {0xf4240 << 44, -44, false}, // 1e6 36 {0x989680 << 40, -40, false}, // 1e7 37 } 38 39 var powersOfTen = [...]extFloat{ 40 {0xfa8fd5a0081c0288, -1220, false}, // 10^-348 41 {0xbaaee17fa23ebf76, -1193, false}, // 10^-340 42 {0x8b16fb203055ac76, -1166, false}, // 10^-332 43 {0xcf42894a5dce35ea, -1140, false}, // 10^-324 44 {0x9a6bb0aa55653b2d, -1113, false}, // 10^-316 45 {0xe61acf033d1a45df, -1087, false}, // 10^-308 46 {0xab70fe17c79ac6ca, -1060, false}, // 10^-300 47 {0xff77b1fcbebcdc4f, -1034, false}, // 10^-292 48 {0xbe5691ef416bd60c, -1007, false}, // 10^-284 49 {0x8dd01fad907ffc3c, -980, false}, // 10^-276 50 {0xd3515c2831559a83, -954, false}, // 10^-268 51 {0x9d71ac8fada6c9b5, -927, false}, // 10^-260 52 {0xea9c227723ee8bcb, -901, false}, // 10^-252 53 {0xaecc49914078536d, -874, false}, // 10^-244 54 {0x823c12795db6ce57, -847, false}, // 10^-236 55 {0xc21094364dfb5637, -821, false}, // 10^-228 56 {0x9096ea6f3848984f, -794, false}, // 10^-220 57 {0xd77485cb25823ac7, -768, false}, // 10^-212 58 {0xa086cfcd97bf97f4, -741, false}, // 10^-204 59 {0xef340a98172aace5, -715, false}, // 10^-196 60 {0xb23867fb2a35b28e, -688, false}, // 10^-188 61 {0x84c8d4dfd2c63f3b, -661, false}, // 10^-180 62 {0xc5dd44271ad3cdba, -635, false}, // 10^-172 63 {0x936b9fcebb25c996, -608, false}, // 10^-164 64 {0xdbac6c247d62a584, -582, false}, // 10^-156 65 {0xa3ab66580d5fdaf6, -555, false}, // 10^-148 66 {0xf3e2f893dec3f126, -529, false}, // 10^-140 67 {0xb5b5ada8aaff80b8, -502, false}, // 10^-132 68 {0x87625f056c7c4a8b, -475, false}, // 10^-124 69 {0xc9bcff6034c13053, -449, false}, // 10^-116 70 {0x964e858c91ba2655, -422, false}, // 10^-108 71 {0xdff9772470297ebd, -396, false}, // 10^-100 72 {0xa6dfbd9fb8e5b88f, -369, false}, // 10^-92 73 {0xf8a95fcf88747d94, -343, false}, // 10^-84 74 {0xb94470938fa89bcf, -316, false}, // 10^-76 75 {0x8a08f0f8bf0f156b, -289, false}, // 10^-68 76 {0xcdb02555653131b6, -263, false}, // 10^-60 77 {0x993fe2c6d07b7fac, -236, false}, // 10^-52 78 {0xe45c10c42a2b3b06, -210, false}, // 10^-44 79 {0xaa242499697392d3, -183, false}, // 10^-36 80 {0xfd87b5f28300ca0e, -157, false}, // 10^-28 81 {0xbce5086492111aeb, -130, false}, // 10^-20 82 {0x8cbccc096f5088cc, -103, false}, // 10^-12 83 {0xd1b71758e219652c, -77, false}, // 10^-4 84 {0x9c40000000000000, -50, false}, // 10^4 85 {0xe8d4a51000000000, -24, false}, // 10^12 86 {0xad78ebc5ac620000, 3, false}, // 10^20 87 {0x813f3978f8940984, 30, false}, // 10^28 88 {0xc097ce7bc90715b3, 56, false}, // 10^36 89 {0x8f7e32ce7bea5c70, 83, false}, // 10^44 90 {0xd5d238a4abe98068, 109, false}, // 10^52 91 {0x9f4f2726179a2245, 136, false}, // 10^60 92 {0xed63a231d4c4fb27, 162, false}, // 10^68 93 {0xb0de65388cc8ada8, 189, false}, // 10^76 94 {0x83c7088e1aab65db, 216, false}, // 10^84 95 {0xc45d1df942711d9a, 242, false}, // 10^92 96 {0x924d692ca61be758, 269, false}, // 10^100 97 {0xda01ee641a708dea, 295, false}, // 10^108 98 {0xa26da3999aef774a, 322, false}, // 10^116 99 {0xf209787bb47d6b85, 348, false}, // 10^124 100 {0xb454e4a179dd1877, 375, false}, // 10^132 101 {0x865b86925b9bc5c2, 402, false}, // 10^140 102 {0xc83553c5c8965d3d, 428, false}, // 10^148 103 {0x952ab45cfa97a0b3, 455, false}, // 10^156 104 {0xde469fbd99a05fe3, 481, false}, // 10^164 105 {0xa59bc234db398c25, 508, false}, // 10^172 106 {0xf6c69a72a3989f5c, 534, false}, // 10^180 107 {0xb7dcbf5354e9bece, 561, false}, // 10^188 108 {0x88fcf317f22241e2, 588, false}, // 10^196 109 {0xcc20ce9bd35c78a5, 614, false}, // 10^204 110 {0x98165af37b2153df, 641, false}, // 10^212 111 {0xe2a0b5dc971f303a, 667, false}, // 10^220 112 {0xa8d9d1535ce3b396, 694, false}, // 10^228 113 {0xfb9b7cd9a4a7443c, 720, false}, // 10^236 114 {0xbb764c4ca7a44410, 747, false}, // 10^244 115 {0x8bab8eefb6409c1a, 774, false}, // 10^252 116 {0xd01fef10a657842c, 800, false}, // 10^260 117 {0x9b10a4e5e9913129, 827, false}, // 10^268 118 {0xe7109bfba19c0c9d, 853, false}, // 10^276 119 {0xac2820d9623bf429, 880, false}, // 10^284 120 {0x80444b5e7aa7cf85, 907, false}, // 10^292 121 {0xbf21e44003acdd2d, 933, false}, // 10^300 122 {0x8e679c2f5e44ff8f, 960, false}, // 10^308 123 {0xd433179d9c8cb841, 986, false}, // 10^316 124 {0x9e19db92b4e31ba9, 1013, false}, // 10^324 125 {0xeb96bf6ebadf77d9, 1039, false}, // 10^332 126 {0xaf87023b9bf0ee6b, 1066, false}, // 10^340 127 } 128 129 // AssignComputeBounds sets f to the floating point value 130 // defined by mant, exp and precision given by flt. It returns 131 // lower, upper such that any number in the closed interval 132 // [lower, upper] is converted back to the same floating point number. 133 func (f *extFloat) AssignComputeBounds(mant uint64, exp int, neg bool, flt *floatInfo) (lower, upper extFloat) { 134 f.mant = mant 135 f.exp = exp - int(flt.mantbits) 136 f.neg = neg 137 if f.exp <= 0 && mant == (mant>>uint(-f.exp))<<uint(-f.exp) { 138 // An exact integer 139 f.mant >>= uint(-f.exp) 140 f.exp = 0 141 return *f, *f 142 } 143 expBiased := exp - flt.bias 144 145 upper = extFloat{mant: 2*f.mant + 1, exp: f.exp - 1, neg: f.neg} 146 if mant != 1<<flt.mantbits || expBiased == 1 { 147 lower = extFloat{mant: 2*f.mant - 1, exp: f.exp - 1, neg: f.neg} 148 } else { 149 lower = extFloat{mant: 4*f.mant - 1, exp: f.exp - 2, neg: f.neg} 150 } 151 return 152 } 153 154 // Normalize normalizes f so that the highest bit of the mantissa is 155 // set, and returns the number by which the mantissa was left-shifted. 156 func (f *extFloat) Normalize() uint { 157 // bits.LeadingZeros64 would return 64 158 if f.mant == 0 { 159 return 0 160 } 161 shift := bits.LeadingZeros64(f.mant) 162 f.mant <<= uint(shift) 163 f.exp -= shift 164 return uint(shift) 165 } 166 167 // Multiply sets f to the product f*g: the result is correctly rounded, 168 // but not normalized. 169 func (f *extFloat) Multiply(g extFloat) { 170 hi, lo := bits.Mul64(f.mant, g.mant) 171 // Round up. 172 f.mant = hi + (lo >> 63) 173 f.exp = f.exp + g.exp + 64 174 } 175 176 var uint64pow10 = [...]uint64{ 177 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 178 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 179 } 180 181 // Frexp10 is an analogue of math.Frexp for decimal powers. It scales 182 // f by an approximate power of ten 10^-exp, and returns exp10, so 183 // that f*10^exp10 has the same value as the old f, up to an ulp, 184 // as well as the index of 10^-exp in the powersOfTen table. 185 func (f *extFloat) frexp10() (exp10, index int) { 186 // The constants expMin and expMax constrain the final value of the 187 // binary exponent of f. We want a small integral part in the result 188 // because finding digits of an integer requires divisions, whereas 189 // digits of the fractional part can be found by repeatedly multiplying 190 // by 10. 191 const expMin = -60 192 const expMax = -32 193 // Find power of ten such that x * 10^n has a binary exponent 194 // between expMin and expMax. 195 approxExp10 := ((expMin+expMax)/2 - f.exp) * 28 / 93 // log(10)/log(2) is close to 93/28. 196 i := (approxExp10 - firstPowerOfTen) / stepPowerOfTen 197 Loop: 198 for { 199 exp := f.exp + powersOfTen[i].exp + 64 200 switch { 201 case exp < expMin: 202 i++ 203 case exp > expMax: 204 i-- 205 default: 206 break Loop 207 } 208 } 209 // Apply the desired decimal shift on f. It will have exponent 210 // in the desired range. This is multiplication by 10^-exp10. 211 f.Multiply(powersOfTen[i]) 212 213 return -(firstPowerOfTen + i*stepPowerOfTen), i 214 } 215 216 // frexp10Many applies a common shift by a power of ten to a, b, c. 217 func frexp10Many(a, b, c *extFloat) (exp10 int) { 218 exp10, i := c.frexp10() 219 a.Multiply(powersOfTen[i]) 220 b.Multiply(powersOfTen[i]) 221 return 222 } 223 224 // FixedDecimal stores in d the first n significant digits 225 // of the decimal representation of f. It returns false 226 // if it cannot be sure of the answer. 227 func (f *extFloat) FixedDecimal(d *decimalSlice, n int) bool { 228 if f.mant == 0 { 229 d.nd = 0 230 d.dp = 0 231 d.neg = f.neg 232 return true 233 } 234 if n == 0 { 235 panic("strconv: internal error: extFloat.FixedDecimal called with n == 0") 236 } 237 // Multiply by an appropriate power of ten to have a reasonable 238 // number to process. 239 f.Normalize() 240 exp10, _ := f.frexp10() 241 242 shift := uint(-f.exp) 243 integer := uint32(f.mant >> shift) 244 fraction := f.mant - (uint64(integer) << shift) 245 ε := uint64(1) // ε is the uncertainty we have on the mantissa of f. 246 247 // Write exactly n digits to d. 248 needed := n // how many digits are left to write. 249 integerDigits := 0 // the number of decimal digits of integer. 250 pow10 := uint64(1) // the power of ten by which f was scaled. 251 for i, pow := 0, uint64(1); i < 20; i++ { 252 if pow > uint64(integer) { 253 integerDigits = i 254 break 255 } 256 pow *= 10 257 } 258 rest := integer 259 if integerDigits > needed { 260 // the integral part is already large, trim the last digits. 261 pow10 = uint64pow10[integerDigits-needed] 262 integer /= uint32(pow10) 263 rest -= integer * uint32(pow10) 264 } else { 265 rest = 0 266 } 267 268 // Write the digits of integer: the digits of rest are omitted. 269 var buf [32]byte 270 pos := len(buf) 271 for v := integer; v > 0; { 272 v1 := v / 10 273 v -= 10 * v1 274 pos-- 275 buf[pos] = byte(v + '0') 276 v = v1 277 } 278 for i := pos; i < len(buf); i++ { 279 d.d[i-pos] = buf[i] 280 } 281 nd := len(buf) - pos 282 d.nd = nd 283 d.dp = integerDigits + exp10 284 needed -= nd 285 286 if needed > 0 { 287 if rest != 0 || pow10 != 1 { 288 panic("strconv: internal error, rest != 0 but needed > 0") 289 } 290 // Emit digits for the fractional part. Each time, 10*fraction 291 // fits in a uint64 without overflow. 292 for needed > 0 { 293 fraction *= 10 294 ε *= 10 // the uncertainty scales as we multiply by ten. 295 if 2*ε > 1<<shift { 296 // the error is so large it could modify which digit to write, abort. 297 return false 298 } 299 digit := fraction >> shift 300 d.d[nd] = byte(digit + '0') 301 fraction -= digit << shift 302 nd++ 303 needed-- 304 } 305 d.nd = nd 306 } 307 308 // We have written a truncation of f (a numerator / 10^d.dp). The remaining part 309 // can be interpreted as a small number (< 1) to be added to the last digit of the 310 // numerator. 311 // 312 // If rest > 0, the amount is: 313 // (rest<<shift | fraction) / (pow10 << shift) 314 // fraction being known with a ±ε uncertainty. 315 // The fact that n > 0 guarantees that pow10 << shift does not overflow a uint64. 316 // 317 // If rest = 0, pow10 == 1 and the amount is 318 // fraction / (1 << shift) 319 // fraction being known with a ±ε uncertainty. 320 // 321 // We pass this information to the rounding routine for adjustment. 322 323 ok := adjustLastDigitFixed(d, uint64(rest)<<shift|fraction, pow10, shift, ε) 324 if !ok { 325 return false 326 } 327 // Trim trailing zeros. 328 for i := d.nd - 1; i >= 0; i-- { 329 if d.d[i] != '0' { 330 d.nd = i + 1 331 break 332 } 333 } 334 return true 335 } 336 337 // adjustLastDigitFixed assumes d contains the representation of the integral part 338 // of some number, whose fractional part is num / (den << shift). The numerator 339 // num is only known up to an uncertainty of size ε, assumed to be less than 340 // (den << shift)/2. 341 // 342 // It will increase the last digit by one to account for correct rounding, typically 343 // when the fractional part is greater than 1/2, and will return false if ε is such 344 // that no correct answer can be given. 345 func adjustLastDigitFixed(d *decimalSlice, num, den uint64, shift uint, ε uint64) bool { 346 if num > den<<shift { 347 panic("strconv: num > den<<shift in adjustLastDigitFixed") 348 } 349 if 2*ε > den<<shift { 350 panic("strconv: ε > (den<<shift)/2") 351 } 352 if 2*(num+ε) < den<<shift { 353 return true 354 } 355 if 2*(num-ε) > den<<shift { 356 // increment d by 1. 357 i := d.nd - 1 358 for ; i >= 0; i-- { 359 if d.d[i] == '9' { 360 d.nd-- 361 } else { 362 break 363 } 364 } 365 if i < 0 { 366 d.d[0] = '1' 367 d.nd = 1 368 d.dp++ 369 } else { 370 d.d[i]++ 371 } 372 return true 373 } 374 return false 375 } 376 377 // ShortestDecimal stores in d the shortest decimal representation of f 378 // which belongs to the open interval (lower, upper), where f is supposed 379 // to lie. It returns false whenever the result is unsure. The implementation 380 // uses the Grisu3 algorithm. 381 func (f *extFloat) ShortestDecimal(d *decimalSlice, lower, upper *extFloat) bool { 382 if f.mant == 0 { 383 d.nd = 0 384 d.dp = 0 385 d.neg = f.neg 386 return true 387 } 388 if f.exp == 0 && *lower == *f && *lower == *upper { 389 // an exact integer. 390 var buf [24]byte 391 n := len(buf) - 1 392 for v := f.mant; v > 0; { 393 v1 := v / 10 394 v -= 10 * v1 395 buf[n] = byte(v + '0') 396 n-- 397 v = v1 398 } 399 nd := len(buf) - n - 1 400 for i := 0; i < nd; i++ { 401 d.d[i] = buf[n+1+i] 402 } 403 d.nd, d.dp = nd, nd 404 for d.nd > 0 && d.d[d.nd-1] == '0' { 405 d.nd-- 406 } 407 if d.nd == 0 { 408 d.dp = 0 409 } 410 d.neg = f.neg 411 return true 412 } 413 upper.Normalize() 414 // Uniformize exponents. 415 if f.exp > upper.exp { 416 f.mant <<= uint(f.exp - upper.exp) 417 f.exp = upper.exp 418 } 419 if lower.exp > upper.exp { 420 lower.mant <<= uint(lower.exp - upper.exp) 421 lower.exp = upper.exp 422 } 423 424 exp10 := frexp10Many(lower, f, upper) 425 // Take a safety margin due to rounding in frexp10Many, but we lose precision. 426 upper.mant++ 427 lower.mant-- 428 429 // The shortest representation of f is either rounded up or down, but 430 // in any case, it is a truncation of upper. 431 shift := uint(-upper.exp) 432 integer := uint32(upper.mant >> shift) 433 fraction := upper.mant - (uint64(integer) << shift) 434 435 // How far we can go down from upper until the result is wrong. 436 allowance := upper.mant - lower.mant 437 // How far we should go to get a very precise result. 438 targetDiff := upper.mant - f.mant 439 440 // Count integral digits: there are at most 10. 441 var integerDigits int 442 for i, pow := 0, uint64(1); i < 20; i++ { 443 if pow > uint64(integer) { 444 integerDigits = i 445 break 446 } 447 pow *= 10 448 } 449 for i := 0; i < integerDigits; i++ { 450 pow := uint64pow10[integerDigits-i-1] 451 digit := integer / uint32(pow) 452 d.d[i] = byte(digit + '0') 453 integer -= digit * uint32(pow) 454 // evaluate whether we should stop. 455 if currentDiff := uint64(integer)<<shift + fraction; currentDiff < allowance { 456 d.nd = i + 1 457 d.dp = integerDigits + exp10 458 d.neg = f.neg 459 // Sometimes allowance is so large the last digit might need to be 460 // decremented to get closer to f. 461 return adjustLastDigit(d, currentDiff, targetDiff, allowance, pow<<shift, 2) 462 } 463 } 464 d.nd = integerDigits 465 d.dp = d.nd + exp10 466 d.neg = f.neg 467 468 // Compute digits of the fractional part. At each step fraction does not 469 // overflow. The choice of minExp implies that fraction is less than 2^60. 470 var digit int 471 multiplier := uint64(1) 472 for { 473 fraction *= 10 474 multiplier *= 10 475 digit = int(fraction >> shift) 476 d.d[d.nd] = byte(digit + '0') 477 d.nd++ 478 fraction -= uint64(digit) << shift 479 if fraction < allowance*multiplier { 480 // We are in the admissible range. Note that if allowance is about to 481 // overflow, that is, allowance > 2^64/10, the condition is automatically 482 // true due to the limited range of fraction. 483 return adjustLastDigit(d, 484 fraction, targetDiff*multiplier, allowance*multiplier, 485 1<<shift, multiplier*2) 486 } 487 } 488 } 489 490 // adjustLastDigit modifies d = x-currentDiff*ε, to get closest to 491 // d = x-targetDiff*ε, without becoming smaller than x-maxDiff*ε. 492 // It assumes that a decimal digit is worth ulpDecimal*ε, and that 493 // all data is known with an error estimate of ulpBinary*ε. 494 func adjustLastDigit(d *decimalSlice, currentDiff, targetDiff, maxDiff, ulpDecimal, ulpBinary uint64) bool { 495 if ulpDecimal < 2*ulpBinary { 496 // Approximation is too wide. 497 return false 498 } 499 for currentDiff+ulpDecimal/2+ulpBinary < targetDiff { 500 d.d[d.nd-1]-- 501 currentDiff += ulpDecimal 502 } 503 if currentDiff+ulpDecimal <= targetDiff+ulpDecimal/2+ulpBinary { 504 // we have two choices, and don't know what to do. 505 return false 506 } 507 if currentDiff < ulpBinary || currentDiff > maxDiff-ulpBinary { 508 // we went too far 509 return false 510 } 511 if d.nd == 1 && d.d[0] == '0' { 512 // the number has actually reached zero. 513 d.nd = 0 514 d.dp = 0 515 } 516 return true 517 }