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 0ln(2)→ 0.693 ln 2ln(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.
pub fn ln(x: f64) -> f64
| x | float | a finite number greater than zero |
| returns | float | within 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
fune!(math.ln@^1); // then call ln(…)
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
// 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.
use super::funejson::Value; ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
// 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.
const LN2_HI: f64 = 6.93147180369123816490e-01;
const LN2_LO: f64 = 1.90821492927058770002e-10;
const SQRT2: f64 = 1.4142135623730951;
/// Natural logarithm using only +, -, * and /.
///
/// `f64::ln` calls the platform's maths library, whose last bit may differ
/// from JavaScript's and Python's. This follows the same steps in the same
/// order as the other two, so all three return the same double.
///
/// # Panics
/// Panics unless `x` is finite and greater than zero.
pub fn ln(x: f64) -> f64 {
if !x.is_finite() || x <= 0.0 {
panic!("ln is only defined for finite numbers greater than zero, received {}", x);
}
let mut m = x;
let mut e = 0i64;
while m >= 2.0 {
m /= 2.0;
e += 1;
}
while m < 1.0 {
m *= 2.0;
e -= 1;
}
if m > SQRT2 {
m /= 2.0;
e += 1;
}
let f = m - 1.0;
let s = f / (2.0 + f);
let z = s * s;
let mut p = 1.0 / 25.0;
p = 1.0 / 23.0 + z * p;
p = 1.0 / 21.0 + z * p;
p = 1.0 / 19.0 + z * p;
p = 1.0 / 17.0 + z * p;
p = 1.0 / 15.0 + z * p;
p = 1.0 / 13.0 + z * p;
p = 1.0 / 11.0 + z * p;
p = 1.0 / 9.0 + z * p;
p = 1.0 / 7.0 + z * p;
p = 1.0 / 5.0 + z * p;
p = 1.0 / 3.0 + z * p;
let lnm = 2.0 * s + 2.0 * s * (z * p);
let ef = e as f64;
ef * LN2_HI + (ef * LN2_LO + lnm)
}
pub fn fune_vector(args: &[Value]) -> Value {
Value::Float(ln(args[0].as_f64()))
}Install
fune build
With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Rust 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. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:
fune add math.ln
The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./math.ln-1.0.1-rust.fune, or fetch it from a terminal with fune pull math.ln@1.0.1:rust.
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.
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Case | Arguments | Expected | |
|---|---|---|---|
| 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
| Path | Bytes |
|---|---|
| NOTICE | 282 |
| README.md | 2,103 |
| impl/python.py | 1,641 |
| impl/rust.rs | 1,899 |
| impl/typescript.ts | 1,983 |
| vectors.json | 957 |