constructive

package
v0.0.0-...-c99f163 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const IntSize = 32 << (^uint(0) >> 63) // 32 or 64

Variables

View Source
var E = sync.OnceValue(func() Real {
	return newNamed("e", newPrescaledExponential(FromInt(1)))
})

E calculates the mathematical constant e using its Taylor series expansion.

View Source
var ErrNotConstructive = errors.New("not constructive")
View Source
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)

View Source
var One = sync.OnceValue(func() Real {
	return newNamed("1", FromInt(1))
})

One represents the constant 1.

View Source
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

View Source
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))

View Source
var PrecisionOverflow error = precisionOverflowError{}
View Source
var Sqrt2 = sync.OnceValue(func() Real {
	return newNamed("√2", Sqrt(FromInt(2)))
})

Sqrt2 calculates the square root of 2.

View Source
var Ten = sync.OnceValue(func() Real {
	return newNamed("10", FromInt(10))
})

Ten represents the constant 10.

View Source
var Two = sync.OnceValue(func() Real {
	return newNamed("2", FromInt(2))
})

Two represents the constant 2.

View Source
var Zero = sync.OnceValue(func() Real {
	return newNamed("0", FromInt(0))
})

Zero represents the constant 0.

Functions

func Approximate

func Approximate(c Real, p int) *big.Int

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

func AsConstruction(c Real) string

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

func AsConstructionIndent(c Real, indent string) string

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 CheckPrecisionOverflow

func CheckPrecisionOverflow(ctx context.Context, p int) error

func Cmp

func Cmp(a, b Real) int

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

func ConstructiveName(c Real) (string, bool)

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 Identify

func Identify(c Real) (*big.Int, bool, error)

func IsIntWithinBitTolerance

func IsIntWithinBitTolerance(value, tolerance int) bool

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

func IsPrecisionValid(p int) bool

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

func PreciseCmp(a, b Real, p int) int

PreciseCmp compares two Real numbers a and b with a precision p.

func PreciseSign

func PreciseSign(c Real, p int) int

PreciseSign computes the sign of a Real number c given precision p.

func PrecisionLimit

func PrecisionLimit(ctx context.Context) (int, bool)

func Pretty

func Pretty(c Real) string

func Sign

func Sign(c Real) int

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

func Text(c Real, dec, radix int) (text string)

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

func WithPrecisionLimit(parent context.Context, limit int) context.Context

func WithoutPrecisionLimit

func WithoutPrecisionLimit(parent context.Context) context.Context

Types

type Real

type Real interface {
	// contains filtered or unexported methods
}

Real represents a constructive real number.

func Abs

func Abs(c Real) Real

Abs computes the absolute value of c.

func Add

func Add(a, b Real) Real

Add computes the addition `a + b`.

func ContinuedFraction

func ContinuedFraction(fracs []Real) Real

ContinuedFraction computes the continued fraction from the given slice of constructive Real values.

func ContinuedFraction64

func ContinuedFraction64(fracs []int64) Real

ContinuedFraction64 computes the continued fraction from the given slice of int64 values.

func Cosine

func Cosine(c Real) Real

Cosine computes the cosine of c.

func DegreesToRadians

func DegreesToRadians(degrees Real) Real

func Divide

func Divide(a, b Real) Real

Divide computes the division `a * (1/b)`, where `1/b` is the multiplicative inverse of b.

func Exp

func Exp(c Real) Real

Exp computes the e^c.

func FromBigInt

func FromBigInt(i *big.Int) Real

FromBigInt creates a Real number from a big.Int.

func FromBigIntSlice

func FromBigIntSlice(ints []*big.Int) []Real

FromBigIntSlice creates a slice of Real numbers from a slice of big.Int.

func FromFloat32

func FromFloat32(f float32) Real

FromFloat32 creates a Real number from a float32.

func FromFloat32Slice

func FromFloat32Slice(floats []float32) []Real

FromFloat32Slice creates a slice of Real numbers from a slice of float32.

func FromFloat64

func FromFloat64(f float64) Real

FromFloat64 creates a Real number from a float64.

func FromFloat64Slice

func FromFloat64Slice(floats []float64) []Real

FromFloat64Slice creates a slice of Real numbers from a slice of float64.

func FromInt

func FromInt(i int) Real

FromInt creates a Real number from an int.

func FromInt64

func FromInt64(i int64) Real

FromInt64 creates a Real number from an int64.

func FromInt64Slice

func FromInt64Slice(ints []int64) []Real

FromInt64Slice creates a slice of Real numbers from a slice of int64.

func FromIntSlice

func FromIntSlice(ints []int) []Real

FromIntSlice creates a slice of Real numbers from a slice of int.

func FromRat

func FromRat(a, b int) Real

FromRat creates a Real number from a rational number a/b, where b != 0.

func Inverse

func Inverse(c Real) Real

Inverse computes the multiplicative inverse, which is 1/c.

func Ln

func Ln(c Real) Real

func Max

func Max(a, b Real) Real

Max computes the maximum of a and b.

func Min

func Min(a, b Real) Real

Min computes the minimum of a and b.

func Multiply

func Multiply(a, b Real) Real

Multiply computes the multiplication `a * b`.

func Negate

func Negate(c Real) Real

Negate computes the negation `-c`. The approximation is actually `-Approximate(c, p)`.

func Pow

func Pow(c, n Real) Real

Pow computes the power c^n.

func Pow10

func Pow10(n Real) Real

Pow10 computes the power 10^n.

func RadiansToDegrees

func RadiansToDegrees(radians Real) Real

func ShiftLeft

func ShiftLeft(c Real, n int) Real

ShiftLeft computes the left shift `c * 2^n`.

func ShiftRight

func ShiftRight(c Real, n int) Real

ShiftRight computes the right shift `c * 2^-n`.

func SimpleLn

func SimpleLn(c Real) Real

SimpleLn computes the natural logarithm of `c`, for `1 < |c| < 2`.

func Sine

func Sine(c Real) Real

Sine computes the sine of c, using the identity `sin(c) = cos(π/2 - c)`.

func Sqrt

func Sqrt(c Real) Real

Sqrt computes the square root of c.

func Square

func Square(c Real) Real

Square computes the square `c * c`.

func Subtract

func Subtract(a, b Real) Real

Subtract computes the subtraction `a + (-b)`.

func Tangent

func Tangent(c Real) Real

Tangent computes the tangent of c, using the identity `tan(c) = sin(c) / cos(c)`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL