
(FPCore (x) :precision binary64 (* (fmod (exp x) (sqrt (cos x))) (exp (- x))))
double code(double x) {
return fmod(exp(x), sqrt(cos(x))) * exp(-x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = mod(exp(x), sqrt(cos(x))) * exp(-x)
end function
def code(x): return math.fmod(math.exp(x), math.sqrt(math.cos(x))) * math.exp(-x)
function code(x) return Float64(rem(exp(x), sqrt(cos(x))) * exp(Float64(-x))) end
code[x_] := N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (* (fmod (exp x) (sqrt (cos x))) (exp (- x))))
double code(double x) {
return fmod(exp(x), sqrt(cos(x))) * exp(-x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = mod(exp(x), sqrt(cos(x))) * exp(-x)
end function
def code(x): return math.fmod(math.exp(x), math.sqrt(math.cos(x))) * math.exp(-x)
function code(x) return Float64(rem(exp(x), sqrt(cos(x))) * exp(Float64(-x))) end
code[x_] := N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot e^{-x}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (log (fmod (exp x) (fma (* -0.25 x) x 1.0))))
(t_1 (fma (* x x) -0.25 1.0))
(t_2 (exp (- x))))
(if (<= x -2e-310)
(pow (pow (exp -1.0) (- (* x x) (pow t_0 2.0))) (pow (+ t_0 x) -1.0))
(if (<= x 0.5)
(* (fmod (* (fma 0.5 x 1.0) x) t_1) t_2)
(* (fmod 1.0 t_1) t_2)))))
double code(double x) {
double t_0 = log(fmod(exp(x), fma((-0.25 * x), x, 1.0)));
double t_1 = fma((x * x), -0.25, 1.0);
double t_2 = exp(-x);
double tmp;
if (x <= -2e-310) {
tmp = pow(pow(exp(-1.0), ((x * x) - pow(t_0, 2.0))), pow((t_0 + x), -1.0));
} else if (x <= 0.5) {
tmp = fmod((fma(0.5, x, 1.0) * x), t_1) * t_2;
} else {
tmp = fmod(1.0, t_1) * t_2;
}
return tmp;
}
function code(x) t_0 = log(rem(exp(x), fma(Float64(-0.25 * x), x, 1.0))) t_1 = fma(Float64(x * x), -0.25, 1.0) t_2 = exp(Float64(-x)) tmp = 0.0 if (x <= -2e-310) tmp = (exp(-1.0) ^ Float64(Float64(x * x) - (t_0 ^ 2.0))) ^ (Float64(t_0 + x) ^ -1.0); elseif (x <= 0.5) tmp = Float64(rem(Float64(fma(0.5, x, 1.0) * x), t_1) * t_2); else tmp = Float64(rem(1.0, t_1) * t_2); end return tmp end
code[x_] := Block[{t$95$0 = N[Log[N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(-0.25 * x), $MachinePrecision] * x + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Block[{t$95$2 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[x, -2e-310], N[Power[N[Power[N[Exp[-1.0], $MachinePrecision], N[(N[(x * x), $MachinePrecision] - N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[Power[N[(t$95$0 + x), $MachinePrecision], -1.0], $MachinePrecision]], $MachinePrecision], If[LessEqual[x, 0.5], N[(N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], TMP2 = t$95$1}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$2), $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = t$95$1}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$2), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \log \left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(-0.25 \cdot x, x, 1\right)\right)\right)\\
t_1 := \mathsf{fma}\left(x \cdot x, -0.25, 1\right)\\
t_2 := e^{-x}\\
\mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\
\;\;\;\;{\left({\left(e^{-1}\right)}^{\left(x \cdot x - {t\_0}^{2}\right)}\right)}^{\left({\left(t\_0 + x\right)}^{-1}\right)}\\
\mathbf{elif}\;x \leq 0.5:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod t\_1\right) \cdot t\_2\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod t\_1\right) \cdot t\_2\\
\end{array}
\end{array}
if x < -1.999999999999994e-310Initial program 6.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f646.0
Applied rewrites6.0%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
clear-numN/A
lower-/.f64N/A
lower-/.f646.0
Applied rewrites6.0%
lift-/.f64N/A
clear-numN/A
frac-2negN/A
div-invN/A
associate-/r*N/A
lower-/.f64N/A
metadata-evalN/A
frac-2negN/A
lower-/.f64N/A
metadata-evalN/A
frac-2negN/A
lower-/.f646.0
Applied rewrites6.0%
Applied rewrites100.0%
if -1.999999999999994e-310 < x < 0.5Initial program 10.4%
Taylor expanded in x around 0
Applied rewrites5.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f645.9
Applied rewrites5.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f649.7
Applied rewrites9.7%
Taylor expanded in x around inf
Applied rewrites96.9%
if 0.5 < x Initial program 0.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64100.0
Applied rewrites100.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (fma (* x x) -0.25 1.0)) (t_1 (exp (- x))))
(if (<= (* (fmod (exp x) (sqrt (cos x))) t_1) 0.5)
(* (fmod (* (fma 0.5 x 1.0) x) t_0) t_1)
(* (fmod (+ 1.0 x) t_0) t_1))))
double code(double x) {
double t_0 = fma((x * x), -0.25, 1.0);
double t_1 = exp(-x);
double tmp;
if ((fmod(exp(x), sqrt(cos(x))) * t_1) <= 0.5) {
tmp = fmod((fma(0.5, x, 1.0) * x), t_0) * t_1;
} else {
tmp = fmod((1.0 + x), t_0) * t_1;
}
return tmp;
}
function code(x) t_0 = fma(Float64(x * x), -0.25, 1.0) t_1 = exp(Float64(-x)) tmp = 0.0 if (Float64(rem(exp(x), sqrt(cos(x))) * t_1) <= 0.5) tmp = Float64(rem(Float64(fma(0.5, x, 1.0) * x), t_0) * t_1); else tmp = Float64(rem(Float64(1.0 + x), t_0) * t_1); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Block[{t$95$1 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision], 0.5], N[(N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], TMP2 = t$95$0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision], N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = t$95$0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(x \cdot x, -0.25, 1\right)\\
t_1 := e^{-x}\\
\mathbf{if}\;\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot t\_1 \leq 0.5:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod t\_0\right) \cdot t\_1\\
\mathbf{else}:\\
\;\;\;\;\left(\left(1 + x\right) \bmod t\_0\right) \cdot t\_1\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 0.5Initial program 6.7%
Taylor expanded in x around 0
Applied rewrites4.5%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f644.5
Applied rewrites4.5%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f646.3
Applied rewrites6.3%
Taylor expanded in x around inf
Applied rewrites48.6%
if 0.5 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) Initial program 4.8%
Taylor expanded in x around 0
Applied rewrites95.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6495.6
Applied rewrites95.6%
Taylor expanded in x around 0
lower-+.f6498.8
Applied rewrites98.8%
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))) (t_1 (fma (* x x) -0.25 1.0)))
(if (<= (* (fmod (exp x) (sqrt (cos x))) t_0) 2.0)
(fmod (exp x) t_1)
(* (fmod 1.0 t_1) t_0))))
double code(double x) {
double t_0 = exp(-x);
double t_1 = fma((x * x), -0.25, 1.0);
double tmp;
if ((fmod(exp(x), sqrt(cos(x))) * t_0) <= 2.0) {
tmp = fmod(exp(x), t_1);
} else {
tmp = fmod(1.0, t_1) * t_0;
}
return tmp;
}
function code(x) t_0 = exp(Float64(-x)) t_1 = fma(Float64(x * x), -0.25, 1.0) tmp = 0.0 if (Float64(rem(exp(x), sqrt(cos(x))) * t_0) <= 2.0) tmp = rem(exp(x), t_1); else tmp = Float64(rem(1.0, t_1) * t_0); end return tmp end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, If[LessEqual[N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision], 2.0], N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = t$95$1}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = t$95$1}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
t_1 := \mathsf{fma}\left(x \cdot x, -0.25, 1\right)\\
\mathbf{if}\;\left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot t\_0 \leq 2:\\
\;\;\;\;\left(\left(e^{x}\right) \bmod t\_1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod t\_1\right) \cdot t\_0\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 2Initial program 8.1%
Taylor expanded in x around 0
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f646.7
Applied rewrites6.7%
Taylor expanded in x around 0
Applied rewrites6.7%
if 2 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) Initial program 0.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (* (fmod (+ 1.0 x) (fma (* x x) -0.25 1.0)) (exp (- x))))
double code(double x) {
return fmod((1.0 + x), fma((x * x), -0.25, 1.0)) * exp(-x);
}
function code(x) return Float64(rem(Float64(1.0 + x), fma(Float64(x * x), -0.25, 1.0)) * exp(Float64(-x))) end
code[x_] := N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(1 + x\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot e^{-x}
\end{array}
Initial program 6.2%
Taylor expanded in x around 0
Applied rewrites26.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6426.6
Applied rewrites26.6%
Taylor expanded in x around 0
lower-+.f6428.5
Applied rewrites28.5%
(FPCore (x) :precision binary64 (fmod (exp x) (fma (* x x) -0.25 1.0)))
double code(double x) {
return fmod(exp(x), fma((x * x), -0.25, 1.0));
}
function code(x) return rem(exp(x), fma(Float64(x * x), -0.25, 1.0)) end
code[x_] := N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25 + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]
\begin{array}{l}
\\
\left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right)
\end{array}
Initial program 6.2%
Taylor expanded in x around 0
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f645.2
Applied rewrites5.2%
Taylor expanded in x around 0
Applied rewrites5.2%
herbie shell --seed 2024318
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))