Documentation
¶
Index ¶
- Constants
- Variables
- func Approximate(c Real, p int) *big.Int
- func AsConstruction(c Real) string
- func AsConstructionIndent(c Real, indent string) string
- func CheckPrecisionOverflow(ctx context.Context, p int) error
- func Cmp(a, b Real) int
- func ConstructiveName(c Real) (string, bool)
- func Identify(c Real) (*big.Int, bool, error)
- func IsIntWithinBitTolerance(value, tolerance int) bool
- func IsPrecisionValid(p int) bool
- func PreciseCmp(a, b Real, p int) int
- func PreciseSign(c Real, p int) int
- func PrecisionLimit(ctx context.Context) (int, bool)
- func Pretty(c Real) string
- func Sign(c Real) int
- func Text(c Real, dec, radix int) (text string)
- func WithPrecisionLimit(parent context.Context, limit int) context.Context
- func WithoutPrecisionLimit(parent context.Context) context.Context
- type Real
- func Abs(c Real) Real
- func Add(a, b Real) Real
- func ContinuedFraction(fracs []Real) Real
- func ContinuedFraction64(fracs []int64) Real
- func Cosine(c Real) Real
- func DegreesToRadians(degrees Real) Real
- func Divide(a, b Real) Real
- func Exp(c Real) Real
- func FromBigInt(i *big.Int) Real
- func FromBigIntSlice(ints []*big.Int) []Real
- func FromFloat32(f float32) Real
- func FromFloat32Slice(floats []float32) []Real
- func FromFloat64(f float64) Real
- func FromFloat64Slice(floats []float64) []Real
- func FromInt(i int) Real
- func FromInt64(i int64) Real
- func FromInt64Slice(ints []int64) []Real
- func FromIntSlice(ints []int) []Real
- func FromRat(a, b int) Real
- func Inverse(c Real) Real
- func Ln(c Real) Real
- func Max(a, b Real) Real
- func Min(a, b Real) Real
- func Multiply(a, b Real) Real
- func Negate(c Real) Real
- func Pow(c, n Real) Real
- func Pow10(n Real) Real
- func RadiansToDegrees(radians Real) Real
- func ShiftLeft(c Real, n int) Real
- func ShiftRight(c Real, n int) Real
- func SimpleLn(c Real) Real
- func Sine(c Real) Real
- func Sqrt(c Real) Real
- func Square(c Real) Real
- func Subtract(a, b Real) Real
- func Tangent(c Real) Real
Constants ¶
const IntSize = 32 << (^uint(0) >> 63) // 32 or 64
Variables ¶
var E = sync.OnceValue(func() Real { return newNamed("e", newPrescaledExponential(FromInt(1))) })
E calculates the mathematical constant e using its Taylor series expansion.
var ErrNotConstructive = errors.New("not constructive")
var Ln2 = sync.OnceValue(func() Real { t1 := Multiply(FromInt(7), SimpleLn(Divide(FromInt(10), FromInt(9)))) t2 := Multiply(FromInt(2), SimpleLn(Divide(FromInt(25), FromInt(24)))) t3 := Multiply(FromInt(3), SimpleLn(Divide(FromInt(81), FromInt(80)))) return newNamed("ln2", Add(Subtract(t1, t2), t3)) })
Ln2 calculates ln(2) using the formula: ln(2) = 7*ln(10/9) - 2*ln(25/24) + 3*ln(81/80)
var One = sync.OnceValue(func() Real { return newNamed("1", FromInt(1)) })
One represents the constant 1.
var Phi = sync.OnceValue(func() Real { return newNamed("φ", Divide(Add(FromInt(1), Sqrt(FromInt(5))), FromInt(2))) })
Phi calculates the golden ratio: φ = (1 + √5) / 2
var Pi = sync.OnceValue(func() Real { m1 := Multiply(FromInt(6), newIntegralArctan(FromInt(8))) m2 := Multiply(FromInt(2), newIntegralArctan(FromInt(57))) m3 := newIntegralArctan(FromInt(239)) return newNamed("π", Multiply(FromInt(4), Add(m1, Add(m2, m3)))) })
Pi calculates π using the Machin-like formula: π = 4 * (6 * arctan(1/8) + 2 * arctan(1/57) + arctan(1/239))
var PrecisionOverflow error = precisionOverflowError{}
Sqrt2 calculates the square root of 2.
var Ten = sync.OnceValue(func() Real { return newNamed("10", FromInt(10)) })
Ten represents the constant 10.
var Two = sync.OnceValue(func() Real { return newNamed("2", FromInt(2)) })
Two represents the constant 2.
var Zero = sync.OnceValue(func() Real { return newNamed("0", FromInt(0)) })
Zero represents the constant 0.
Functions ¶
func Approximate ¶
Approximate computes the approximation of a Real number, given a precision p. When possible, the approximation is cached to save time on future calls.
func AsConstruction ¶
AsConstruction returns a string representing the construction of the Real number c, which may provide insight into how the number is constructed. The construction is returned as a single line string.
func AsConstructionIndent ¶
AsConstructionIndent returns a string representing the construction of the Real number c, which may provide insight into how the number is constructed.
When indent is empty, the construction is returned as a single line string.
Otherwise, every opening parenthesis increases the indentation level by one, and every closing parenthesis decreases it by one. Arguments are separated by commas, and each argument starts on a new line with the appropriate indentation.
func Cmp ¶
Cmp compares two Real numbers a and b with higher and higher precision until a non-zero result is found. It returns 1 if `a > b`, -1 if `a < b`.
This function never terminates if `a == b`; use PreciseCmp instead.
func ConstructiveName ¶
ConstructiveName returns the name of the constructive Real number c, if it has one. The second return value indicates whether a name was found.
func IsIntWithinBitTolerance ¶
IsIntWithinBitTolerance checks if the integer value is within the bit tolerance. A bit tolerance of 4 means that there must be at least 4 bits unused in the integer representation.
func IsPrecisionValid ¶
IsPrecisionValid checks if the precision is within 4 bits of tolerance. That value depends on the size of the integer on our architecture, 32 or 64.
func PreciseCmp ¶
PreciseCmp compares two Real numbers a and b with a precision p.
func PreciseSign ¶
PreciseSign computes the sign of a Real number c given precision p.
func Sign ¶
Sign computes the sign of a Real number c. It returns 1 if c > 0, or -1 if c < 0.
This function never terminates if c == 0; use PreciseSign instead.
func Text ¶
Text converts a Real number to a string representation. The function takes a Real number, a non-negative decimal precision, and a radix (base) for the conversion.
func WithPrecisionLimit ¶
Types ¶
type Real ¶
type Real interface {
// contains filtered or unexported methods
}
Real represents a constructive real number.
func ContinuedFraction ¶
ContinuedFraction computes the continued fraction from the given slice of constructive Real values.
func ContinuedFraction64 ¶
ContinuedFraction64 computes the continued fraction from the given slice of int64 values.
func DegreesToRadians ¶
func Divide ¶
Divide computes the division `a * (1/b)`, where `1/b` is the multiplicative inverse of b.
func FromBigIntSlice ¶
FromBigIntSlice creates a slice of Real numbers from a slice of big.Int.
func FromFloat32 ¶
FromFloat32 creates a Real number from a float32.
func FromFloat32Slice ¶
FromFloat32Slice creates a slice of Real numbers from a slice of float32.
func FromFloat64 ¶
FromFloat64 creates a Real number from a float64.
func FromFloat64Slice ¶
FromFloat64Slice creates a slice of Real numbers from a slice of float64.
func FromInt64Slice ¶
FromInt64Slice creates a slice of Real numbers from a slice of int64.
func FromIntSlice ¶
FromIntSlice creates a slice of Real numbers from a slice of int.
func Negate ¶
Negate computes the negation `-c`. The approximation is actually `-Approximate(c, p)`.
func RadiansToDegrees ¶
func ShiftRight ¶
ShiftRight computes the right shift `c * 2^-n`.