math

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package math provides transcendental and advanced mathematical primitives.

Transcendental Functions (R7RS 6.2.6)

  • sin, cos, tan, asin, acos, atan
  • exp, log
  • sqrt, expt

Rounding

  • floor, ceiling, truncate, round

Numeric Conversion

  • numerator, denominator
  • rationalize
  • exact-integer-sqrt

Use Extension or AddToRegistry to register all primitives.

Index

Constants

This section is empty.

Variables

View Source
var (
	PrimSin  = makeComplexPrimitive("sin", cmplx.Sin, values.BigSin, values.BigComplexSin)
	PrimCos  = makeComplexPrimitive("cos", cmplx.Cos, values.BigCos, values.BigComplexCos)
	PrimTan  = makeComplexPrimitive("tan", cmplx.Tan, values.BigTan, values.BigComplexTan)
	PrimAsin = makeComplexPrimitive("asin", cmplx.Asin, values.BigAsin, values.BigComplexAsin)
	PrimAcos = makeComplexPrimitive("acos", cmplx.Acos, values.BigAcos, values.BigComplexAcos)
)

Unary transcendental primitives (R7RS §6.2.6). Big-real kernels are wired in as they land. exp is custom (below) for the bounded-real overflow rescue.

View Source
var AddToRegistry = Builder.AddToRegistry

AddToRegistry registers all math primitives.

View Source
var Builder = registry.NewRegistryBuilder(addPrimitives)

Builder aggregates all math registration functions.

View Source
var Extension = registry.NewDescribedExtension("math",
	"Extended math: trigonometry, logarithms, bitwise operations.",
	AddToRegistry)

Extension is the math extension.

View Source
var PrimCeiling = makeRealNumberPrimitive(realNumberOp{
	name:    "ceiling",
	mode:    roundCeiling,
	floatOp: math.Ceil,
})
View Source
var PrimFloor = makeRealNumberPrimitive(realNumberOp{
	name:    "floor",
	mode:    roundFloor,
	floatOp: math.Floor,
})
View Source
var PrimRound = makeRealNumberPrimitive(realNumberOp{
	name:    "round",
	mode:    roundNearestEven,
	floatOp: math.RoundToEven,
})
View Source
var PrimTruncate = makeRealNumberPrimitive(realNumberOp{
	name:    "truncate",
	mode:    roundTruncate,
	floatOp: math.Trunc,
})

Functions

func PrimAngle

func PrimAngle(mc machine.CallContext) error

PrimAngle implements R7RS §6.2.6 angle.

The five real kinds used to be hand-unrolled here, one arm each, and every arm carried the same three bugs -- which is what a hand-unrolled dispatch buys you: the same mistake, five times, fixed one at a time forever. They ask ONE question, so they get one answer: angleOfReal.

func PrimAtan

func PrimAtan(mc machine.CallContext) error

PrimAtan implements the (atan z) and (atan y x) primitives.

func PrimComplexInexactWithAccuracy added in v1.16.0

func PrimComplexInexactWithAccuracy(mc machine.CallContext) error

PrimComplexInexactWithAccuracy implements (complex-inexact-with-accuracy n) — the uniform 3-value variant.

Always returns (values inexact-c real-acc-sym imag-acc-sym), regardless of whether the input is real or complex. For real input N, the imaginary accuracy is trivially 'exact. Useful when callers want a single arity regardless of input domain.

func PrimDenominator

func PrimDenominator(mc machine.CallContext) error

PrimDenominator implements the (denominator) primitive.

func PrimExactIntegerSqrt

func PrimExactIntegerSqrt(mc machine.CallContext) error

PrimExactIntegerSqrt implements the (exact-integer-sqrt) primitive.

R7RS §6.2.6: Returns two non-negative exact integers s and r where n = s² + r and n < (s+1)².

func PrimExp

func PrimExp(mc machine.CallContext) error

PrimExp implements (exp z). Unlike the other unary transcendentals, exp overflows float64 at ~709 — inside the bounded range — so besides the big-precision path for unbounded-tier operands it also rescues a bounded real operand whose exp over/underflows float64 to a finite BigFloat (e^1000 ≈ 1.97e434 is representable as a bignum even though math.Exp returns +Inf).

func PrimExpt

func PrimExpt(mc machine.CallContext) error

PrimExpt implements the (expt) primitive.

func PrimFiniteQ

func PrimFiniteQ(mc machine.CallContext) error

PrimFiniteQ implements the (finite?) primitive.

func PrimFloorDiv

func PrimFloorDiv(mc machine.CallContext) error

PrimFloorDiv implements the (floor/) primitive.

R7RS §6.2.6: Returns two values: floor quotient and floor remainder.

func PrimFloorQuotient

func PrimFloorQuotient(mc machine.CallContext) error

PrimFloorQuotient implements the (floor-quotient) primitive.

R7RS §6.2.6: Returns the floor quotient for any real numbers.

func PrimFloorRemainder

func PrimFloorRemainder(mc machine.CallContext) error

PrimFloorRemainder implements the (floor-remainder) primitive.

