aboutsummaryrefslogtreecommitdiff
path: root/src/math/big/floatconv_test.go
AgeCommit message (Collapse)Author
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2019-09-18math/big: avoid MinExp exponent wrap-around in 'x' Text formatRobert Griesemer
Fixes #34343. Change-Id: I74240c8f431f6596338633a86a7a5ee1fce70a65 Reviewed-on: https://go-review.googlesource.com/c/go/+/196057 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-03-12math/big: add support for underscores '_' in numbersRobert Griesemer
The primary change is in nat.scan which now accepts underscores for base 0. While at it, streamlined error handling in that function as well. Also, improved the corresponding test significantly by checking the expected result values also in case of scan errors. The second major change is in scanExponent which now accepts underscores when the new sepOk argument is set. While at it, essentially rewrote that function to match error and underscore handling of nat.scan more closely. Added a new test for scanExponent which until now was only tested indirectly. Finally, updated the documentation for several functions and added many new test cases to clients of nat.scan. A major portion of this CL is due to much better test coverage. Updates #28493. Change-Id: I7f17b361b633fbe6c798619d891bd5e0a045b5c5 Reviewed-on: https://go-review.googlesource.com/c/go/+/166157 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-03-07math/big: support new octal prefixes 0o and 0ORobert Griesemer
This CL extends the various SetString and Parse methods for Ints, Rats, and Floats to accept the new octal prefixes. The main change is in natconv.go, all other changes are documentation and test updates. Finally, this CL also fixes TestRatSetString which silently dropped certain failures. Updates #12711. Change-Id: I5ee5879e25013ba1e6eda93ff280915f25ab5d55 Reviewed-on: https://go-review.googlesource.com/c/go/+/165898 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2019-02-26math/big: add %x float formatRuss Cox
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>
2019-02-12strconv: format hex floatsRuss Cox
This CL updates FormatFloat to format standard hexadecimal floating-point constants, using the 'x' and 'X' verbs. See golang.org/design/19308-number-literals for background. For #29008. Change-Id: I540b8f71d492cfdb7c58af533d357a564591f28b Reviewed-on: https://go-review.googlesource.com/c/160242 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-02-11math/big: permit upper-case 'P' binary exponent (not just 'p')Robert Griesemer
The current implementation accepted binary exponents but restricted them to 'p'. This change permits both 'p' and 'P'. R=Go1.13 Updates #29008. Change-Id: I7a89ccb86af4438f17b0422be7cb630ffcf43272 Reviewed-on: https://go-review.googlesource.com/c/159297 Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2018-06-01all: update comment URLs from HTTP to HTTPS, where possibleTim Cooper
Each URL was manually verified to ensure it did not serve up incorrect content. Change-Id: I4dc846227af95a73ee9a3074d0c379ff0fa955df Reviewed-on: https://go-review.googlesource.com/115798 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org>
2017-03-23math/big: replace local versions of bitLen, nlz with math/bits versionsRobert Griesemer
Verified that BenchmarkBitLen time went down from 2.25 ns/op to 0.65 ns/op an a 2.3 GHz Intel Core i7, before removing that benchmark (now covered by math/bits benchmarks). Change-Id: I3890bb7d1889e95b9a94bd68f0bdf06f1885adeb Reviewed-on: https://go-review.googlesource.com/38464 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-03-23math/big: fix TestFloatSetFloat64StringRobert Griesemer
A -0 constant is the same as 0. Use explicit negative zero for float64 -0.0. Also, fix two test cases that were wrong. Fixes #19673. Change-Id: Ic09775f29d9bc2ee7814172e59c4a693441ea730 Reviewed-on: https://go-review.googlesource.com/38463 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-10-19math/big: implement Float.Scan, type assert fmt interfaces to enforce docsEmmanuel Odeke
Implements Float.Scan which satisfies fmt.Scanner interface. Also enforces docs' interface implementation claims with compile time type assertions, that is: + Float always implements fmt.Formatter and fmt.Scanner + Int always implements fmt.Formatter and fmt.Scanner + Rat always implements fmt.Formatter which will ensure that the API claims are strictly matched. Also note that Float.Scan doesn't handle ±Inf. Fixes #17391 Change-Id: I3d3dfbe7f602066975c7a7794fe25b4c645440ce Reviewed-on: https://go-review.googlesource.com/30723 Reviewed-by: Robert Griesemer <gri@golang.org>
2016-06-01math/big: avoid panic in float.Text with negative precAlberto Donizetti
Fixes #15918 Change-Id: I4b434aed262960a2e6c659d4c2296fbf662c3a52 Reviewed-on: https://go-review.googlesource.com/23633 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-29math/big: more documentationRobert Griesemer
Good enough for now. Fixes #11241. Change-Id: Ieb50809f104d20bcbe14daecac503f72486bec92 Reviewed-on: https://go-review.googlesource.com/15111 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-09-24math/big: fix test for denormalized inputs and enable more test casesRobert Griesemer
Also: removed unnecessary BUG comment (was fixed). Change-Id: I8f11fbcb4e30a19ec5a25df742b3e25e2ee7f846 Reviewed-on: https://go-review.googlesource.com/14923 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-09-23math/big: implement negative precision for Float.Append/TextRobert Griesemer
Enabled all but a handful of disabled Float formatting test cases. Fixes #10991. Change-Id: Id18e160e857be2743429a377000e996978015a1a Reviewed-on: https://go-review.googlesource.com/14850 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-09-22math/big: add test cases for min/max exponent valuesRobert Griesemer
Change-Id: I2e74e39628285e2fecaab712be6cff230619a6c2 Reviewed-on: https://go-review.googlesource.com/14778 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-09-22math/big: optimize Float.Parse by reducing powers of 10 to powers of 2 and 5Robert Griesemer
Instead of computing the final adjustment factor as a power of 10, it's more efficient to split 10**e into 2**e * 5**e . Powers of 2 are trivially added to the Float exponent, and powers of 5 are smaller and thus faster to compute. Also, use a table of uint64 values rather than float64 values for initial power value. uint64 values appear to be faster to convert to Floats (useful for small exponents). Added two small benchmarks to confirm that there's no regresssion. benchmark old ns/op new ns/op delta BenchmarkParseFloatSmallExp-8 17543 16220 -7.54% BenchmarkParseFloatLargeExp-8 60865 59996 -1.43% Change-Id: I3efd7556b023316f86f334137a67fe0c6d52f8ef Reviewed-on: https://go-review.googlesource.com/14782 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-08-21math/big: correctly handle large exponent in SetStringAlberto Donizetti
Even though the umul/uquo functions expect two valid, finite big.Floats arguments, SetString was calling them with possibly Inf values, which resulted in bogus return values. Replace umul and udiv calls with Mul and Quo calls to fix this. Also, fix two wrong tests. See relevant issue on issue tracker for a detailed explanation. Fixes #11341 Change-Id: Ie35222763a57a2d712a5f5f7baec75cab8189a53 Reviewed-on: https://go-review.googlesource.com/13778 Reviewed-by: Robert Griesemer <gri@golang.org>
2015-06-02math/big: implement fmt.Formatter-compatible (*Float).FormatRobert Griesemer
Change-Id: I22fdba8ecaecf4e9201b845e65d982cac09f254a Reviewed-on: https://go-review.googlesource.com/10499 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-05-29math/big: remove (*Float).Scan, ScanFloat; more robust (*Float).ParseRobert Griesemer
- (*Float).Scan conflicted with fmt.Scanner.Scan; it was also only used internally. Removed it, as well as the companion ScanFloat function. - (*Float).Parse (and thus ParseFloat) can now also parse infinities. As a result, more code could be simplified. - Fixed a bug in rounding (round may implicitly be called for infinite values). Found via existing test cases, after simplifying some code. - Added more test cases. Fixes issue #10938. Change-Id: I1df97821654f034965ba8b82b272e52e6dc427f1 Reviewed-on: https://go-review.googlesource.com/10498 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-05-29math/big: rename (*Float).Format to (*Float).TextRobert Griesemer
This paves the way for a fmt-compatible (*Float).Format method. A better name then Text is still desirable (suggestions welcome). This is partly fixing issue #10938. Change-Id: I59c20a8cee11f5dba059fe0f38b414fe75f2ab13 Reviewed-on: https://go-review.googlesource.com/10493 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-05-28math/big: fix latent decimal conversion bugRobert Griesemer
A decimal represented 0.0 with a 0-length mantissa and undefined exponent, but the formatting code assumes a valid zero exponent if the float value is 0.0. The code worked because we allocate a new decimal value each time and because there's no rounding that lead to 0.0. Change-Id: Ifd771d7709de83b87fdbf141786286b4c3e13d4f Reviewed-on: https://go-review.googlesource.com/10448 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-05-27math/big: removed TODO, cleanupsRobert Griesemer
- factor out handling of sign - rename bstring, pstring to fmtB, fmtP consistent with fmtE, fmtF - move all float-to-string conversion functions into ftoa.go - no functional changes Change-Id: I5970ecb874dc9c387630b59147d90bda16a5d8e6 Reviewed-on: https://go-review.googlesource.com/10387 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-05-22math/big: Always print exponent sign when using 'p' exponent for Floats.Robert Griesemer
Float.Format supports the 'b' and 'p' format, both of which print a binary ('p') exponent. The 'b' format always printed a sign ('+' or '-') for the exponent; the 'p' format only printed a negative sign for the exponent. This change makes the two consistent. It also makes the 'p' format easier to read if the exponent is >= 0. Also: - Comments added elsewhere. Change-Id: Ifd2e01bdafb3043345972ca22a90248d055bd29b Reviewed-on: https://go-review.googlesource.com/10359 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-04-04math/big: compute 10**exp efficiently when converting FloatsRobert Griesemer
Change-Id: Ic2d9fdae43d18255c198ae62376212bdc89b75da Reviewed-on: https://go-review.googlesource.com/8464 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-03-31math/big: remove NaN support - just not worth itRobert Griesemer
NaNs make the API more complicated for no real good reasons. There are few operations that produce NaNs with IEEE arithmetic, there's no need to copy the behavior. It's easy to test for these scenarios and avoid them (on the other hand, it's not easy to test for overflow or underflow, so we want to keep +/-Inf). Also: - renamed IsNeg -> Signbit (clearer, especially for x == -0) - removed IsZero (Sign() == 0 is sufficient and efficient) - removed IsFinite (now same as !IsInf) Change-Id: I3f3b4445c325d9bbb1bf46ce2e298a6aeb498e07 Reviewed-on: https://go-review.googlesource.com/8280 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-03-14math/big: wrap Float.Cmp result in struct to prevent wrong useRobert Griesemer
Float.Cmp used to return a value < 0, 0, or > 0 depending on how arguments x, y compared against each other. With the possibility of NaNs, the result was changed into an Accuracy (to include Undef). Consequently, Float.Cmp results could still be compared for (in-) equality with 0, but comparing if < 0 or > 0 would provide the wrong answer w/o any obvious notice by the compiler. This change wraps Float.Cmp results into a struct and accessors are used to access the desired result. This prevents incorrect use. Change-Id: I34e6a6c1859251ec99b5cf953e82542025ace56f Reviewed-on: https://go-review.googlesource.com/7526 Reviewed-by: Rob Pike <r@golang.org>
2015-02-24math/big: fix several issues with string->Float conversionRobert Griesemer
Change-Id: I7bf7154e2d8d779fdf7f1d2bb561a06ad174f3b0 Reviewed-on: https://go-review.googlesource.com/4883 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-02-23math/big: fix formatting for 'b' formatRobert Griesemer
Fixes #9939. Change-Id: I9d60722b648fbc00650115da539a7466c6c86552 Reviewed-on: https://go-review.googlesource.com/5640 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-02-23math/big: don't return io.EOF on successful call of ParseFloatRobert Griesemer
Fixes $9938. Change-Id: Ie8680a875225748abd660fb26b4c25546e7b92d3 Reviewed-on: https://go-review.googlesource.com/5620 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-02-06math/big: handling of +/-Inf and zero precision, enable zero valuesRobert Griesemer
- clarified representation of +/-Inf - only 0 and Inf values can have 0 precision - a zero precision value used as result value takes the max precision of the arguments (to be fine-tuned for setters) - the zero precision approach makes Float zero values possible (they represent +0) - more tests Missing: Filling in the blanks. More tests. Change-Id: Ibb4f97e12e1f356c3085ce80f3464e97b82ac130 Reviewed-on: https://go-review.googlesource.com/4000 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-02-05math/big: more Float conversion testsRobert Griesemer
Change-Id: Ia30886569141ca2e0321bea6ee1d5c9e0f79d6f9 Reviewed-on: https://go-review.googlesource.com/3941 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-02-04math/big: first version of Float %e, %f, %g, %G formatting workingRobert Griesemer
Change-Id: I10efa3bc8bc7f41100feabe17837f805a42d7eb6 Reviewed-on: https://go-review.googlesource.com/3842 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-02-04math/big: fix %b format so it matches strconf %b format for non-zero valuesRobert Griesemer
(For zero values the strconv %b format prints the bias-adjusted exponent; there's no bias in Float.) Change-Id: I6f4dda9c3a50d02eac375cfe2c927c1540aae865 Reviewed-on: https://go-review.googlesource.com/3841 Reviewed-by: Alan Donovan <adonovan@google.com>
2015-01-30math/big: split float conversion routines and tests into separate filesRobert Griesemer
No other functional changes. Change-Id: I7e0bb7452c6a265535297ec7ce6a629f1aff695c Reviewed-on: https://go-review.googlesource.com/3674 Reviewed-by: Alan Donovan <adonovan@google.com>