Functional Weave
Code in Python

math.pow

base to the power exponent from + - * / only, exact by squaring for whole exponents, identical in every language.

1.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 16 tests, run in TypeScript, Python and Rust.

What it does

`base` to the power `exponent`, bit-identical in TypeScript, Python and Rust.

- **Whole-number exponents** are done by repeated squaring, so `pow(10, 3)` is exactly `1000` and `pow(-2, 3)` is `-8`. A negative whole exponent is the reciprocal. `exp(3 x ln 10)` would give 999.9999999999998. - **Other exponents** are `exp(exponent x ln(base))`, from `math.exp` and `math.ln`, which use only IEEE-exact operations. The error grows with the size of `exponent x ln(base)`: within a few dozen units in the last place for everyday arguments (cube roots, the sRGB gamma 2.4), which is far below any rounding a caller applies, and identical in every language, which is the point.

For example

  • pow(10, 3) → 1,000 a whole exponent is exact: 10^3 is 1000, not 999.9999999999998
  • pow(2, -2) → 0.25 a negative whole exponent
  • pow(-2, 3) → -8 a negative base with an odd exponent

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def pow(base: float, exponent: float) -> float
basefloatany finite number; negative only with a whole-number exponent
exponentfloatany finite number; 1/3 is a cube root, 2.4 the sRGB gamma
returnsfloatbit-identical in every language

Your code names it in one line, in the file that uses it

from fune.math.pow import pow  # math.pow@^1
impl/python.py · 52 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import math

from .math_exp import exp  ← from math.exp ^1.0.0 · built alongside by fune
from .math_ln import ln  ← from math.ln ^1.0.0 · built alongside by fune

# Below this exponent of e the result is smaller than any normal double.
_SMALLEST = -708
_LARGEST = 709


def pow(base: float, exponent: float) -> float:  # noqa: A001 - the manifest names it pow
    """base^exponent with the same bits in TypeScript, Python and Rust.

    A whole-number exponent is done by repeated squaring, so 10^3 is exactly
    1000; any other is exp(exponent * ln(base)) from math.exp and math.ln.
    Python's ** and math.pow use the C library, whose last bit may differ.
    """
    for v, what in ((base, "base"), (exponent, "exponent")):
        if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v):
            raise ValueError("pow needs a finite %s, received %r" % (what, v))
    base = float(base)
    exponent = float(exponent)
    if base == 0:
        if exponent < 0:
            raise ValueError("zero cannot be raised to a negative power")
        return 1.0 if exponent == 0 else 0.0
    if math.floor(exponent) == exponent:
        n = int(abs(exponent))
        b = base
        result = 1.0
        while n > 0:
            if n % 2 == 1:
                result *= b
            n //= 2
            if n > 0:
                b *= b
        if exponent < 0:
            if result == 0 or math.isinf(result):
                result = 0.0 if math.isinf(result) else math.inf
            else:
                result = 1 / result
        if not math.isfinite(result):
            raise ValueError("pow result is too large to represent")
        return result + 0.0
    if base < 0:
        raise ValueError("a negative base needs a whole-number exponent")
    power = exponent * ln(base)
    if power > _LARGEST:
        raise ValueError("pow result is too large to represent")
    if power < _SMALLEST:
        return 0.0
    return exp(power)

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and its 2 dependencies, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add math.pow
Download for Python math.pow-1.0.0-python.fune · 6,333 bytes sha256 263c09913cde8fb8b8e2c754508242d3dcad519936883ce074c8dc1d96e4bf48

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./math.pow-1.0.0-python.fune, or fetch it from a terminal with fune pull math.pow@1.0.0:python.

The whole function, every language, is one file too: math.pow-1.0.0.fune, 10,336 bytes, sha256 cd57d65c28d5c41529f94f72488bf0eaae5a2fe9c3bc09339de550bc46cd5cc7. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before math.pow

after — your function gets the result and the arguments, and returns the final result.

# fune: after math.pow

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

# fune: replace math.exp in math.pow
# fune: replace math.ln in math.pow

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show math.pow --steps.

# fune: step math.pow after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
a whole exponent is exact: 10^3 is 1000, not 999.9999999999998 10, 3 → 1,000
a negative whole exponent 2, -2 → 0.25
a negative base with an odd exponent -2, 3 → -8
a negative base with an even exponent -2, 2 → 4
squaring keeps binary rounding: 1.1^2 1.1, 2 → 1.21
a square root 2, 0.5 → 1.414
a cube root 8, 0.333 → 2
the sRGB gamma exponent 0.5, 2.4 → 0.189
anything to the power 0 is 1 7.5, 0 → 1
zero to the power 0 is 1 0, 0 → 1
Show the other 6 tests
CaseArgumentsExpected
zero to a positive power is 0 0, 2.5 → 0
a result below the smallest normal double is 0 0.5, 2,000.5 → 0
zero to a negative power is an error 0, -1 → error: zero cannot be raised to a negative power
a negative base with a fractional exponent is an error -8, 0.333 → error: a negative base needs a whole-number exponent
an overflowing whole power is an error 10, 400 → error: pow result is too large to represent
an overflowing fractional power is an error 10, 400.5 → error: pow result is too large to represent

More from the author

Edge cases: `0^0` is 1, `0^positive` is 0, `0^negative` is an error. A negative base needs a whole-number exponent (the real answer does not exist otherwise). A result too large for a double is an error; one smaller than the smallest normal double (about 1e-308) is 0.

`**`, `Math.pow`, `math.pow` and `f64::powf` are not used because they come from each platform's maths library, whose last bit may differ.

Files

PathBytes
README.md1,109
impl/python.py1,876
impl/rust.rs2,053
impl/typescript.ts1,779
vectors.json1,605