var optimize … // commonPrefixLenIgnoreCase returns the length of the common // prefix of s and prefix, with the character case of s ignored. // The prefix argument must be all lower-case. func commonPrefixLenIgnoreCase(s, prefix string) int { … } // special returns the floating-point value for the special, // possibly signed floating-point representations inf, infinity, // and NaN. The result is ok if a prefix of s contains one // of these representations and n is the length of that prefix. // The character case is ignored. func special(s string) (f float64, n int, ok bool) { … } func (b *decimal) set(s string) (ok bool) { … } // readFloat reads a decimal or hexadecimal mantissa and exponent from a float // string representation in s; the number may be followed by other characters. // readFloat reports the number of bytes consumed (i), and whether the number // is valid (ok). func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex bool, i int, ok bool) { … } var powtab … func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) { … } var float64pow10 … var float32pow10 … // If possible to convert decimal representation to 64-bit float f exactly, // entirely in floating-point math, do so, avoiding the expense of decimalToFloatBits. // Three common cases: // // value is exact integer // value is exact integer * exact power of ten // value is exact integer / exact power of ten // // These all produce potentially inexact but correctly rounded answers. func atof64exact(mantissa uint64, exp int, neg bool) (f float64, ok bool) { … } // If possible to compute mantissa*10^exp to 32-bit float f exactly, // entirely in floating-point math, do so, avoiding the machinery above. func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) { … } // atofHex converts the hex floating-point string s // to a rounded float32 or float64 value (depending on flt==&float32info or flt==&float64info) // and returns it as a float64. // The string s has already been parsed into a mantissa, exponent, and sign (neg==true for negative). // If trunc is true, trailing non-zero bits have been omitted from the mantissa. func atofHex(s string, flt *floatInfo, mantissa uint64, exp int, neg, trunc bool) (float64, error) { … } const fnParseFloat … func atof32(s string) (f float32, n int, err error) { … } func atof64(s string) (f float64, n int, err error) { … } // ParseFloat converts the string s to a floating-point number // with the precision specified by bitSize: 32 for float32, or 64 for float64. // When bitSize=32, the result still has type float64, but it will be // convertible to float32 without changing its value. // // ParseFloat accepts decimal and hexadecimal floating-point numbers // as defined by the Go syntax for [floating-point literals]. // If s is well-formed and near a valid floating-point number, // ParseFloat returns the nearest floating-point number rounded // using IEEE754 unbiased rounding. // (Parsing a hexadecimal floating-point value only rounds when // there are more bits in the hexadecimal representation than // will fit in the mantissa.) // // The errors that ParseFloat returns have concrete type *NumError // and include err.Num = s. // // If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax. // // If s is syntactically well-formed but is more than 1/2 ULP // away from the largest floating point number of the given size, // ParseFloat returns f = ±Inf, err.Err = ErrRange. // // ParseFloat recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity" // as their respective special floating point values. It ignores case when matching. // // [floating-point literals]: https://go.dev/ref/spec#Floating-point_literals func ParseFloat(s string, bitSize int) (float64, error) { … } func parseFloatPrefix(s string, bitSize int) (float64, int, error) { … }