the exp / log / log10 / log1p family

exp(float $num): float — log(float $num, float $base = M_E): float — log10(float $num): float — log1p(float $num): float

The transcendental family: exp/log are mutual inverses (log(exp(2)) returns 2), log10 is the common logarithm, and log1p computes log(1+x) accurately even when x is near zero — the contrast experiment "1 + 1e-16 rounds to exactly 1.0 in double" is the heart of this entry.

C C23

C23
#include <math.h>
#include <stdio.h>

int main(void) {
    // exp and log are inverses: the round trip comes back to 2
    printf("%.10f\n", log(exp(2.0)));
    printf("%.10f\n", log10(1000.0));  // base-10 logarithm
    // log1p(x) computes log(1 + x) without first forming 1 + x:
    // 1 + 1e-16 rounds to exactly 1.0 in double precision,
    // so the plain log loses every bit of x
    printf("%.6e\n", log1p(1e-16));
    printf("%.6e\n", log(1.0 + 1e-16));
    printf("%.10f\n", log1p(1.0));     // ln(2), same value as log(2.0)
    return 0;
}
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+00
0.6931471806

C++ C++26

C++26
#include <cmath>
#include <cstdio>

int main() {
    // exp and log are inverses: the round trip comes back to 2
    std::printf("%.10f\n", std::log(std::exp(2.0)));
    std::printf("%.10f\n", std::log10(1000.0));
    // log1p(x) computes log(1 + x) without first forming 1 + x:
    // 1 + 1e-16 rounds to exactly 1.0 in double precision,
    // so the plain log loses every bit of x
    std::printf("%.6e\n", std::log1p(1e-16));
    std::printf("%.6e\n", std::log(1.0 + 1e-16));
    std::printf("%.10f\n", std::log1p(1.0));
    return 0;
}
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+00
0.6931471806

C# v11.0.100

v11.0.100
// .NET has no Math.Log1p (verified via reflection): hand-write it with the
// classic correction - when u == 1.0 the add swallowed x, so log(1+x) ~= x;
// otherwise rescale to recover the bits that 1 + x discarded
static double Log1p(double x) {
    double u = 1.0 + x;
    if (u == 1.0) return x;
    return Math.Log(u) * (x / (u - 1.0));
}

Console.WriteLine(Math.Log(Math.Exp(2)).ToString("F10"));
Console.WriteLine(Math.Log10(1000).ToString("F10"));
Console.WriteLine(Log1p(1e-16).ToString("0.000000e+00"));
Console.WriteLine(Math.Log(1 + 1e-16).ToString("0.000000e+00"));
Console.WriteLine(Log1p(1).ToString("F10"));
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+00
0.6931471806

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math"
)

func main() {
	// exp and log are inverses: the round trip comes back to 2
	fmt.Printf("%.10f\n", math.Log(math.Exp(2)))
	fmt.Printf("%.10f\n", math.Log10(1000))
	// Log1p computes log(1 + x) without first forming 1 + x:
	// 1 + 1e-16 rounds to exactly 1.0, so the plain Log loses every bit of x
	fmt.Printf("%.6e\n", math.Log1p(1e-16))
	fmt.Printf("%.6e\n", math.Log(1+1e-16))
	fmt.Printf("%.10f\n", math.Log1p(1))
}
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+00
0.6931471806

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        // exp and log are inverses: the round trip comes back to 2
        System.out.printf("%.10f%n", Math.log(Math.exp(2)));
        System.out.printf("%.10f%n", Math.log10(1000));
        // log1p computes log(1 + x) without first forming 1 + x:
        // 1 + 1e-16 rounds to exactly 1.0, so the plain log loses every bit of x
        System.out.printf("%.6e%n", Math.log1p(1e-16));
        System.out.printf("%.6e%n", Math.log(1 + 1e-16));
        System.out.printf("%.10f%n", Math.log1p(1));
    }
}
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+00
0.6931471806

JavaScript v26.13.1

v26.13.1
// exp and log are inverses: the round trip comes back to 2
console.log(Math.log(Math.exp(2)).toFixed(10));
console.log(Math.log10(1000).toFixed(10));
// Math.log1p(x) computes log(1 + x) without first forming 1 + x:
// 1 + 1e-16 rounds to exactly 1.0, so the plain log loses every bit of x
console.log(Math.log1p(1e-16).toExponential(6));
console.log(Math.log(1 + 1e-16).toExponential(6));
console.log(Math.log1p(1).toFixed(10));
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+0
0.6931471806

