double log1p(double x) { if (x.abs() < 1e-4) return x - 0.5 * x * x + x * x * x / 3.0; return math.log(1 + x); }