ML Foundations
Keep tiny logarithmic increments with Python log1p
Avoid losing a small increment before taking a logarithm, with an executable floating-point check.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
The increment can vanish before the logarithm
For a very small increment x, computing log(1 + x) literally first forms the floating-point sum. If that sum rounds to 1, the subsequent logarithm receives exactly 1 and returns zero. This can matter when a small log change contributes to an aggregate or a numerical check. Python floats have finite binary precision. [1, 2]
The mathematical expression is valid, but the order of floating-point operations can discard information before the logarithm is evaluated. Python provides math.log1p specifically to calculate the logarithm of 1 plus x accurately for x near zero. [1]
Compare the two calculations
Run python examples/python_log1p_small_increment.py. For x equal to 1e-18 in this runtime, the sum 1.0 + x equals 1.0. The direct calculation consequently prints zero. Calling log1p with x instead keeps a positive result, close to x for this small input. [1]
Assertions check the rounded sum, the zero direct result and the retained positive increment. The tolerance in the last assertion is a fixture check, not an error bound for all inputs or all math libraries.
The example uses Python scalar floats and the standard library, without model data or external services. It demonstrates loss during addition rather than benchmarking log functions.
Executed with Python 3.12.14; stdout and checksum are in private evidence.
import math
x = 1e-18
naive = math.log(1.0 + x)
stable = math.log1p(x)
assert 1.0 + x == 1.0
assert naive == 0.0
assert stable > 0.0
assert math.isclose(stable, x, rel_tol=1e-12)
print("1 + x equals 1:", 1.0 + x == 1.0)
print("log(1 + x):", naive)
print("log1p(x):", stable)
Recorded output
1 + x equals 1: True log(1 + x): 0.0 log1p(x): 1e-18
Check the domain and downstream policy
The real-valued logarithm requires 1 + x to be positive, so validate the intended input domain before applying this calculation. log1p does not turn an invalid domain into a valid one. [1]
A robust numerical pipeline also needs a policy for nonfinite inputs and accumulated error. The tiny fixture does not guarantee exact results or prescribe tolerances for a real dataset; choose those based on the scale and consequences of the application.