
(FPCore (c x y) :precision binary64 (* c (log (+ 1.0 (* (- (pow (E) x) 1.0) y)))))
\begin{array}{l}
\\
c \cdot \log \left(1 + \left({\mathsf{E}\left(\right)}^{x} - 1\right) \cdot y\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (c x y) :precision binary64 (* c (log (+ 1.0 (* (- (pow (E) x) 1.0) y)))))
\begin{array}{l}
\\
c \cdot \log \left(1 + \left({\mathsf{E}\left(\right)}^{x} - 1\right) \cdot y\right)
\end{array}
(FPCore (c x y)
:precision binary64
(if (<= y -1e-39)
(* (log1p (* y (expm1 x))) c)
(if (<= y 2200000000.0)
(/ (* (expm1 (* x 3.0)) (* c y)) (fma (exp x) (exp x) (+ (exp x) 1.0)))
(* (log1p (* y (* (fma 0.5 x 1.0) x))) c))))
double code(double c, double x, double y) {
double tmp;
if (y <= -1e-39) {
tmp = log1p((y * expm1(x))) * c;
} else if (y <= 2200000000.0) {
tmp = (expm1((x * 3.0)) * (c * y)) / fma(exp(x), exp(x), (exp(x) + 1.0));
} else {
tmp = log1p((y * (fma(0.5, x, 1.0) * x))) * c;
}
return tmp;
}
function code(c, x, y) tmp = 0.0 if (y <= -1e-39) tmp = Float64(log1p(Float64(y * expm1(x))) * c); elseif (y <= 2200000000.0) tmp = Float64(Float64(expm1(Float64(x * 3.0)) * Float64(c * y)) / fma(exp(x), exp(x), Float64(exp(x) + 1.0))); else tmp = Float64(log1p(Float64(y * Float64(fma(0.5, x, 1.0) * x))) * c); end return tmp end
code[c_, x_, y_] := If[LessEqual[y, -1e-39], N[(N[Log[1 + N[(y * N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * c), $MachinePrecision], If[LessEqual[y, 2200000000.0], N[(N[(N[(Exp[N[(x * 3.0), $MachinePrecision]] - 1), $MachinePrecision] * N[(c * y), $MachinePrecision]), $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] * N[Exp[x], $MachinePrecision] + N[(N[Exp[x], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Log[1 + N[(y * N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \cdot 10^{-39}:\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(x\right)\right) \cdot c\\
\mathbf{elif}\;y \leq 2200000000:\\
\;\;\;\;\frac{\mathsf{expm1}\left(x \cdot 3\right) \cdot \left(c \cdot y\right)}{\mathsf{fma}\left(e^{x}, e^{x}, e^{x} + 1\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right)\right) \cdot c\\
\end{array}
\end{array}
if y < -9.99999999999999929e-40Initial program 42.8%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6442.8
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6448.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6448.6
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6499.6
Applied rewrites99.6%
if -9.99999999999999929e-40 < y < 2.2e9Initial program 46.6%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6446.6
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6468.2
lift-*.f64N/A
*-commutativeN/A
lower-*.f6468.2
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6490.1
Applied rewrites90.1%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6490.1
Applied rewrites90.1%
Applied rewrites99.9%
if 2.2e9 < y Initial program 12.2%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6412.2
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6412.2
lift-*.f64N/A
*-commutativeN/A
lower-*.f6412.2
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6499.5
Applied rewrites99.5%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6499.5
Applied rewrites99.5%
(FPCore (c x y) :precision binary64 (if (or (<= y -1e-39) (not (<= y 9.2e-86))) (* (log1p (* y (expm1 x))) c) (* (* c (/ y (fma (+ 1.0 (exp x)) (exp x) 1.0))) (expm1 (* 3.0 x)))))
double code(double c, double x, double y) {
double tmp;
if ((y <= -1e-39) || !(y <= 9.2e-86)) {
tmp = log1p((y * expm1(x))) * c;
} else {
tmp = (c * (y / fma((1.0 + exp(x)), exp(x), 1.0))) * expm1((3.0 * x));
}
return tmp;
}
function code(c, x, y) tmp = 0.0 if ((y <= -1e-39) || !(y <= 9.2e-86)) tmp = Float64(log1p(Float64(y * expm1(x))) * c); else tmp = Float64(Float64(c * Float64(y / fma(Float64(1.0 + exp(x)), exp(x), 1.0))) * expm1(Float64(3.0 * x))); end return tmp end
code[c_, x_, y_] := If[Or[LessEqual[y, -1e-39], N[Not[LessEqual[y, 9.2e-86]], $MachinePrecision]], N[(N[Log[1 + N[(y * N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * c), $MachinePrecision], N[(N[(c * N[(y / N[(N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision] * N[Exp[x], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(Exp[N[(3.0 * x), $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \cdot 10^{-39} \lor \neg \left(y \leq 9.2 \cdot 10^{-86}\right):\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(x\right)\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\left(c \cdot \frac{y}{\mathsf{fma}\left(1 + e^{x}, e^{x}, 1\right)}\right) \cdot \mathsf{expm1}\left(3 \cdot x\right)\\
\end{array}
\end{array}
if y < -9.99999999999999929e-40 or 9.19999999999999985e-86 < y Initial program 32.0%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6432.0
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6439.3
lift-*.f64N/A
*-commutativeN/A
lower-*.f6439.3
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6499.6
Applied rewrites99.6%
if -9.99999999999999929e-40 < y < 9.19999999999999985e-86Initial program 50.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6450.3
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6471.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6471.9
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6487.9
Applied rewrites87.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6487.9
Applied rewrites87.9%
Applied rewrites99.9%
Applied rewrites99.8%
Final simplification99.7%
(FPCore (c x y) :precision binary64 (if (<= (pow (E) x) 0.02) (* (* (expm1 x) y) c) (* (log1p (* y (* (fma 0.5 x 1.0) x))) c)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} \leq 0.02:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right)\right) \cdot c\\
\end{array}
\end{array}
if (pow.f64 (E.f64) x) < 0.0200000000000000004Initial program 53.8%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6453.8
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6499.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.9
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6464.7
Applied rewrites64.7%
if 0.0200000000000000004 < (pow.f64 (E.f64) x) Initial program 35.1%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.1
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6436.2
lift-*.f64N/A
*-commutativeN/A
lower-*.f6436.2
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6492.0
Applied rewrites92.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6491.7
Applied rewrites91.7%
(FPCore (c x y) :precision binary64 (* (log1p (* y (expm1 x))) c))
double code(double c, double x, double y) {
return log1p((y * expm1(x))) * c;
}
public static double code(double c, double x, double y) {
return Math.log1p((y * Math.expm1(x))) * c;
}
def code(c, x, y): return math.log1p((y * math.expm1(x))) * c
function code(c, x, y) return Float64(log1p(Float64(y * expm1(x))) * c) end
code[c_, x_, y_] := N[(N[Log[1 + N[(y * N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * c), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(x\right)\right) \cdot c
\end{array}
Initial program 40.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6440.4
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6454.3
lift-*.f64N/A
*-commutativeN/A
lower-*.f6454.3
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6494.2
Applied rewrites94.2%
(FPCore (c x y) :precision binary64 (if (or (<= y -3.6e+130) (not (<= y 2.5e+191))) (* c (log (fma y x 1.0))) (* (* (expm1 x) y) c)))
double code(double c, double x, double y) {
double tmp;
if ((y <= -3.6e+130) || !(y <= 2.5e+191)) {
tmp = c * log(fma(y, x, 1.0));
} else {
tmp = (expm1(x) * y) * c;
}
return tmp;
}
function code(c, x, y) tmp = 0.0 if ((y <= -3.6e+130) || !(y <= 2.5e+191)) tmp = Float64(c * log(fma(y, x, 1.0))); else tmp = Float64(Float64(expm1(x) * y) * c); end return tmp end
code[c_, x_, y_] := If[Or[LessEqual[y, -3.6e+130], N[Not[LessEqual[y, 2.5e+191]], $MachinePrecision]], N[(c * N[Log[N[(y * x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(N[(Exp[x] - 1), $MachinePrecision] * y), $MachinePrecision] * c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.6 \cdot 10^{+130} \lor \neg \left(y \leq 2.5 \cdot 10^{+191}\right):\\
\;\;\;\;c \cdot \log \left(\mathsf{fma}\left(y, x, 1\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\end{array}
\end{array}
if y < -3.6000000000000001e130 or 2.5000000000000001e191 < y Initial program 32.5%
Taylor expanded in x around 0
+-commutativeN/A
log-EN/A
metadata-evalN/A
log-EN/A
associate-*r*N/A
log-EN/A
metadata-evalN/A
*-rgt-identityN/A
*-commutativeN/A
lower-fma.f6459.6
Applied rewrites59.6%
if -3.6000000000000001e130 < y < 2.5000000000000001e191Initial program 42.5%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6442.5
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6460.1
lift-*.f64N/A
*-commutativeN/A
lower-*.f6460.1
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6492.8
Applied rewrites92.8%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6485.4
Applied rewrites85.4%
Final simplification79.9%
(FPCore (c x y) :precision binary64 (if (<= x -1.02e-157) (* (* (expm1 x) y) c) (* (* x c) y)))
double code(double c, double x, double y) {
double tmp;
if (x <= -1.02e-157) {
tmp = (expm1(x) * y) * c;
} else {
tmp = (x * c) * y;
}
return tmp;
}
public static double code(double c, double x, double y) {
double tmp;
if (x <= -1.02e-157) {
tmp = (Math.expm1(x) * y) * c;
} else {
tmp = (x * c) * y;
}
return tmp;
}
def code(c, x, y): tmp = 0 if x <= -1.02e-157: tmp = (math.expm1(x) * y) * c else: tmp = (x * c) * y return tmp
function code(c, x, y) tmp = 0.0 if (x <= -1.02e-157) tmp = Float64(Float64(expm1(x) * y) * c); else tmp = Float64(Float64(x * c) * y); end return tmp end
code[c_, x_, y_] := If[LessEqual[x, -1.02e-157], N[(N[(N[(Exp[x] - 1), $MachinePrecision] * y), $MachinePrecision] * c), $MachinePrecision], N[(N[(x * c), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.02 \cdot 10^{-157}:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot c\right) \cdot y\\
\end{array}
\end{array}
if x < -1.0200000000000001e-157Initial program 41.8%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6441.8
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6469.1
lift-*.f64N/A
*-commutativeN/A
lower-*.f6469.1
lift--.f64N/A
lift-pow.f64N/A
pow-to-expN/A
lift-E.f64N/A
log-EN/A
*-lft-identityN/A
lower-expm1.f6499.0
Applied rewrites99.0%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6466.2
Applied rewrites66.2%
if -1.0200000000000001e-157 < x Initial program 39.2%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
log-EN/A
lower-*.f64N/A
log-EN/A
metadata-evalN/A
log-EN/A
log-EN/A
metadata-evalN/A
*-rgt-identityN/A
lower-*.f6480.4
Applied rewrites80.4%
Applied rewrites82.0%
(FPCore (c x y) :precision binary64 (if (<= c 1e-72) (* (* c y) x) (* (* x c) y)))
double code(double c, double x, double y) {
double tmp;
if (c <= 1e-72) {
tmp = (c * y) * x;
} else {
tmp = (x * c) * y;
}
return tmp;
}
real(8) function code(c, x, y)
real(8), intent (in) :: c
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (c <= 1d-72) then
tmp = (c * y) * x
else
tmp = (x * c) * y
end if
code = tmp
end function
public static double code(double c, double x, double y) {
double tmp;
if (c <= 1e-72) {
tmp = (c * y) * x;
} else {
tmp = (x * c) * y;
}
return tmp;
}
def code(c, x, y): tmp = 0 if c <= 1e-72: tmp = (c * y) * x else: tmp = (x * c) * y return tmp
function code(c, x, y) tmp = 0.0 if (c <= 1e-72) tmp = Float64(Float64(c * y) * x); else tmp = Float64(Float64(x * c) * y); end return tmp end
function tmp_2 = code(c, x, y) tmp = 0.0; if (c <= 1e-72) tmp = (c * y) * x; else tmp = (x * c) * y; end tmp_2 = tmp; end
code[c_, x_, y_] := If[LessEqual[c, 1e-72], N[(N[(c * y), $MachinePrecision] * x), $MachinePrecision], N[(N[(x * c), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq 10^{-72}:\\
\;\;\;\;\left(c \cdot y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(x \cdot c\right) \cdot y\\
\end{array}
\end{array}
if c < 9.9999999999999997e-73Initial program 45.4%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
log-EN/A
lower-*.f64N/A
log-EN/A
metadata-evalN/A
log-EN/A
log-EN/A
metadata-evalN/A
*-rgt-identityN/A
lower-*.f6463.8
Applied rewrites63.8%
if 9.9999999999999997e-73 < c Initial program 30.4%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
log-EN/A
lower-*.f64N/A
log-EN/A
metadata-evalN/A
log-EN/A
log-EN/A
metadata-evalN/A
*-rgt-identityN/A
lower-*.f6454.9
Applied rewrites54.9%
Applied rewrites57.6%
(FPCore (c x y) :precision binary64 (* (* c y) x))
double code(double c, double x, double y) {
return (c * y) * x;
}
real(8) function code(c, x, y)
real(8), intent (in) :: c
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (c * y) * x
end function
public static double code(double c, double x, double y) {
return (c * y) * x;
}
def code(c, x, y): return (c * y) * x
function code(c, x, y) return Float64(Float64(c * y) * x) end
function tmp = code(c, x, y) tmp = (c * y) * x; end
code[c_, x_, y_] := N[(N[(c * y), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}
\\
\left(c \cdot y\right) \cdot x
\end{array}
Initial program 40.4%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
*-lft-identityN/A
*-commutativeN/A
log-EN/A
lower-*.f64N/A
log-EN/A
metadata-evalN/A
log-EN/A
log-EN/A
metadata-evalN/A
*-rgt-identityN/A
lower-*.f6460.8
Applied rewrites60.8%
(FPCore (c x y) :precision binary64 (* c (log1p (* (expm1 x) y))))
double code(double c, double x, double y) {
return c * log1p((expm1(x) * y));
}
public static double code(double c, double x, double y) {
return c * Math.log1p((Math.expm1(x) * y));
}
def code(c, x, y): return c * math.log1p((math.expm1(x) * y))
function code(c, x, y) return Float64(c * log1p(Float64(expm1(x) * y))) end
code[c_, x_, y_] := N[(c * N[Log[1 + N[(N[(Exp[x] - 1), $MachinePrecision] * y), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(x\right) \cdot y\right)
\end{array}
herbie shell --seed 2024326
(FPCore (c x y)
:name "Logarithmic Transform"
:precision binary64
:alt
(* c (log1p (* (expm1 x) y)))
(* c (log (+ 1.0 (* (- (pow (E) x) 1.0) y)))))