
(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 9 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 -1.22e+29) (not (<= y 1300000000.0))) (* c (log1p (* (expm1 x) y))) (* (fma (* (* -0.5 (pow (expm1 x) 2.0)) y) c (* c (expm1 x))) y)))
double code(double c, double x, double y) {
double tmp;
if ((y <= -1.22e+29) || !(y <= 1300000000.0)) {
tmp = c * log1p((expm1(x) * y));
} else {
tmp = fma(((-0.5 * pow(expm1(x), 2.0)) * y), c, (c * expm1(x))) * y;
}
return tmp;
}
function code(c, x, y) tmp = 0.0 if ((y <= -1.22e+29) || !(y <= 1300000000.0)) tmp = Float64(c * log1p(Float64(expm1(x) * y))); else tmp = Float64(fma(Float64(Float64(-0.5 * (expm1(x) ^ 2.0)) * y), c, Float64(c * expm1(x))) * y); end return tmp end
code[c_, x_, y_] := If[Or[LessEqual[y, -1.22e+29], N[Not[LessEqual[y, 1300000000.0]], $MachinePrecision]], N[(c * N[Log[1 + N[(N[(Exp[x] - 1), $MachinePrecision] * y), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(N[(N[(-0.5 * N[Power[N[(Exp[x] - 1), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision] * c + N[(c * N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.22 \cdot 10^{+29} \lor \neg \left(y \leq 1300000000\right):\\
\;\;\;\;c \cdot \mathsf{log1p}\left(\mathsf{expm1}\left(x\right) \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\left(-0.5 \cdot {\left(\mathsf{expm1}\left(x\right)\right)}^{2}\right) \cdot y, c, c \cdot \mathsf{expm1}\left(x\right)\right) \cdot y\\
\end{array}
\end{array}
if y < -1.22e29 or 1.3e9 < y Initial program 33.9%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6433.9
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6433.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6433.9
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 -1.22e29 < y < 1.3e9Initial program 42.5%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6442.5
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6472.3
lift-*.f64N/A
*-commutativeN/A
lower-*.f6472.3
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6488.4
Applied rewrites88.4%
Taylor expanded in y around 0
*-commutativeN/A
+-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
associate-*r*N/A
lower-*.f64N/A
Applied rewrites99.2%
Final simplification99.4%
(FPCore (c x y)
:precision binary64
(if (<= (pow (E) x) 0.8)
(* (* (expm1 x) y) c)
(*
(log1p
(*
(*
(fma (fma (fma 0.041666666666666664 x 0.16666666666666666) x 0.5) x 1.0)
x)
y))
c)))\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} \leq 0.8:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(\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) \cdot y\right) \cdot c\\
\end{array}
\end{array}
if (pow.f64 (E.f64) x) < 0.80000000000000004Initial program 46.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6446.4
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
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6472.6
Applied rewrites72.6%
if 0.80000000000000004 < (pow.f64 (E.f64) x) Initial program 35.7%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.7
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6437.1
lift-*.f64N/A
*-commutativeN/A
lower-*.f6437.1
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 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.f6488.6
Applied rewrites88.6%
Final simplification83.3%
(FPCore (c x y) :precision binary64 (if (<= (pow (E) x) 0.8) (* (* (expm1 x) y) c) (* (log1p (* (* (fma (fma 0.16666666666666666 x 0.5) x 1.0) x) y)) c)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} \leq 0.8:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(\left(\mathsf{fma}\left(\mathsf{fma}\left(0.16666666666666666, x, 0.5\right), x, 1\right) \cdot x\right) \cdot y\right) \cdot c\\
\end{array}
\end{array}
if (pow.f64 (E.f64) x) < 0.80000000000000004Initial program 46.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6446.4
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
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6472.6
Applied rewrites72.6%
if 0.80000000000000004 < (pow.f64 (E.f64) x) Initial program 35.7%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.7
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6437.1
lift-*.f64N/A
*-commutativeN/A
lower-*.f6437.1
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 x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6488.6
Applied rewrites88.6%
Final simplification83.3%
(FPCore (c x y) :precision binary64 (if (<= (pow (E) x) 0.8) (* (* (expm1 x) y) c) (* (log1p (* (* (fma 0.5 x 1.0) x) y)) c)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} \leq 0.8:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \cdot y\right) \cdot c\\
\end{array}
\end{array}
if (pow.f64 (E.f64) x) < 0.80000000000000004Initial program 46.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6446.4
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
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6472.6
Applied rewrites72.6%
if 0.80000000000000004 < (pow.f64 (E.f64) x) Initial program 35.7%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.7
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6437.1
lift-*.f64N/A
*-commutativeN/A
lower-*.f6437.1
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 x around 0
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6488.4
Applied rewrites88.4%
Final simplification83.1%
(FPCore (c x y) :precision binary64 (if (<= (pow (E) x) 0.8) (* (* (expm1 x) y) c) (* (log1p (* x y)) c)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} \leq 0.8:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(x \cdot y\right) \cdot c\\
\end{array}
\end{array}
if (pow.f64 (E.f64) x) < 0.80000000000000004Initial program 46.4%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6446.4
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
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6499.9
Applied rewrites99.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6472.6
Applied rewrites72.6%
if 0.80000000000000004 < (pow.f64 (E.f64) x) Initial program 35.7%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6435.7
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6437.1
lift-*.f64N/A
*-commutativeN/A
lower-*.f6437.1
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 x around 0
lower-*.f6487.6
Applied rewrites87.6%
(FPCore (c x y) :precision binary64 (if (<= (pow (E) x) 0.999999999999998) (* (* (expm1 x) y) c) (* (* c y) x)))
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;{\mathsf{E}\left(\right)}^{x} \leq 0.999999999999998:\\
\;\;\;\;\left(\mathsf{expm1}\left(x\right) \cdot y\right) \cdot c\\
\mathbf{else}:\\
\;\;\;\;\left(c \cdot y\right) \cdot x\\
\end{array}
\end{array}
if (pow.f64 (E.f64) x) < 0.999999999999998Initial program 45.9%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6445.9
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6497.2
lift-*.f64N/A
*-commutativeN/A
lower-*.f6497.2
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6499.8
Applied rewrites99.8%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64N/A
lower-expm1.f6471.0
Applied rewrites71.0%
if 0.999999999999998 < (pow.f64 (E.f64) x) Initial program 35.7%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-rgt-identityN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-lft-identityN/A
*-commutativeN/A
log-EN/A
lower-*.f64N/A
*-commutativeN/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
lower-*.f6479.8
Applied rewrites79.8%
Final simplification76.7%
(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}
Initial program 39.3%
lift-*.f64N/A
*-commutativeN/A
lower-*.f6439.3
lift-log.f64N/A
lift-+.f64N/A
lower-log1p.f6457.9
lift-*.f64N/A
*-commutativeN/A
lower-*.f6457.9
lift--.f64N/A
lift-pow.f64N/A
lift-E.f64N/A
e-exp-1N/A
pow-expN/A
*-lft-identityN/A
lower-expm1.f6492.6
Applied rewrites92.6%
Final simplification92.6%
(FPCore (c x y) :precision binary64 (if (<= c 1.2e-30) (* (* c y) x) (* (* c x) y)))
double code(double c, double x, double y) {
double tmp;
if (c <= 1.2e-30) {
tmp = (c * y) * x;
} else {
tmp = (c * x) * 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 <= 1.2d-30) then
tmp = (c * y) * x
else
tmp = (c * x) * y
end if
code = tmp
end function
public static double code(double c, double x, double y) {
double tmp;
if (c <= 1.2e-30) {
tmp = (c * y) * x;
} else {
tmp = (c * x) * y;
}
return tmp;
}
def code(c, x, y): tmp = 0 if c <= 1.2e-30: tmp = (c * y) * x else: tmp = (c * x) * y return tmp
function code(c, x, y) tmp = 0.0 if (c <= 1.2e-30) tmp = Float64(Float64(c * y) * x); else tmp = Float64(Float64(c * x) * y); end return tmp end
function tmp_2 = code(c, x, y) tmp = 0.0; if (c <= 1.2e-30) tmp = (c * y) * x; else tmp = (c * x) * y; end tmp_2 = tmp; end
code[c_, x_, y_] := If[LessEqual[c, 1.2e-30], N[(N[(c * y), $MachinePrecision] * x), $MachinePrecision], N[(N[(c * x), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq 1.2 \cdot 10^{-30}:\\
\;\;\;\;\left(c \cdot y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(c \cdot x\right) \cdot y\\
\end{array}
\end{array}
if c < 1.19999999999999992e-30Initial program 49.9%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-rgt-identityN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-lft-identityN/A
*-commutativeN/A
log-EN/A
lower-*.f64N/A
*-commutativeN/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
lower-*.f6461.5
Applied rewrites61.5%
if 1.19999999999999992e-30 < c Initial program 13.7%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-rgt-identityN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-lft-identityN/A
*-commutativeN/A
log-EN/A
lower-*.f64N/A
*-commutativeN/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
lower-*.f6452.1
Applied rewrites52.1%
Applied rewrites58.6%
Final simplification60.7%
(FPCore (c x y) :precision binary64 (* (* c x) y))
double code(double c, double x, double y) {
return (c * x) * y;
}
real(8) function code(c, x, y)
real(8), intent (in) :: c
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (c * x) * y
end function
public static double code(double c, double x, double y) {
return (c * x) * y;
}
def code(c, x, y): return (c * x) * y
function code(c, x, y) return Float64(Float64(c * x) * y) end
function tmp = code(c, x, y) tmp = (c * x) * y; end
code[c_, x_, y_] := N[(N[(c * x), $MachinePrecision] * y), $MachinePrecision]
\begin{array}{l}
\\
\left(c \cdot x\right) \cdot y
\end{array}
Initial program 39.3%
Taylor expanded in x around 0
associate-*r*N/A
log-EN/A
*-rgt-identityN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-lft-identityN/A
*-commutativeN/A
log-EN/A
lower-*.f64N/A
*-commutativeN/A
log-EN/A
*-commutativeN/A
*-lft-identityN/A
lower-*.f6458.8
Applied rewrites58.8%
Applied rewrites56.3%
(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 2024271
(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)))))