PHP v8.6.0

v8.6.0
// exp and log are inverses: the round trip comes back to 2
printf("%.10f\n", log(exp(2)));
printf("%.10f\n", log10(1000));   // base-10 logarithm
// log1p(x) computes log(1 + x) without first forming 1 + x:
// 1 + 1e-16 rounds to exactly 1.0 in double precision,
// so the plain log loses every bit of x
printf("%.6e\n", log1p(1e-16));
printf("%.6e\n", log(1 + 1e-16));
printf("%.10f\n", log1p(1));       // ln(2), same value as log(2)
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+0
0.6931471806

Python v3.15.2

v3.15.2
import math

# exp and log are inverses: the round trip comes back to 2
print(f"{math.log(math.exp(2)):.10f}")
print(f"{math.log10(1000):.10f}")       # base-10 logarithm
# math.log1p(x) computes log(1 + x) without first forming 1 + x:
# 1 + 1e-16 rounds to exactly 1.0 in double precision,
# so the plain log loses every bit of x
print(f"{math.log1p(1e-16):.6e}")
print(f"{math.log(1 + 1e-16):.6e}")
print(f"{math.log1p(1):.10f}")         # ln(2), same value as math.log(2)
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+00
0.6931471806

Rust v1.99.1

v1.99.1
fn main() {
    // exp and ln are inverses: the round trip comes back to 2
    println!("{:.10}", 2.0_f64.exp().ln());
    println!("{:.10}", 1000.0_f64.log10());
    // ln_1p computes ln(1 + x) without first forming 1 + x:
    // 1 + 1e-16 rounds to exactly 1.0, so the plain ln loses every bit of x
    println!("{:.6e}", 1e-16_f64.ln_1p());
    println!("{:.6e}", (1.0_f64 + 1e-16).ln());
    println!("{:.10}", 1.0_f64.ln_1p());
}
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e0
0.6931471806

TypeScript v26.13.1

v26.13.1
// exp and log are inverses: the round trip comes back to 2
console.log(Math.log(Math.exp(2)).toFixed(10));
console.log(Math.log10(1000).toFixed(10));
// Math.log1p(x) computes log(1 + x) without first forming 1 + x:
// 1 + 1e-16 rounds to exactly 1.0, so the plain log loses every bit of x
const tiny: number = Math.log1p(1e-16);
console.log(tiny.toExponential(6));
console.log(Math.log(1 + 1e-16).toExponential(6));
console.log(Math.log1p(1).toFixed(10));
Output
2.0000000000
3.0000000000
1.000000e-16
0.000000e+0
0.6931471806

Language differences

PHP: exp/log/log10/log1p are all built in; log($x, $base) supports an explicit base (e.g. log(8, 2) = 3) and defaults to e; the log1p manual page explicitly warns that plain log "might only return log(1)" when x is near zero.

Inverse-pair lesson: log(exp(2)) returns 2 — the transcendental round trip agrees across all ten languages under %.10f (the 1-ulp tail of a round trip sits far below the 10th decimal).

The log1p precision core: log1p(x) never forms 1 + x first — 1 + 1e-16 rounds to exactly 1.0 in double precision (1e-16 is less than half the minimum spacing 2.2e-16), so the naive log(1+1e-16) measures 0 while log1p(1e-16) accurately returns 1e-16 (the measured contrast in this entry's output: 1.000000e-16 vs 0.000000e+00).

Language map: Python math.log1p, JS/TS Math.log1p, Go math.Log1p, Rust ln_1p, Java Math.log1p, C/C++ log1p(3) (<math.h> included explicitly).

C#: .NET has no Math.Log1p (confirmed on this machine by reflection over net11.0) — the example hand-writes the classic correction u == 1.0 ? x : log(u) * (x / (u - 1)); at 1e-16 it takes the u == 1.0 branch and returns 1e-16 exactly.

Exponent format note: %.6e scientific notation differs in exponent width per language (PHP/JS/TS e+0, Rust e0, the rest e+00); each output field records its own run — read the significant digits (6 fixed).

The dual function: expm1(x) computes exp(x) - 1 accurately (the dual of log1p near zero) and exists in PHP/JS/Go/Rust/Java/C/C++; C# needs the same hand-written treatment.

See also: arbitrary-precision logarithms/exponentials are covered by the BCMath/GMP family; the e and pi constants have their own entries.