?

Average Error: 39.0 → 0.2
Time: 10.9s
Precision: binary64
Cost: 20484

?

\[\log \left(1 + x\right) \]
\[\begin{array}{l} \mathbf{if}\;1 + x \leq 2:\\ \;\;\;\;\left(x + -0.25 \cdot {x}^{4}\right) + \left(-0.5 \cdot {x}^{2} + 0.3333333333333333 \cdot {x}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array} \]
(FPCore (x) :precision binary64 (log (+ 1.0 x)))
(FPCore (x)
 :precision binary64
 (if (<= (+ 1.0 x) 2.0)
   (+
    (+ x (* -0.25 (pow x 4.0)))
    (+ (* -0.5 (pow x 2.0)) (* 0.3333333333333333 (pow x 3.0))))
   (log (+ 1.0 x))))
double code(double x) {
	return log((1.0 + x));
}
double code(double x) {
	double tmp;
	if ((1.0 + x) <= 2.0) {
		tmp = (x + (-0.25 * pow(x, 4.0))) + ((-0.5 * pow(x, 2.0)) + (0.3333333333333333 * pow(x, 3.0)));
	} else {
		tmp = log((1.0 + x));
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = log((1.0d0 + x))
end function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((1.0d0 + x) <= 2.0d0) then
        tmp = (x + ((-0.25d0) * (x ** 4.0d0))) + (((-0.5d0) * (x ** 2.0d0)) + (0.3333333333333333d0 * (x ** 3.0d0)))
    else
        tmp = log((1.0d0 + x))
    end if
    code = tmp
end function
public static double code(double x) {
	return Math.log((1.0 + x));
}
public static double code(double x) {
	double tmp;
	if ((1.0 + x) <= 2.0) {
		tmp = (x + (-0.25 * Math.pow(x, 4.0))) + ((-0.5 * Math.pow(x, 2.0)) + (0.3333333333333333 * Math.pow(x, 3.0)));
	} else {
		tmp = Math.log((1.0 + x));
	}
	return tmp;
}
def code(x):
	return math.log((1.0 + x))
def code(x):
	tmp = 0
	if (1.0 + x) <= 2.0:
		tmp = (x + (-0.25 * math.pow(x, 4.0))) + ((-0.5 * math.pow(x, 2.0)) + (0.3333333333333333 * math.pow(x, 3.0)))
	else:
		tmp = math.log((1.0 + x))
	return tmp
function code(x)
	return log(Float64(1.0 + x))
end
function code(x)
	tmp = 0.0
	if (Float64(1.0 + x) <= 2.0)
		tmp = Float64(Float64(x + Float64(-0.25 * (x ^ 4.0))) + Float64(Float64(-0.5 * (x ^ 2.0)) + Float64(0.3333333333333333 * (x ^ 3.0))));
	else
		tmp = log(Float64(1.0 + x));
	end
	return tmp
end
function tmp = code(x)
	tmp = log((1.0 + x));
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((1.0 + x) <= 2.0)
		tmp = (x + (-0.25 * (x ^ 4.0))) + ((-0.5 * (x ^ 2.0)) + (0.3333333333333333 * (x ^ 3.0)));
	else
		tmp = log((1.0 + x));
	end
	tmp_2 = tmp;
end
code[x_] := N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]
code[x_] := If[LessEqual[N[(1.0 + x), $MachinePrecision], 2.0], N[(N[(x + N[(-0.25 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(-0.5 * N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision] + N[(0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]]
\log \left(1 + x\right)
\begin{array}{l}
\mathbf{if}\;1 + x \leq 2:\\
\;\;\;\;\left(x + -0.25 \cdot {x}^{4}\right) + \left(-0.5 \cdot {x}^{2} + 0.3333333333333333 \cdot {x}^{3}\right)\\

\mathbf{else}:\\
\;\;\;\;\log \left(1 + x\right)\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original39.0
Target0.3
Herbie0.2
\[\begin{array}{l} \mathbf{if}\;1 + x = 1:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot \log \left(1 + x\right)}{\left(1 + x\right) - 1}\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if (+.f64 1 x) < 2

    1. Initial program 58.6

      \[\log \left(1 + x\right) \]
    2. Applied egg-rr58.6

      \[\leadsto \color{blue}{\left(0 - \left(-1 - \log \left(1 + x\right)\right)\right) - 1} \]
    3. Taylor expanded in x around 0 0.3

      \[\leadsto \color{blue}{-0.5 \cdot {x}^{2} + \left(0.3333333333333333 \cdot {x}^{3} + \left(-0.25 \cdot {x}^{4} + x\right)\right)} \]
    4. Simplified0.3

      \[\leadsto \color{blue}{\left(x + -0.25 \cdot {x}^{4}\right) + \left(-0.5 \cdot {x}^{2} + 0.3333333333333333 \cdot {x}^{3}\right)} \]
      Proof

      [Start]0.3

      \[ -0.5 \cdot {x}^{2} + \left(0.3333333333333333 \cdot {x}^{3} + \left(-0.25 \cdot {x}^{4} + x\right)\right) \]

      rational.json-simplify-41 [<=]0.3

      \[ \color{blue}{\left(-0.25 \cdot {x}^{4} + x\right) + \left(-0.5 \cdot {x}^{2} + 0.3333333333333333 \cdot {x}^{3}\right)} \]

      rational.json-simplify-1 [=>]0.3

      \[ \color{blue}{\left(x + -0.25 \cdot {x}^{4}\right)} + \left(-0.5 \cdot {x}^{2} + 0.3333333333333333 \cdot {x}^{3}\right) \]

    if 2 < (+.f64 1 x)

    1. Initial program 0

      \[\log \left(1 + x\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;1 + x \leq 2:\\ \;\;\;\;\left(x + -0.25 \cdot {x}^{4}\right) + \left(-0.5 \cdot {x}^{2} + 0.3333333333333333 \cdot {x}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array} \]

Alternatives

Alternative 1
Error0.2
Cost20484
\[\begin{array}{l} \mathbf{if}\;1 + x \leq 2:\\ \;\;\;\;-0.5 \cdot {x}^{2} + \left(0.3333333333333333 \cdot {x}^{3} + \left(-0.25 \cdot {x}^{4} + x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array} \]
Alternative 2
Error0.2
Cost13764
\[\begin{array}{l} \mathbf{if}\;1 + x \leq 1.0005:\\ \;\;\;\;-0.5 \cdot {x}^{2} + \left(0.3333333333333333 \cdot {x}^{3} + x\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array} \]
Alternative 3
Error0.3
Cost7044
\[\begin{array}{l} \mathbf{if}\;1 + x \leq 1.0005:\\ \;\;\;\;-0.5 \cdot {x}^{2} + x\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array} \]
Alternative 4
Error0.7
Cost6852
\[\begin{array}{l} \mathbf{if}\;1 + x \leq 1.00000002:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + x\right)\\ \end{array} \]
Alternative 5
Error21.1
Cost64
\[x \]

Error

Reproduce?

herbie shell --seed 2023077 
(FPCore (x)
  :name "ln(1 + x)"
  :precision binary64

  :herbie-target
  (if (== (+ 1.0 x) 1.0) x (/ (* x (log (+ 1.0 x))) (- (+ 1.0 x) 1.0)))

  (log (+ 1.0 x)))