math

package
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 12 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.

Package math provides transcendental math functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	PrimExp  = makeComplexPrimitive("exp", cmplx.Exp)
	PrimSin  = makeComplexPrimitive("sin", cmplx.Sin)
	PrimCos  = makeComplexPrimitive("cos", cmplx.Cos)
	PrimTan  = makeComplexPrimitive("tan", cmplx.Tan)
	PrimAsin = makeComplexPrimitive("asin", cmplx.Asin)
	PrimAcos = makeComplexPrimitive("acos", cmplx.Acos)
)

Unary transcendental primitives (R7RS §6.2.6).

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",
	integerOp:  integerPassthrough,
	floatOp:    math.Ceil,
	rationalOp: rationalToInteger(math.Ceil),
})
View Source
var PrimFloor = makeRealNumberPrimitive(realNumberOp{
	name:       "floor",
	integerOp:  integerPassthrough,
	floatOp:    math.Floor,
	rationalOp: rationalToInteger(math.Floor),
})
View Source
var PrimRound = makeRealNumberPrimitive(realNumberOp{
	name:       "round",
	integerOp:  integerPassthrough,
	floatOp:    math.RoundToEven,
	rationalOp: rationalToInteger(math.RoundToEven),
})
View Source
var PrimTruncate = makeRealNumberPrimitive(realNumberOp{
	name:       "truncate",
	integerOp:  integerPassthrough,
	floatOp:    math.Trunc,
	rationalOp: rationalToInteger(math.Trunc),
})

Functions

func PrimAngle

func PrimAngle(mc machine.CallContext) error

PrimAngle implements the angle primitive.

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 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.

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.

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