diff options
author | Russ Cox <rsc@golang.org> | 2009-03-05 15:29:04 -0800 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-03-05 15:29:04 -0800 |
commit | e9ea1aa25a20daad0b07e6faf9f5a2e3ba456b0b (patch) | |
tree | a07fcb35311b83c6fe99e2c4fbbff7842de7e144 /src/lib/strconv/atof.go | |
parent | 1c3c163bc5a7eddec1fa2fb3131e14b1c103d74a (diff) | |
download | golang-e9ea1aa25a20daad0b07e6faf9f5a2e3ba456b0b.tar.gz |
strconv: doc
R=r
DELTA=110 (64 added, 19 deleted, 27 changed)
OCL=25761
CL=25782
Diffstat (limited to 'src/lib/strconv/atof.go')
-rw-r--r-- | src/lib/strconv/atof.go | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/src/lib/strconv/atof.go b/src/lib/strconv/atof.go index 358594416..ec94b7c74 100644 --- a/src/lib/strconv/atof.go +++ b/src/lib/strconv/atof.go @@ -8,6 +8,8 @@ // 2) Multiply/divide decimal by powers of two until in range [0.5, 1) // 3) Multiply by 2^precision and round to get mantissa. +// The strconv package implements conversions to and from +// string representations of basic data types. package strconv import ( @@ -308,53 +310,57 @@ func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) { return; } -// Convert string s to floating-point number. +// Atof32 converts the string s to a 32-bit floating-point number. // // If s is well-formed and near a valid floating point number, -// returns f, false, true, where f is the nearest floating point -// number rounded using IEEE754 unbiased rounding. +// Atof32 returns the nearest floating point number rounded +// using IEEE754 unbiased rounding. // -// If s is not syntactically well-formed, returns err = os.EINVAL. +// If s is not syntactically well-formed, Atof32 returns err = os.EINVAL. // // If s is syntactically well-formed but is more than 1/2 ULP // away from the largest floating point number of the given size, -// returns f = ±Inf, err = os.ERANGE. -func Atof64(s string) (f float64, err *os.Error) { +// Atof32 returns f = ±Inf, err = os.ERANGE. +func Atof32(s string) (f float32, err *os.Error) { neg, d, trunc, ok := stringToDecimal(s); if !ok { return 0, os.EINVAL; } if optimize { - if f, ok := decimalAtof64(neg, d, trunc); ok { + if f, ok := decimalAtof32(neg, d, trunc); ok { return f, nil; } } - b, ovf := decimalToFloatBits(neg, d, trunc, &float64info); - f = math.Float64frombits(b); + b, ovf := decimalToFloatBits(neg, d, trunc, &float32info); + f = math.Float32frombits(uint32(b)); if ovf { err = os.ERANGE; } return f, err } -func Atof32(s string) (f float32, err *os.Error) { +// Atof64 converts the string s to a 64-bit floating-point number. +// Except for the type of its result, its definition is the same as that +// of Atof32. +func Atof64(s string) (f float64, err *os.Error) { neg, d, trunc, ok := stringToDecimal(s); if !ok { return 0, os.EINVAL; } if optimize { - if f, ok := decimalAtof32(neg, d, trunc); ok { + if f, ok := decimalAtof64(neg, d, trunc); ok { return f, nil; } } - b, ovf := decimalToFloatBits(neg, d, trunc, &float32info); - f = math.Float32frombits(uint32(b)); + b, ovf := decimalToFloatBits(neg, d, trunc, &float64info); + f = math.Float64frombits(b); if ovf { err = os.ERANGE; } return f, err } +// Atof is like Atof32 or Atof64, depending on the size of float. func Atof(s string) (f float, err *os.Error) { if FloatSize == 32 { f1, err1 := Atof32(s); |