
(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 (or (<= y -1e-14) (not (<= y 300000.0))) (* (log1p (* y (expm1 x))) c) (* (* c (fma (* (pow (expm1 x) 2.0) y) -0.5 (expm1 x))) y)))
double code(double c, double x, double y) {
double tmp;
if ((y <= -1e-14) || !(y <= 300000.0)) {
tmp = log1p((y * expm1(x))) * c;
} else {
tmp = (c * fma((pow(expm1(x), 2.0) * y), -0.5, expm1(x))) * y;
}
return tmp;
}
function code(c, x, y) tmp = 0.0 if ((y <= -1e-14) || !(y <= 300000.0)) tmp = Float64(log1p(Float64(y * expm1(x))) * c); else tmp = Float64(Float64(c * fma(Float64((expm1(x) ^ 2.0) * y), -0.5, expm1(x))) * y); end return tmp end
code[c_, x_, y_] := If[Or[LessEqual[y, -1e-14], N[Not[LessEqual[y, 300000.0]], $MachinePrecision]], N[(N[Log[1 + N[(y * N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * c), $MachinePrecision], N[(N[(c * N[(N[(N[Power[N[(Exp[x] - 1), $MachinePrecision], 2.0], $MachinePrecision] * y), $MachinePrecision] * -0.5 + N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \cdot 10^{-14} \lor \neg \left(y \leq 300000\right):\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \mathsf{expm1}\left(x\right)\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\left(c \cdot \mathsf{fma}\left({\left(\mathsf{expm1}\left(x\right)\right)}^{2} \cdot y, -0.5, \mathsf{expm1}\left(x\right)\right)\right) \cdot y\\
\end{array}
\end{array}
if y < -9.99999999999999999e-15 or 3e5 < y Initial program 35.2%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.2
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6436.3
lift-*.f64N/A
*-commutativeN/A
lower-*.f6436.3
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6499.6
Applied rewrites99.6%
if -9.99999999999999999e-15 < y < 3e5Initial program 43.6%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6443.6
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6463.5
lift-*.f64N/A
*-commutativeN/A
lower-*.f6463.5
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6489.0
Applied rewrites89.0%
Taylor expanded in y around 0
Applied rewrites99.8%
Final simplification99.8%
(FPCore (c x y)
:precision binary64
(if (<= (pow (E) x) 0.0)
(* c (* (expm1 x) y))
(*
(log1p
(*
y
(*
(fma (fma (fma 0.041666666666666664 x 0.16666666666666666) x 0.5) x 1.0)
x)))
c)))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} \leq 0:\\
\;\;\;\;c \cdot \left(\mathsf{expm1}\left(x\right) \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.041666666666666664, x, 0.16666666666666666\right), x, 0.5\right), x, 1\right) \cdot x\right)\right) \cdot c\\
\end{array}
\end{array}
if (pow.f64 (E.f64) x) < 0.0Initial program 55.7%
Taylor expanded in x around 0
*-commutativeN/A
log-EN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
Applied rewrites6.3%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
e-exp-1N/A
exp-prodN/A
*-lft-identityN/A
lower-expm1.f6470.2
Applied rewrites70.2%
if 0.0 < (pow.f64 (E.f64) x) Initial program 34.8%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6434.8
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6435.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.9
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6490.9
Applied rewrites90.9%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6490.8
Applied rewrites90.8%
(FPCore (c x y) :precision binary64 (if (<= (pow (E) x) 0.0) (* c (* (expm1 x) y)) (* (log1p (* y (* (fma (fma 0.16666666666666666 x 0.5) x 1.0) x))) c)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} \leq 0:\\
\;\;\;\;c \cdot \left(\mathsf{expm1}\left(x\right) \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(y \cdot \left(\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666, x, 0.5\right), x, 1\right) \cdot x\right)\right) \cdot c\\
\end{array}
\end{array}
if (pow.f64 (E.f64) x) < 0.0Initial program 55.7%
Taylor expanded in x around 0
*-commutativeN/A
log-EN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
Applied rewrites6.3%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
e-exp-1N/A
exp-prodN/A
*-lft-identityN/A
lower-expm1.f6470.2
Applied rewrites70.2%
if 0.0 < (pow.f64 (E.f64) x) Initial program 34.8%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6434.8
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6435.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.9
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6490.9
Applied rewrites90.9%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6490.6
Applied rewrites90.6%
(FPCore (c x y) :precision binary64 (if (<= (pow (E) x) 0.0) (* c (* (expm1 x) y)) (* (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:\\
\;\;\;\;c \cdot \left(\mathsf{expm1}\left(x\right) \cdot y\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 (pow.f64 (E.f64) x) < 0.0Initial program 55.7%
Taylor expanded in x around 0
*-commutativeN/A
log-EN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
Applied rewrites6.3%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
e-exp-1N/A
exp-prodN/A
*-lft-identityN/A
lower-expm1.f6470.2
Applied rewrites70.2%
if 0.0 < (pow.f64 (E.f64) x) Initial program 34.8%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6434.8
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6435.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.9
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6490.9
Applied rewrites90.9%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6490.5
Applied rewrites90.5%
(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.2%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6440.2
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6452.7
lift-*.f64N/A
*-commutativeN/A
lower-*.f6452.7
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6493.3
Applied rewrites93.3%
(FPCore (c x y) :precision binary64 (if (or (<= y -3.3e+220) (not (<= y 1.35e+138))) (* c (log (fma y x 1.0))) (* c (* (expm1 x) y))))
double code(double c, double x, double y) {
double tmp;
if ((y <= -3.3e+220) || !(y <= 1.35e+138)) {
tmp = c * log(fma(y, x, 1.0));
} else {
tmp = c * (expm1(x) * y);
}
return tmp;
}
function code(c, x, y) tmp = 0.0 if ((y <= -3.3e+220) || !(y <= 1.35e+138)) tmp = Float64(c * log(fma(y, x, 1.0))); else tmp = Float64(c * Float64(expm1(x) * y)); end return tmp end
code[c_, x_, y_] := If[Or[LessEqual[y, -3.3e+220], N[Not[LessEqual[y, 1.35e+138]], $MachinePrecision]], N[(c * N[Log[N[(y * x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(c * N[(N[(Exp[x] - 1), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.3 \cdot 10^{+220} \lor \neg \left(y \leq 1.35 \cdot 10^{+138}\right):\\
\;\;\;\;c \cdot \log \left(\mathsf{fma}\left(y, x, 1\right)\right)\\
\mathbf{else}:\\
\;\;\;\;c \cdot \left(\mathsf{expm1}\left(x\right) \cdot y\right)\\
\end{array}
\end{array}
if y < -3.30000000000000021e220 or 1.35000000000000004e138 < y Initial program 23.9%
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.f6470.5
Applied rewrites70.5%
if -3.30000000000000021e220 < y < 1.35000000000000004e138Initial program 43.0%
Taylor expanded in x around 0
*-commutativeN/A
log-EN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
Applied rewrites62.7%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
e-exp-1N/A
exp-prodN/A
*-lft-identityN/A
lower-expm1.f6483.8
Applied rewrites83.8%
Final simplification81.9%
(FPCore (c x y) :precision binary64 (if (<= x -2.25e-195) (* c (* (expm1 x) y)) (* (* c (* (fma 0.5 x 1.0) y)) x)))
double code(double c, double x, double y) {
double tmp;
if (x <= -2.25e-195) {
tmp = c * (expm1(x) * y);
} else {
tmp = (c * (fma(0.5, x, 1.0) * y)) * x;
}
return tmp;
}
function code(c, x, y) tmp = 0.0 if (x <= -2.25e-195) tmp = Float64(c * Float64(expm1(x) * y)); else tmp = Float64(Float64(c * Float64(fma(0.5, x, 1.0) * y)) * x); end return tmp end
code[c_, x_, y_] := If[LessEqual[x, -2.25e-195], N[(c * N[(N[(Exp[x] - 1), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision], N[(N[(c * N[(N[(0.5 * x + 1.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.25 \cdot 10^{-195}:\\
\;\;\;\;c \cdot \left(\mathsf{expm1}\left(x\right) \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\left(c \cdot \left(\mathsf{fma}\left(0.5, x, 1\right) \cdot y\right)\right) \cdot x\\
\end{array}
\end{array}
if x < -2.25e-195Initial program 42.5%
Taylor expanded in x around 0
*-commutativeN/A
log-EN/A
*-rgt-identityN/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
+-commutativeN/A
Applied rewrites35.7%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
e-exp-1N/A
exp-prodN/A
*-lft-identityN/A
lower-expm1.f6473.1
Applied rewrites73.1%
if -2.25e-195 < x Initial program 38.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6438.3
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6439.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f6439.0
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6487.7
Applied rewrites87.7%
Taylor expanded in x around 0
Applied rewrites84.1%
Taylor expanded in y around 0
Applied rewrites84.5%
Final simplification79.3%
(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.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
metadata-evalN/A
log-EN/A
log-EN/A
metadata-evalN/A
log-EN/A
lower-*.f64N/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
lower-*.f6466.6
Applied rewrites66.6%
(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 2024298
(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)))))