
(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 7 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 (if (or (<= x -2.6e-26) (not (<= x 5.2e-26))) (log1p x) (+ (+ x 1.0) -1.0)))
double code(double x) {
double tmp;
if ((x <= -2.6e-26) || !(x <= 5.2e-26)) {
tmp = log1p(x);
} else {
tmp = (x + 1.0) + -1.0;
}
return tmp;
}
public static double code(double x) {
double tmp;
if ((x <= -2.6e-26) || !(x <= 5.2e-26)) {
tmp = Math.log1p(x);
} else {
tmp = (x + 1.0) + -1.0;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -2.6e-26) or not (x <= 5.2e-26): tmp = math.log1p(x) else: tmp = (x + 1.0) + -1.0 return tmp
function code(x) tmp = 0.0 if ((x <= -2.6e-26) || !(x <= 5.2e-26)) tmp = log1p(x); else tmp = Float64(Float64(x + 1.0) + -1.0); end return tmp end
code[x_] := If[Or[LessEqual[x, -2.6e-26], N[Not[LessEqual[x, 5.2e-26]], $MachinePrecision]], N[Log[1 + x], $MachinePrecision], N[(N[(x + 1.0), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.6 \cdot 10^{-26} \lor \neg \left(x \leq 5.2 \cdot 10^{-26}\right):\\
\;\;\;\;\mathsf{log1p}\left(x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x + 1\right) + -1\\
\end{array}
\end{array}
if x < -2.6000000000000001e-26 or 5.2000000000000002e-26 < x Initial program 89.4%
log1p-def95.3%
Simplified95.3%
if -2.6000000000000001e-26 < x < 5.2000000000000002e-26Initial program 100.0%
add-cbrt-cube100.0%
pow1/3100.0%
log-pow100.0%
pow3100.0%
log-pow100.0%
log1p-def5.4%
Applied egg-rr5.4%
Taylor expanded in x around 0 5.4%
*-commutative5.4%
Simplified5.4%
*-commutative5.4%
associate-*r*5.4%
metadata-eval5.4%
expm1-log1p-u5.4%
*-un-lft-identity5.4%
expm1-udef100.0%
log1p-udef100.0%
rem-exp-log100.0%
+-commutative100.0%
Applied egg-rr100.0%
Final simplification98.3%
(FPCore (x) :precision binary64 (* (pow (pow (log1p x) 16.0) 0.05555555555555555) (+ (+ 1.0 (cbrt (cbrt (log1p x)))) -1.0)))
double code(double x) {
return pow(pow(log1p(x), 16.0), 0.05555555555555555) * ((1.0 + cbrt(cbrt(log1p(x)))) + -1.0);
}
public static double code(double x) {
return Math.pow(Math.pow(Math.log1p(x), 16.0), 0.05555555555555555) * ((1.0 + Math.cbrt(Math.cbrt(Math.log1p(x)))) + -1.0);
}
function code(x) return Float64(((log1p(x) ^ 16.0) ^ 0.05555555555555555) * Float64(Float64(1.0 + cbrt(cbrt(log1p(x)))) + -1.0)) end
code[x_] := N[(N[Power[N[Power[N[Log[1 + x], $MachinePrecision], 16.0], $MachinePrecision], 0.05555555555555555], $MachinePrecision] * N[(N[(1.0 + N[Power[N[Power[N[Log[1 + x], $MachinePrecision], 1/3], $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left({\left(\mathsf{log1p}\left(x\right)\right)}^{16}\right)}^{0.05555555555555555} \cdot \left(\left(1 + \sqrt[3]{\sqrt[3]{\mathsf{log1p}\left(x\right)}}\right) + -1\right)
\end{array}
Initial program 96.3%
add-cbrt-cube96.0%
pow1/394.4%
add-cube-cbrt94.4%
associate-*r*94.4%
unpow-prod-down94.5%
Applied egg-rr76.8%
unpow1/376.9%
unpow276.9%
rem-cube-cbrt76.7%
pow-plus76.7%
metadata-eval76.7%
rem-cube-cbrt76.6%
pow-plus76.6%
metadata-eval76.6%
pow-sqr76.6%
metadata-eval76.6%
Simplified76.6%
pow1/376.6%
add-cbrt-cube93.5%
pow1/393.3%
pow-pow93.5%
pow393.5%
pow1/357.8%
pow-pow57.8%
pow-pow93.6%
metadata-eval93.6%
metadata-eval93.6%
metadata-eval93.6%
Applied egg-rr93.6%
expm1-log1p-u93.5%
expm1-udef95.8%
sqr-pow95.8%
pow-prod-down95.8%
pow-prod-up95.8%
metadata-eval95.8%
metadata-eval95.8%
Applied egg-rr95.8%
expm1-def97.3%
expm1-log1p97.3%
Simplified97.3%
expm1-log1p-u97.4%
expm1-udef97.4%
log1p-udef97.4%
rem-exp-log97.4%
Applied egg-rr97.4%
Final simplification97.4%
(FPCore (x) :precision binary64 (* (pow (pow (log1p x) 16.0) 0.05555555555555555) (cbrt (cbrt (log1p x)))))
double code(double x) {
return pow(pow(log1p(x), 16.0), 0.05555555555555555) * cbrt(cbrt(log1p(x)));
}
public static double code(double x) {
return Math.pow(Math.pow(Math.log1p(x), 16.0), 0.05555555555555555) * Math.cbrt(Math.cbrt(Math.log1p(x)));
}
function code(x) return Float64(((log1p(x) ^ 16.0) ^ 0.05555555555555555) * cbrt(cbrt(log1p(x)))) end
code[x_] := N[(N[Power[N[Power[N[Log[1 + x], $MachinePrecision], 16.0], $MachinePrecision], 0.05555555555555555], $MachinePrecision] * N[Power[N[Power[N[Log[1 + x], $MachinePrecision], 1/3], $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left({\left(\mathsf{log1p}\left(x\right)\right)}^{16}\right)}^{0.05555555555555555} \cdot \sqrt[3]{\sqrt[3]{\mathsf{log1p}\left(x\right)}}
\end{array}
Initial program 96.3%
add-cbrt-cube96.0%
pow1/394.4%
add-cube-cbrt94.4%
associate-*r*94.4%
unpow-prod-down94.5%
Applied egg-rr76.8%
unpow1/376.9%
unpow276.9%
rem-cube-cbrt76.7%
pow-plus76.7%
metadata-eval76.7%
rem-cube-cbrt76.6%
pow-plus76.6%
metadata-eval76.6%
pow-sqr76.6%
metadata-eval76.6%
Simplified76.6%
pow1/376.6%
add-cbrt-cube93.5%
pow1/393.3%
pow-pow93.5%
pow393.5%
pow1/357.8%
pow-pow57.8%
pow-pow93.6%
metadata-eval93.6%
metadata-eval93.6%
metadata-eval93.6%
Applied egg-rr93.6%
expm1-log1p-u93.5%
expm1-udef95.8%
sqr-pow95.8%
pow-prod-down95.8%
pow-prod-up95.8%
metadata-eval95.8%
metadata-eval95.8%
Applied egg-rr95.8%
expm1-def97.3%
expm1-log1p97.3%
Simplified97.3%
Final simplification97.3%
(FPCore (x) :precision binary64 (if (<= x -2.6e-26) (* 0.3333333333333333 (* (log1p x) 3.0)) (log (+ x 1.0))))
double code(double x) {
double tmp;
if (x <= -2.6e-26) {
tmp = 0.3333333333333333 * (log1p(x) * 3.0);
} else {
tmp = log((x + 1.0));
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= -2.6e-26) {
tmp = 0.3333333333333333 * (Math.log1p(x) * 3.0);
} else {
tmp = Math.log((x + 1.0));
}
return tmp;
}
def code(x): tmp = 0 if x <= -2.6e-26: tmp = 0.3333333333333333 * (math.log1p(x) * 3.0) else: tmp = math.log((x + 1.0)) return tmp
function code(x) tmp = 0.0 if (x <= -2.6e-26) tmp = Float64(0.3333333333333333 * Float64(log1p(x) * 3.0)); else tmp = log(Float64(x + 1.0)); end return tmp end
code[x_] := If[LessEqual[x, -2.6e-26], N[(0.3333333333333333 * N[(N[Log[1 + x], $MachinePrecision] * 3.0), $MachinePrecision]), $MachinePrecision], N[Log[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.6 \cdot 10^{-26}:\\
\;\;\;\;0.3333333333333333 \cdot \left(\mathsf{log1p}\left(x\right) \cdot 3\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(x + 1\right)\\
\end{array}
\end{array}
if x < -2.6000000000000001e-26Initial program 29.1%
add-cbrt-cube27.5%
pow1/329.1%
log-pow29.1%
pow328.9%
log-pow29.1%
log1p-def68.4%
Applied egg-rr68.4%
if -2.6000000000000001e-26 < x Initial program 99.3%
Final simplification98.0%
(FPCore (x) :precision binary64 (if (<= x -2.6e-26) (log1p x) (log (+ x 1.0))))
double code(double x) {
double tmp;
if (x <= -2.6e-26) {
tmp = log1p(x);
} else {
tmp = log((x + 1.0));
}
return tmp;
}
public static double code(double x) {
double tmp;
if (x <= -2.6e-26) {
tmp = Math.log1p(x);
} else {
tmp = Math.log((x + 1.0));
}
return tmp;
}
def code(x): tmp = 0 if x <= -2.6e-26: tmp = math.log1p(x) else: tmp = math.log((x + 1.0)) return tmp
function code(x) tmp = 0.0 if (x <= -2.6e-26) tmp = log1p(x); else tmp = log(Float64(x + 1.0)); end return tmp end
code[x_] := If[LessEqual[x, -2.6e-26], N[Log[1 + x], $MachinePrecision], N[Log[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.6 \cdot 10^{-26}:\\
\;\;\;\;\mathsf{log1p}\left(x\right)\\
\mathbf{else}:\\
\;\;\;\;\log \left(x + 1\right)\\
\end{array}
\end{array}
if x < -2.6000000000000001e-26Initial program 29.1%
log1p-def68.4%
Simplified68.4%
if -2.6000000000000001e-26 < x Initial program 99.3%
Final simplification97.9%
(FPCore (x) :precision binary64 (+ (+ x 1.0) -1.0))
double code(double x) {
return (x + 1.0) + -1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x + 1.0d0) + (-1.0d0)
end function
public static double code(double x) {
return (x + 1.0) + -1.0;
}
def code(x): return (x + 1.0) + -1.0
function code(x) return Float64(Float64(x + 1.0) + -1.0) end
function tmp = code(x) tmp = (x + 1.0) + -1.0; end
code[x_] := N[(N[(x + 1.0), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\left(x + 1\right) + -1
\end{array}
Initial program 96.3%
add-cbrt-cube79.9%
pow1/379.9%
log-pow79.9%
pow379.9%
log-pow96.1%
log1p-def36.9%
Applied egg-rr36.9%
Taylor expanded in x around 0 8.1%
*-commutative8.1%
Simplified8.1%
*-commutative8.1%
associate-*r*8.1%
metadata-eval8.1%
expm1-log1p-u8.1%
*-un-lft-identity8.1%
expm1-udef67.6%
log1p-udef67.6%
rem-exp-log67.6%
+-commutative67.6%
Applied egg-rr67.6%
Final simplification67.6%
(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 96.3%
Taylor expanded in x around 0 8.1%
Final simplification8.1%
(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 2024031
(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)))