algolib.numerics package

Submodules

algolib.numerics.constants module

Centralized numerical constants for algolib.

  • No imports from math or third-party libs: pure Python floats only.

  • Values are given either as exact decimal, or with comments showing hex-float for traceability.

  • Includes Cody–Waite style splits for stable range-reduction in exp/sin/cos.

algolib.numerics.constants.copysign1(x: float, y: float) float[source]

Return :math:\abs{x} with the sign of y. No math module, handles ±0.0 and NaN.

algolib.numerics.constants.pow2_int(k: int) float[source]

Compute 2**k using only multiplies (supports negative k) with IEEE754-style bounds.

Behavior:
  • k > 1023 -> raise NumericOverflowError (subclass of OverflowError)

  • k < -1074 -> 0.0 (underflow to zero)

  • -1074 <= k <= -1023 -> exact subnormal via halving from DBL_MIN

  • otherwise -> exponentiation-by-squaring (<= 1023 以及 >= -1022)

algolib.numerics.diff module

algolib.numerics.diff.derivative_central(f: Callable[[float], float], x: float, *, h: float | None = None, max_iter: int = 6) float[source]

Central difference derivative approximation with Richardson extrapolation.

Useful when f does not support complex input. Accuracy is O(h^2) per step and improved by extrapolation.

algolib.numerics.diff.derivative_cstep(f: Callable[[complex], complex], x: float, h: float = 1e-20) float[source]

Complex-step derivative approximation:

f’(x) ≈ Im(f(x + i*h)) / h

Requires f to support complex input. Very stable since there is no subtractive cancellation.

algolib.numerics.trig module

algolib.numerics.trig.cos(x: Any) float[source]

Cosine of an angle (system backend).

Parameters:

x (float) – Input angle in radians.

Returns:

cos(x) evaluated by the active numerics backend.

Return type:

float

algolib.numerics.trig.sin(x: Any) float[source]

Sine of an angle (system backend).

Parameters:

x (float) – Input angle in radians.

Returns:

sin(x) evaluated by the active numerics backend.

Return type:

float

algolib.numerics.trig.tan(x: Any) float[source]

Tangent of an angle (system backend).

Notes

Argument-reduction and non-finite handling are performed inside the active backend (see _backend.SystemTrigBackend.tan). Keeping this wrapper free of extra reduction ensures consistent periodicity tests.

Parameters:

x (float) – Input angle in radians.

Returns:

tan(x) evaluated by the active numerics backend.

Return type:

float

algolib.numerics.trig_pure module

Numerical trig functions: sin / cos / tan

  • No stdlib math usage in implementation

  • Cody–Waite style range reduction by π/2

  • Polynomial approximation on [-π/4, π/4]

class algolib.numerics.trig_pure.PureTrigBackend[source]

Bases: object

Wrap current pure-Python trig implementations as a backend.

cos(x)[source]
name = 'pure'
sin(x)[source]
tan(x)[source]

Module contents