
(FPCore (x) :precision binary64 (log (+ 1.0 x)))
double code(double x) {
return log((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = log((1.0d0 + x))
end function
public static double code(double x) {
return Math.log((1.0 + x));
}
def code(x): return math.log((1.0 + x))
function code(x) return log(Float64(1.0 + x)) end
function tmp = code(x) tmp = log((1.0 + x)); end
code[x_] := N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + x\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (log (+ 1.0 x)))
double code(double x) {
return log((1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = log((1.0d0 + x))
end function
public static double code(double x) {
return Math.log((1.0 + x));
}
def code(x): return math.log((1.0 + x))
function code(x) return log(Float64(1.0 + x)) end
function tmp = code(x) tmp = log((1.0 + x)); end
code[x_] := N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\log \left(1 + x\right)
\end{array}
(FPCore (x) :precision binary64 (log1p x))
double code(double x) {
return log1p(x);
}
public static double code(double x) {
return Math.log1p(x);
}
def code(x): return math.log1p(x)
function code(x) return log1p(x) end
code[x_] := N[Log[1 + x], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(x\right)
\end{array}
Initial program 40.0%
log1p-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x 1.45) (+ x (* (* x x) (+ -0.5 (* x (+ (* x -0.25) 0.3333333333333333))))) (log x)))
double code(double x) {
double tmp;
if (x <= 1.45) {
tmp = x + ((x * x) * (-0.5 + (x * ((x * -0.25) + 0.3333333333333333))));
} else {
tmp = log(x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 1.45d0) then
tmp = x + ((x * x) * ((-0.5d0) + (x * ((x * (-0.25d0)) + 0.3333333333333333d0))))
else
tmp = log(x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 1.45) {
tmp = x + ((x * x) * (-0.5 + (x * ((x * -0.25) + 0.3333333333333333))));
} else {
tmp = Math.log(x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 1.45: tmp = x + ((x * x) * (-0.5 + (x * ((x * -0.25) + 0.3333333333333333)))) else: tmp = math.log(x) return tmp
function code(x) tmp = 0.0 if (x <= 1.45) tmp = Float64(x + Float64(Float64(x * x) * Float64(-0.5 + Float64(x * Float64(Float64(x * -0.25) + 0.3333333333333333))))); else tmp = log(x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 1.45) tmp = x + ((x * x) * (-0.5 + (x * ((x * -0.25) + 0.3333333333333333)))); else tmp = log(x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 1.45], N[(x + N[(N[(x * x), $MachinePrecision] * N[(-0.5 + N[(x * N[(N[(x * -0.25), $MachinePrecision] + 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[Log[x], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.45:\\
\;\;\;\;x + \left(x \cdot x\right) \cdot \left(-0.5 + x \cdot \left(x \cdot -0.25 + 0.3333333333333333\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\log x\\
\end{array}
\end{array}
if x < 1.44999999999999996Initial program 8.5%
Taylor expanded in x around 0 99.7%
*-commutative99.7%
+-commutative99.7%
*-commutative99.7%
unpow399.7%
unpow299.7%
associate-*l*99.7%
*-commutative99.7%
metadata-eval99.7%
pow-sqr99.7%
associate-*l*99.7%
distribute-lft-out99.7%
distribute-lft-out99.7%
unpow299.7%
+-commutative99.7%
unpow299.7%
associate-*l*99.7%
distribute-lft-out99.7%
Simplified99.7%
if 1.44999999999999996 < x Initial program 100.0%
Taylor expanded in x around inf 99.2%
mul-1-neg99.2%
log-rec99.2%
remove-double-neg99.2%
Simplified99.2%
Final simplification99.5%
(FPCore (x) :precision binary64 (+ x (* (* x x) (- (* x 0.3333333333333333) 0.5))))
double code(double x) {
return x + ((x * x) * ((x * 0.3333333333333333) - 0.5));
}
real(8) function code(x)
real(8), intent (in) :: x
code = x + ((x * x) * ((x * 0.3333333333333333d0) - 0.5d0))
end function
public static double code(double x) {
return x + ((x * x) * ((x * 0.3333333333333333) - 0.5));
}
def code(x): return x + ((x * x) * ((x * 0.3333333333333333) - 0.5))
function code(x) return Float64(x + Float64(Float64(x * x) * Float64(Float64(x * 0.3333333333333333) - 0.5))) end
function tmp = code(x) tmp = x + ((x * x) * ((x * 0.3333333333333333) - 0.5)); end
code[x_] := N[(x + N[(N[(x * x), $MachinePrecision] * N[(N[(x * 0.3333333333333333), $MachinePrecision] - 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(x \cdot x\right) \cdot \left(x \cdot 0.3333333333333333 - 0.5\right)
\end{array}
Initial program 40.0%
Taylor expanded in x around 0 65.6%
*-commutative65.6%
+-commutative65.6%
*-commutative65.6%
unpow365.6%
unpow265.6%
associate-*l*65.6%
*-commutative65.6%
metadata-eval65.6%
pow-sqr65.6%
associate-*l*65.6%
distribute-lft-out65.7%
distribute-lft-out65.7%
unpow265.7%
+-commutative65.7%
unpow265.7%
associate-*l*65.7%
distribute-lft-out65.7%
Simplified65.7%
Taylor expanded in x around 0 66.7%
Final simplification66.7%
(FPCore (x) :precision binary64 x)
double code(double x) {
return x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x
end function
public static double code(double x) {
return x;
}
def code(x): return x
function code(x) return x end
function tmp = code(x) tmp = x; end
code[x_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 40.0%
Taylor expanded in x around 0 66.2%
Final simplification66.2%
(FPCore (x) :precision binary64 (if (== (+ 1.0 x) 1.0) x (/ (* x (log (+ 1.0 x))) (- (+ 1.0 x) 1.0))))
double code(double x) {
double tmp;
if ((1.0 + x) == 1.0) {
tmp = x;
} else {
tmp = (x * log((1.0 + x))) / ((1.0 + x) - 1.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((1.0d0 + x) == 1.0d0) then
tmp = x
else
tmp = (x * log((1.0d0 + x))) / ((1.0d0 + x) - 1.0d0)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((1.0 + x) == 1.0) {
tmp = x;
} else {
tmp = (x * Math.log((1.0 + x))) / ((1.0 + x) - 1.0);
}
return tmp;
}
def code(x): tmp = 0 if (1.0 + x) == 1.0: tmp = x else: tmp = (x * math.log((1.0 + x))) / ((1.0 + x) - 1.0) return tmp
function code(x) tmp = 0.0 if (Float64(1.0 + x) == 1.0) tmp = x; else tmp = Float64(Float64(x * log(Float64(1.0 + x))) / Float64(Float64(1.0 + x) - 1.0)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((1.0 + x) == 1.0) tmp = x; else tmp = (x * log((1.0 + x))) / ((1.0 + x) - 1.0); end tmp_2 = tmp; end
code[x_] := If[Equal[N[(1.0 + x), $MachinePrecision], 1.0], x, N[(N[(x * N[Log[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(N[(1.0 + x), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\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}
\end{array}
herbie shell --seed 2023271
(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)))