R7RS §6.2.6: Returns the floor remainder for any real numbers.

func PrimImagPart

func PrimImagPart(mc machine.CallContext) error

PrimImagPart implements the (imag-part) primitive.

func PrimInexactAccuracy added in v1.16.0

func PrimInexactAccuracy(mc machine.CallContext) error

PrimInexactAccuracy implements (inexact-accuracy n).

Polymorphic return shape: real input → one symbol; complex input → two symbols via mc.SetValues. R7RS-style multi-value protocol.

func PrimInexactLosslessQ added in v1.16.0

func PrimInexactLosslessQ(mc machine.CallContext) error

PrimInexactLosslessQ implements (inexact-lossless? n).

Returns #t iff (exact->inexact n) would lose no information. For real input, this means the float64 conversion is big.Exact. For complex input, BOTH real and imaginary components must be big.Exact. (For real-only inputs the imaginary accuracy is trivially big.Exact, so the complex predicate collapses to the real-part check — hence the unified ToComplex128WithAccuracy path.)

func PrimInexactWithAccuracy added in v1.16.0

func PrimInexactWithAccuracy(mc machine.CallContext) error

PrimInexactWithAccuracy implements (inexact-with-accuracy n).

Polymorphic return: real input → (values inexact-n acc-sym); complex input → (values inexact-c real-acc-sym imag-acc-sym). The inexact value is the actual float64 / complex128 produced by the conversion; the accuracy symbols indicate the rounding direction.

func PrimInfiniteQ

func PrimInfiniteQ(mc machine.CallContext) error

PrimInfiniteQ implements the (infinite?) primitive.

func PrimLog

func PrimLog(mc machine.CallContext) error

PrimLog implements the (log z) and (log z1 z2) primitives. A positive, unbounded-tier real argument takes the big-precision log (so a value beyond the float64 range no longer overflows its input to +Inf); non-positive or complex arguments keep the branch-correct complex path.

func PrimMagnitude

func PrimMagnitude(mc machine.CallContext) error

PrimMagnitude implements the (magnitude) primitive.

func PrimMakePolar

func PrimMakePolar(mc machine.CallContext) error

PrimMakePolar implements the (make-polar) primitive.

func PrimMakeRectangular

func PrimMakeRectangular(mc machine.CallContext) error

PrimMakeRectangular implements make-rectangular. R7RS §6.2.6: If both arguments are exact, the result is exact.

func PrimNanQ

func PrimNanQ(mc machine.CallContext) error

PrimNanQ implements the nan? primitive.

func PrimNumberToString

func PrimNumberToString(mc machine.CallContext) error

PrimNumberToString implements the number->string primitive.

Every result is a MUTABLE string. R7RS §3.4's storage model makes a newly allocated object's locations mutable unless a procedure's own description says otherwise, and number->string has no such clause — unlike command-line and the get-environment-variable family (§6.14, "it is an error to mutate any of these strings"), which correctly refuse.

values.NewString defaults to immutable, so mutability is not the absence of a flag here: an allocator that wants it must call NewMutableString explicitly. That polarity is why every one of these arms was wrong the same way.

func PrimNumerator

func PrimNumerator(mc machine.CallContext) error

PrimNumerator implements the numerator primitive.

func PrimRationalize

func PrimRationalize(mc machine.CallContext) error

PrimRationalize implements the (rationalize) primitive.

func PrimRealPart

func PrimRealPart(mc machine.CallContext) error

PrimRealPart implements the (real-part) primitive.

func PrimSqrt

func PrimSqrt(mc machine.CallContext) error

PrimSqrt implements the (sqrt) primitive.

R7RS §6.2.6: The branch cut for sqrt lies along the negative real axis, continuous with quadrant II. This means for values on the negative real axis (including those with -0.0 imaginary part), sqrt returns positive imaginary.

func PrimStringToNumber

func PrimStringToNumber(mc machine.CallContext) error

PrimStringToNumber implements the string->number primitive.

R7RS §6.2.7: string->number returns a number of the maximally precise representation expressed by the given string. It is an error if radix is not 2, 8, 10, or 16.

R7RS §7.1.1: The string may contain prefix directives #b, #o, #d, #x (radix) and #e, #i (exactness), in either order, up to one of each. A radix prefix in the string overrides the radix argument.

func PrimTruncateDiv

func PrimTruncateDiv(mc machine.CallContext) error

PrimTruncateDiv implements the truncate/ primitive.

R7RS §6.2.6: Returns two values: truncate quotient and truncate remainder.

func PrimTruncateQuotient

func PrimTruncateQuotient(mc machine.CallContext) error

PrimTruncateQuotient implements the truncate-quotient primitive.

R7RS §6.2.6: Returns the truncate quotient for any real numbers.

func PrimTruncateRemainder

func PrimTruncateRemainder(mc machine.CallContext) error

PrimTruncateRemainder implements the truncate-remainder primitive.

R7RS §6.2.6: Returns the truncate remainder for any real numbers.

Types

This section is empty.

Jump to

Keyboard shortcuts

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