func ratTok(ch rune) bool { … } var ratZero … var _ … // Scan is a support routine for fmt.Scanner. It accepts the formats // 'e', 'E', 'f', 'F', 'g', 'G', and 'v'. All formats are equivalent. func (z *Rat) Scan(s fmt.ScanState, ch rune) error { … } // SetString sets z to the value of s and returns z and a boolean indicating // success. s can be given as a (possibly signed) fraction "a/b", or as a // floating-point number optionally followed by an exponent. // If a fraction is provided, both the dividend and the divisor may be a // decimal integer or independently use a prefix of “0b”, “0” or “0o”, // or “0x” (or their upper-case variants) to denote a binary, octal, or // hexadecimal integer, respectively. The divisor may not be signed. // If a floating-point number is provided, it may be in decimal form or // use any of the same prefixes as above but for “0” to denote a non-decimal // mantissa. A leading “0” is considered a decimal leading 0; it does not // indicate octal representation in this case. // An optional base-10 “e” or base-2 “p” (or their upper-case variants) // exponent may be provided as well, except for hexadecimal floats which // only accept an (optional) “p” exponent (because an “e” or “E” cannot // be distinguished from a mantissa digit). If the exponent's absolute value // is too large, the operation may fail. // The entire string, not just a prefix, must be valid for success. If the // operation failed, the value of z is undefined but the returned value is nil. func (z *Rat) SetString(s string) (*Rat, bool) { … } // scanExponent scans the longest possible prefix of r representing a base 10 // (“e”, “E”) or a base 2 (“p”, “P”) exponent, if any. It returns the // exponent, the exponent base (10 or 2), or a read or syntax error, if any. // // If sepOk is set, an underscore character “_” may appear between successive // exponent digits; such underscores do not change the value of the exponent. // Incorrect placement of underscores is reported as an error if there are no // other errors. If sepOk is not set, underscores are not recognized and thus // terminate scanning like any other character that is not a valid digit. // // exponent = ( "e" | "E" | "p" | "P" ) [ sign ] digits . // sign = "+" | "-" . // digits = digit { [ '_' ] digit } . // digit = "0" ... "9" . // // A base 2 exponent is only permitted if base2ok is set. func scanExponent(r io.ByteScanner, base2ok, sepOk bool) (exp int64, base int, err error) { … } // String returns a string representation of x in the form "a/b" (even if b == 1). func (x *Rat) String() string { … } // marshal implements [Rat.String] returning a slice of bytes. // It appends the string representation of x in the form "a/b" (even if b == 1) to buf, // and returns the extended buffer. func (x *Rat) marshal(buf []byte) []byte { … } // RatString returns a string representation of x in the form "a/b" if b != 1, // and in the form "a" if b == 1. func (x *Rat) RatString() string { … } // FloatString returns a string representation of x in decimal form with prec // digits of precision after the radix point. The last digit is rounded to // nearest, with halves rounded away from zero. func (x *Rat) FloatString(prec int) string { … } // FloatPrec returns the number n of non-repeating digits immediately // following the decimal point of the decimal representation of x. // The boolean result indicates whether a decimal representation of x // with that many fractional digits is exact or rounded. // // Examples: // // x n exact decimal representation n fractional digits // 0 0 true 0 // 1 0 true 1 // 1/2 1 true 0.5 // 1/3 0 false 0 (0.333... rounded) // 1/4 2 true 0.25 // 1/6 1 false 0.2 (0.166... rounded) func (x *Rat) FloatPrec() (n int, exact bool) { … }