
(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 8 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 (exp (- x))))
(if (<= x -1.6e-162)
(*
(fmod
(exp x)
(fma
(* x x)
(*
(* x x)
(fma
x
(* x -0.003298611111111111)
(+ -0.010416666666666666 (/ -0.25 (* x x)))))
1.0))
t_0)
(if (<= x 0.88)
(*
(fmod
(fma (* x x) (fma x 0.16666666666666666 0.5) x)
(fma (* x x) (fma x (* x -0.010416666666666666) -0.25) 1.0))
(fma x (fma x 0.5 -1.0) 1.0))
(* t_0 (fmod (+ x 1.0) (sqrt (cos x))))))))
double code(double x) {
double t_0 = exp(-x);
double tmp;
if (x <= -1.6e-162) {
tmp = fmod(exp(x), fma((x * x), ((x * x) * fma(x, (x * -0.003298611111111111), (-0.010416666666666666 + (-0.25 / (x * x))))), 1.0)) * t_0;
} else if (x <= 0.88) {
tmp = fmod(fma((x * x), fma(x, 0.16666666666666666, 0.5), x), fma((x * x), fma(x, (x * -0.010416666666666666), -0.25), 1.0)) * fma(x, fma(x, 0.5, -1.0), 1.0);
} else {
tmp = t_0 * fmod((x + 1.0), sqrt(cos(x)));
}
return tmp;
}
function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if (x <= -1.6e-162) tmp = Float64(rem(exp(x), fma(Float64(x * x), Float64(Float64(x * x) * fma(x, Float64(x * -0.003298611111111111), Float64(-0.010416666666666666 + Float64(-0.25 / Float64(x * x))))), 1.0)) * t_0); elseif (x <= 0.88) tmp = Float64(rem(fma(Float64(x * x), fma(x, 0.16666666666666666, 0.5), x), fma(Float64(x * x), fma(x, Float64(x * -0.010416666666666666), -0.25), 1.0)) * fma(x, fma(x, 0.5, -1.0), 1.0)); else tmp = Float64(t_0 * rem(Float64(x + 1.0), sqrt(cos(x)))); end return tmp end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[x, -1.6e-162], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * -0.003298611111111111), $MachinePrecision] + N[(-0.010416666666666666 + N[(-0.25 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision], If[LessEqual[x, 0.88], N[(N[With[{TMP1 = N[(N[(x * x), $MachinePrecision] * N[(x * 0.16666666666666666 + 0.5), $MachinePrecision] + x), $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * -0.010416666666666666), $MachinePrecision] + -0.25), $MachinePrecision] + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[(x * N[(x * 0.5 + -1.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[With[{TMP1 = N[(x + 1.0), $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;x \leq -1.6 \cdot 10^{-162}:\\
\;\;\;\;\left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, \left(x \cdot x\right) \cdot \mathsf{fma}\left(x, x \cdot -0.003298611111111111, -0.010416666666666666 + \frac{-0.25}{x \cdot x}\right), 1\right)\right)\right) \cdot t\_0\\
\mathbf{elif}\;x \leq 0.88:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right), x\right)\right) \bmod \left(\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot -0.010416666666666666, -0.25\right), 1\right)\right)\right) \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.5, -1\right), 1\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot \left(\left(x + 1\right) \bmod \left(\sqrt{\cos x}\right)\right)\\
\end{array}
\end{array}
if x < -1.59999999999999988e-162Initial program 12.6%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
unpow2N/A
associate-*l*N/A
metadata-evalN/A
lower-fma.f64N/A
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6412.6
Applied rewrites12.6%
Taylor expanded in x around inf
Applied rewrites14.5%
if -1.59999999999999988e-162 < x < 0.880000000000000004Initial program 6.7%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f646.6
Applied rewrites6.6%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f646.1
Applied rewrites6.1%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f646.1
Applied rewrites6.1%
Taylor expanded in x around inf
Applied rewrites60.1%
if 0.880000000000000004 < x Initial program 0.3%
Taylor expanded in x around 0
+-commutativeN/A
lower-+.f6498.8
Applied rewrites98.8%
Final simplification60.1%
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))) (t_1 (* t_0 (fmod (exp x) (sqrt (cos x))))))
(if (<= t_1 1e-9)
(*
(fmod
(fma (* x x) (fma x 0.16666666666666666 0.5) x)
(fma (* x x) (fma x (* x -0.010416666666666666) -0.25) 1.0))
(fma x (fma x 0.5 -1.0) 1.0))
(if (<= t_1 2.0)
(*
t_0
(fmod
(exp x)
(fma
(* x x)
(fma
x
(* x (fma (* x x) -0.003298611111111111 -0.010416666666666666))
-0.25)
1.0)))
(* (fmod 1.0 1.0) 1.0)))))
double code(double x) {
double t_0 = exp(-x);
double t_1 = t_0 * fmod(exp(x), sqrt(cos(x)));
double tmp;
if (t_1 <= 1e-9) {
tmp = fmod(fma((x * x), fma(x, 0.16666666666666666, 0.5), x), fma((x * x), fma(x, (x * -0.010416666666666666), -0.25), 1.0)) * fma(x, fma(x, 0.5, -1.0), 1.0);
} else if (t_1 <= 2.0) {
tmp = t_0 * fmod(exp(x), fma((x * x), fma(x, (x * fma((x * x), -0.003298611111111111, -0.010416666666666666)), -0.25), 1.0));
} else {
tmp = fmod(1.0, 1.0) * 1.0;
}
return tmp;
}
function code(x) t_0 = exp(Float64(-x)) t_1 = Float64(t_0 * rem(exp(x), sqrt(cos(x)))) tmp = 0.0 if (t_1 <= 1e-9) tmp = Float64(rem(fma(Float64(x * x), fma(x, 0.16666666666666666, 0.5), x), fma(Float64(x * x), fma(x, Float64(x * -0.010416666666666666), -0.25), 1.0)) * fma(x, fma(x, 0.5, -1.0), 1.0)); elseif (t_1 <= 2.0) tmp = Float64(t_0 * rem(exp(x), fma(Float64(x * x), fma(x, Float64(x * fma(Float64(x * x), -0.003298611111111111, -0.010416666666666666)), -0.25), 1.0))); else tmp = Float64(rem(1.0, 1.0) * 1.0); end return tmp end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, 1e-9], N[(N[With[{TMP1 = N[(N[(x * x), $MachinePrecision] * N[(x * 0.16666666666666666 + 0.5), $MachinePrecision] + x), $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * -0.010416666666666666), $MachinePrecision] + -0.25), $MachinePrecision] + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[(x * N[(x * 0.5 + -1.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 2.0], N[(t$95$0 * N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(x * x), $MachinePrecision] * N[(x * N[(x * N[(N[(x * x), $MachinePrecision] * -0.003298611111111111 + -0.010416666666666666), $MachinePrecision]), $MachinePrecision] + -0.25), $MachinePrecision] + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * 1.0), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
t_1 := t\_0 \cdot \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right)\\
\mathbf{if}\;t\_1 \leq 10^{-9}:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, 0.16666666666666666, 0.5\right), x\right)\right) \bmod \left(\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot -0.010416666666666666, -0.25\right), 1\right)\right)\right) \cdot \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.5, -1\right), 1\right)\\
\mathbf{elif}\;t\_1 \leq 2:\\
\;\;\;\;t\_0 \cdot \left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x, x \cdot \mathsf{fma}\left(x \cdot x, -0.003298611111111111, -0.010416666666666666\right), -0.25\right), 1\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod 1\right) \cdot 1\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 1.00000000000000006e-9Initial program 4.7%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f644.7
Applied rewrites4.7%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f644.7
Applied rewrites4.7%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f644.7
Applied rewrites4.7%
Taylor expanded in x around inf
Applied rewrites51.6%
if 1.00000000000000006e-9 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 2Initial program 93.4%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
unpow2N/A
associate-*l*N/A
metadata-evalN/A
lower-fma.f64N/A
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6488.7
Applied rewrites88.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 rewrites0.0%
Taylor expanded in x around 0
Applied rewrites0.1%
Taylor expanded in x around 0
Applied rewrites97.9%
Final simplification62.3%
herbie shell --seed 2024227
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))