Functional Weave
Code in Python

math.ln

Natural logarithm from + - * / only, so TypeScript, Python and Rust return the same double for the same input.

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

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

What it does

The natural logarithm, computed with nothing but `+`, `-`, `*` and `/`, so TypeScript, Python and Rust return the same double for the same input.

`Math.log`, Python's `math.log` and Rust's `f64::ln` all call their platform's maths library, and IEEE 754 does not require those to be correctly rounded: the last bit can differ between a browser, CPython on glibc and Rust on macOS. Usually that does not matter. It does when the result is then rounded for display or compared against a threshold, because a value next to the boundary goes one way in one language and the other way in another. A log scale, a colour conversion or a growth rate that must agree across languages uses this instead.

For example

  • ln(1) → 0 ln 1 is exactly 0
  • ln(2) → 0.693 ln 2
  • ln(10) → 2.303 ln 10

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 ln(x: float) -> float
xfloata finite number greater than zero
returnsfloatwithin a few units in the last place of the true value; bit-identical in every language

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

from fune.math.ln import ln  # math.ln@^1
impl/python.py · 54 lines · open · raw
# Derived from fdlibm e_log.c.
# Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
# Developed at SunSoft, a Sun Microsystems, Inc. business.
# Permission to use, copy, modify, and distribute this
# software is freely granted, provided that this notice
# is preserved.

import math

# fdlibm's split of ln 2: the high part has trailing zero bits, so e * LN2_HI
# is exact for any exponent a double can have.
LN2_HI = 6.93147180369123816490e-01
LN2_LO = 1.90821492927058770002e-10
SQRT2 = 1.4142135623730951


def ln(x: float) -> float:
    """Natural logarithm using only +, -, * and /.

    math.log comes from the platform's C library, whose last bit may differ
    from JavaScript's and Rust's. This follows the same steps in the same
    order as the TypeScript and Rust versions, so all three return the same
    double. See the README for the method.
    """
    if isinstance(x, bool) or not isinstance(x, (int, float)) or not math.isfinite(x) or x <= 0:
        raise ValueError("ln is only defined for finite numbers greater than zero, received %r" % (x,))
    m = float(x)
    e = 0
    while m >= 2:
        m /= 2
        e += 1
    while m < 1:
        m *= 2
        e -= 1
    if m > SQRT2:
        m /= 2
        e += 1
    f = m - 1
    s = f / (2 + f)
    z = s * s
    p = 1 / 25
    p = 1 / 23 + z * p
    p = 1 / 21 + z * p
    p = 1 / 19 + z * p
    p = 1 / 17 + z * p
    p = 1 / 15 + z * p
    p = 1 / 13 + z * p
    p = 1 / 11 + z * p
    p = 1 / 9 + z * p
    p = 1 / 7 + z * p
    p = 1 / 5 + z * p
    p = 1 / 3 + z * p
    lnm = 2 * s + 2 * s * (z * p)
    return e * LN2_HI + (e * LN2_LO + lnm)

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, 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.ln
Download for Python math.ln-1.0.1-python.fune · 6,521 bytes sha256 c198be757be45feff14d99d1c6cc0a859c6438af2d818cb9ac9a843536d82903

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

The whole function, every language, is one file too: math.ln-1.0.1.fune, 10,563 bytes, sha256 fcc649f7bc35683193541d1555c0148e1698113437f262b6ec72d5868216ca15. 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.ln

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

# fune: after math.ln

replace — it requires no other capability, so there is no dependency to replace.

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

# fune: step math.ln 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
ln 1 is exactly 0 1 → 0
ln 2 2 → 0.693
ln 10 10 → 2.303
ln e is 1 2.718 → 1
ln 1/2 is -ln 2 0.5 → -0.693
ln 1.5, which sits just above the sqrt(2) split 1.5 → 0.405
a tiny value 0 → -23.026
a huge value 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,0… → 690.776
the smallest positive double 0 → -744.44
zero is an error 0 → error: ln is only defined for finite numbers greater than zero
Show the other 1 test
CaseArgumentsExpected
a negative number is an error -1 → error: ln is only defined for finite numbers greater than zero

More from the author

## Method

1. Write x = m x 2^e with m in (sqrt(1/2), sqrt(2)], by halving or doubling, which is exact for every double, subnormals included. 2. With s = (m - 1) / (m + 1), |s| < 0.172, ln m = 2 atanh s = 2s (1 + s²/3 + s⁴/5 + ... + s²⁴/25), summed by Horner's rule. 3. ln x = e ln 2 + ln m, with ln 2 split into a high part whose trailing bits are zero (so e x high is exact) and a low correction, as in fdlibm.

The result is within 2 units in the last place of the true logarithm over the whole positive range (checked against a correctly rounded reference), and bit-identical in all three languages because every step is an IEEE operation performed in the same order. `ln(1)` is exactly 0.

Zero, negative numbers, infinity and NaN are errors.

Sources: W. J. Cody and W. Waite, *Software Manual for the Elementary Functions* (1980); Sun Microsystems fdlibm, `e_log.c` (the ln 2 split).

## Notices

Derived from fdlibm e_log.c. Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. Developed at SunSoft, a Sun Microsystems, Inc. business. Permission to use, copy, modify, and distribute this software is freely granted, provided that this notice is preserved.

The same notice heads each implementation file.

1.0.1 adds its attribution notices (NOTICE). The code is unchanged apart from that notice at the top of each implementation file; the tests are unchanged.

Files

PathBytes
NOTICE282
README.md2,103
impl/python.py1,641
impl/rust.rs1,899
impl/typescript.ts1,983
vectors.json957