Documentation ¶
Overview ¶
Package rat128 provides fixed-precision rational numbers. See the N type and New function for details.
Index ¶
- Variables
- func ExtGCD(m, n int64) (a, b, d int64)
- func GCD(m, n int64) int64
- type N
- func (x N) Abs() N
- func (x N) Add(y N) N
- func (x N) BigRat() *big.Rat
- func (x N) Cmp(y N) int
- func (x N) DecimalString(prec int) string
- func (x N) Den() int64
- func (x N) Div(y N) N
- func (x N) Float64() (v float64, exact bool)
- func (x N) Inv() N
- func (x N) IsValid() bool
- func (x N) IsZero() bool
- func (x N) Mul(y N) N
- func (x N) Neg() N
- func (x N) Num() int64
- func (x N) RationalString(sep string) string
- func (x N) Sign() int
- func (x N) String() string
- func (x N) Sub(y N) N
- func (x N) TryAdd(y N) (N, error)
- func (x N) TryDiv(y N) (N, error)
- func (x N) TryInv() (N, error)
- func (x N) TryMul(y N) (N, error)
- func (x N) TrySub(y N) (N, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrDenInvalid = errors.New("denominator is not positive") ErrDenOverflow = errors.New("denominator overflow") ErrNumOverflow = errors.New("numerator overflow") ErrDivByZero = errors.New("division by zero") ErrFmtInvalid = errors.New("invalid number format") )
Common errors returned by functions in this package.
Functions ¶
Types ¶
type N ¶
type N struct {
// contains filtered or unexported fields
}
N is a rational number with 64-bit numerator and denominator.
One bit of the numerator is used for the sign and the denominator must be positive, so only 63 bits of precision are actually available in each. Internally, the denominator is biased by 1, which means the zero value is equivalent to 0/1 and thus valid and equal to 0. Due to the asymmetry of the int64 type (|math.MinInt64| > math.MaxInt64), math.MinInt64 is not a valid numerator in reduced form.
Valid values are obtained in the following ways:
- the zero value of the type N
- returned by the New or Try functions (with non-nil error)
- returned by arithmetic on any valid values (with non-nil error)
- copied from a valid value
N has proper value semantics and its values can be freely copied. Two valid values of N can be compared using the == and != operators.
func FromBigRat ¶
FromBigRat converts a big.Rat to N, if it is possible to do so.
func FromFloat64 ¶
FromFloat64 extracts a rational number from a float64. The result will be exactly equal to v, or else an error will be returned.
func New ¶
New is like Try but panics instead of returning an error.
Example ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { n := rat128.New(1, 2) fmt.Println(n) }
Output: 1/2
func ParseDecimalString ¶
ParseDecimalString parses a string representation of a decimal number as a rational number. The string must be in the form "A", "A.B", or ".B" where A is an integer that may have leading zeroes and may be negative (indicated with leading hyphen) and B is an integer that may have trailing zeroes. The concatenation of A without leading zeroes and B without trailing zeroes must not overflow int64.
func ParseRationalString ¶
ParseRationalString parses a string representation of a rational number. The string must be in the form "m/n", where m and n are integers in base 10, n is not zero, and only m may be negative (indicated with leading hyphen). It is not necessary for m/n to be in lowest terms, but the result will be. Also, m and n cannot overflow int64.
Example (DenomZero) ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { _, err := rat128.ParseRationalString("1/0") fmt.Println(err) }
Output: denominator is not positive
Example (NegOneHalf) ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { n, err := rat128.ParseRationalString("-1/2") if err != nil { panic(err) } fmt.Println(n) }
Output: -1/2
Example (OneHalf) ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { n, err := rat128.ParseRationalString("1/2") if err != nil { panic(err) } fmt.Println(n) }
Output: 1/2
Example (TwoFourths) ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { n, err := rat128.ParseRationalString("2/4") if err != nil { panic(err) } fmt.Println(n) }
Output: 1/2
func Try ¶
Try creates a new rational number with the given numerator and denominator. Try returns an error if the reduced numerator is math.MinInt64 or if the denominator is not positive.
Example ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { n, err := rat128.Try(1, 2) if err != nil { panic(err) } fmt.Println(n) }
Output: 1/2
Example (DenomZero) ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { _, err := rat128.Try(1, 0) fmt.Println(err) }
Output: denominator is not positive
func (N) Add ¶
Add adds x and y and returns the result. Add panics if the result would overflow.
Example ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { x := rat128.New(1, 2) y := rat128.New(1, 3) z := x.Add(y) fmt.Println(z) }
Output: 5/6
func (N) DecimalString ¶
DecimalString returns a string representation of x, as a decimal number to the given number of digits after the decimal point. The last digit is rounded to nearest, with ties rounded away from zero. If prec <= 0, the decimal point is omitted from the string. If the result of rounding is zero but x is negative, the string will still include a negative sign.
The following relation should hold for all valid values of x:
x.DecimalString(prec) == x.BigRat().FloatString(prec)
func (N) Div ¶
Div divides x by y and returns the result. The following are equivalent in outcome and behavior:
x.Div(y) == x.Mul(y.Inv())
Example ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { x := rat128.New(1, 2) y := rat128.New(2, 3) z := x.Div(y) fmt.Println(z) }
Output: 3/4
func (N) Float64 ¶
Float64 returns the floating-point equivalent of x. If exact is true, then v is exactly equal to x; otherwise, it is the closest approximation.
func (N) IsValid ¶
IsValid returns true if x is a valid rational number. Invalid numbers do not arise under normal circumstances, but may occur if a value is constructed or manipulated using unsafe operations.
func (N) Mul ¶
Mul multiplies x and y and returns the result. Mul panics if the result would overflow.
Example ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { x := rat128.New(1, 2) y := rat128.New(2, 3) z := x.Mul(y) fmt.Println(z) }
Output: 1/3
func (N) RationalString ¶
RationalString returns a string representation of x, as m+sep+n. For example, x.String() is equivalent to x.RationalString("/").
func (N) Sub ¶
Sub subtracts y from x and returns the result. The following are equivalent in outcome and behavior:
x.Sub(y) == x.Add(y.Neg())
Example ¶
package main import ( "fmt" "github.com/kbolino/rat128" ) func main() { x := rat128.New(1, 2) y := rat128.New(1, 3) z := x.Sub(y) fmt.Println(z) }
Output: 1/6
func (N) TryAdd ¶
TryAdd adds x and y and returns the result. TryAdd returns 0 and a non-nil error if the result would overflow.
func (N) TryDiv ¶
TryDiv divides x by y and returns the result. TryDiv returns 0 and a non-nil error for division by zero or if the result would overflow.
func (N) TryInv ¶
TryInv returns the inverse of x, 1/x. If x.Num() == 0, TryInv returns (0, ErrDivByZero).