
(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 6 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 (sqrt (cos x))) (t_1 (exp (- x))))
(if (<= (* (fmod (exp x) t_0) t_1) 0.5)
(* (fmod (* (fma 0.5 x 1.0) x) t_0) t_1)
(* (fmod 1.0 (* (* x x) -0.25)) t_1))))
double code(double x) {
double t_0 = sqrt(cos(x));
double t_1 = exp(-x);
double tmp;
if ((fmod(exp(x), t_0) * t_1) <= 0.5) {
tmp = fmod((fma(0.5, x, 1.0) * x), t_0) * t_1;
} else {
tmp = fmod(1.0, ((x * x) * -0.25)) * t_1;
}
return tmp;
}
function code(x) t_0 = sqrt(cos(x)) t_1 = exp(Float64(-x)) tmp = 0.0 if (Float64(rem(exp(x), t_0) * t_1) <= 0.5) tmp = Float64(rem(Float64(fma(0.5, x, 1.0) * x), t_0) * t_1); else tmp = Float64(rem(1.0, Float64(Float64(x * x) * -0.25)) * t_1); end return tmp end
code[x_] := Block[{t$95$0 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = t$95$0}, 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 = 1.0, TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \sqrt{\cos x}\\
t_1 := e^{-x}\\
\mathbf{if}\;\left(\left(e^{x}\right) \bmod t\_0\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(1 \bmod \left(\left(x \cdot x\right) \cdot -0.25\right)\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.1%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f645.3
Applied rewrites5.3%
Taylor expanded in x around inf
Applied rewrites53.8%
if 0.5 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) Initial program 5.3%
Taylor expanded in x around 0
Applied rewrites95.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6495.0
Applied rewrites95.0%
Taylor expanded in x around inf
Applied rewrites95.2%
(FPCore (x) :precision binary64 (* (exp (- x)) (fmod (/ 1.0 (/ 1.0 x)) (sqrt (cos x)))))
double code(double x) {
return exp(-x) * fmod((1.0 / (1.0 / x)), sqrt(cos(x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = exp(-x) * mod((1.0d0 / (1.0d0 / x)), sqrt(cos(x)))
end function
def code(x): return math.exp(-x) * math.fmod((1.0 / (1.0 / x)), math.sqrt(math.cos(x)))
function code(x) return Float64(exp(Float64(-x)) * rem(Float64(1.0 / Float64(1.0 / x)), sqrt(cos(x)))) end
code[x_] := N[(N[Exp[(-x)], $MachinePrecision] * N[With[{TMP1 = N[(1.0 / N[(1.0 / x), $MachinePrecision]), $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
e^{-x} \cdot \left(\left(\frac{1}{\frac{1}{x}}\right) \bmod \left(\sqrt{\cos x}\right)\right)
\end{array}
Initial program 5.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6414.9
Applied rewrites14.9%
Taylor expanded in x around inf
Applied rewrites51.8%
Applied rewrites48.6%
Taylor expanded in x around 0
Applied rewrites62.6%
Final simplification62.6%
(FPCore (x) :precision binary64 (if (<= x 1.15) (/ (fmod (exp x) (fma (* -0.25 x) x 1.0)) (exp x)) (* (fmod 1.0 (* (* x x) -0.25)) (exp (- x)))))
double code(double x) {
double tmp;
if (x <= 1.15) {
tmp = fmod(exp(x), fma((-0.25 * x), x, 1.0)) / exp(x);
} else {
tmp = fmod(1.0, ((x * x) * -0.25)) * exp(-x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 1.15) tmp = Float64(rem(exp(x), fma(Float64(-0.25 * x), x, 1.0)) / exp(x)); else tmp = Float64(rem(1.0, Float64(Float64(x * x) * -0.25)) * exp(Float64(-x))); end return tmp end
code[x_] := If[LessEqual[x, 1.15], N[(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] / N[Exp[x], $MachinePrecision]), $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.15:\\
\;\;\;\;\frac{\left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(-0.25 \cdot x, x, 1\right)\right)\right)}{e^{x}}\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod \left(\left(x \cdot x\right) \cdot -0.25\right)\right) \cdot e^{-x}\\
\end{array}
\end{array}
if x < 1.1499999999999999Initial program 7.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f646.7
Applied rewrites6.7%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
lower-/.f646.7
Applied rewrites6.7%
if 1.1499999999999999 < x Initial program 1.7%
Taylor expanded in x around 0
Applied rewrites98.5%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6498.5
Applied rewrites98.5%
Taylor expanded in x around inf
Applied rewrites98.7%
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))))
(if (<= x 1.15)
(* (fmod (exp x) (fma (* x x) -0.25 1.0)) t_0)
(* (fmod 1.0 (* (* x x) -0.25)) t_0))))
double code(double x) {
double t_0 = exp(-x);
double tmp;
if (x <= 1.15) {
tmp = fmod(exp(x), fma((x * x), -0.25, 1.0)) * t_0;
} else {
tmp = fmod(1.0, ((x * x) * -0.25)) * t_0;
}
return tmp;
}
function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if (x <= 1.15) tmp = Float64(rem(exp(x), fma(Float64(x * x), -0.25, 1.0)) * t_0); else tmp = Float64(rem(1.0, Float64(Float64(x * x) * -0.25)) * t_0); end return tmp end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[x, 1.15], N[(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] * t$95$0), $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;x \leq 1.15:\\
\;\;\;\;\left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod \left(\left(x \cdot x\right) \cdot -0.25\right)\right) \cdot t\_0\\
\end{array}
\end{array}
if x < 1.1499999999999999Initial program 7.0%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f646.7
Applied rewrites6.7%
if 1.1499999999999999 < x Initial program 1.7%
Taylor expanded in x around 0
Applied rewrites98.5%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6498.5
Applied rewrites98.5%
Taylor expanded in x around inf
Applied rewrites98.7%
(FPCore (x) :precision binary64 (* (fmod 1.0 (fma (* x x) -0.25 1.0)) (exp (- x))))
double code(double x) {
return fmod(1.0, fma((x * x), -0.25, 1.0)) * exp(-x);
}
function code(x) return Float64(rem(1.0, fma(Float64(x * x), -0.25, 1.0)) * exp(Float64(-x))) end
code[x_] := N[(N[With[{TMP1 = 1.0, 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(1 \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot e^{-x}
\end{array}
Initial program 5.9%
Taylor expanded in x around 0
Applied rewrites24.3%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6424.3
Applied rewrites24.3%
(FPCore (x) :precision binary64 (* (fmod 1.0 (* (* x x) -0.25)) (exp (- x))))
double code(double x) {
return fmod(1.0, ((x * x) * -0.25)) * exp(-x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = mod(1.0d0, ((x * x) * (-0.25d0))) * exp(-x)
end function
def code(x): return math.fmod(1.0, ((x * x) * -0.25)) * math.exp(-x)
function code(x) return Float64(rem(1.0, Float64(Float64(x * x) * -0.25)) * exp(Float64(-x))) end
code[x_] := N[(N[With[{TMP1 = 1.0, TMP2 = N[(N[(x * x), $MachinePrecision] * -0.25), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 \bmod \left(\left(x \cdot x\right) \cdot -0.25\right)\right) \cdot e^{-x}
\end{array}
Initial program 5.9%
Taylor expanded in x around 0
Applied rewrites24.3%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6424.3
Applied rewrites24.3%
Taylor expanded in x around inf
Applied rewrites23.2%
herbie shell --seed 2024332
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))