
(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 7 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 (<= (* t_0 (fmod (exp x) (sqrt (cos x)))) 5e-8)
(* (fmod (* (fma 0.5 x 1.0) x) (fma (* x x) -0.25 1.0)) t_0)
(/ (fmod (+ 1.0 x) (fma (* -0.25 x) x 1.0)) (exp x)))))
double code(double x) {
double t_0 = exp(-x);
double tmp;
if ((t_0 * fmod(exp(x), sqrt(cos(x)))) <= 5e-8) {
tmp = fmod((fma(0.5, x, 1.0) * x), fma((x * x), -0.25, 1.0)) * t_0;
} else {
tmp = fmod((1.0 + x), fma((-0.25 * x), x, 1.0)) / exp(x);
}
return tmp;
}
function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if (Float64(t_0 * rem(exp(x), sqrt(cos(x)))) <= 5e-8) tmp = Float64(rem(Float64(fma(0.5, x, 1.0) * x), fma(Float64(x * x), -0.25, 1.0)) * t_0); else tmp = Float64(rem(Float64(1.0 + x), fma(Float64(-0.25 * x), x, 1.0)) / exp(x)); end return tmp end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, If[LessEqual[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], 5e-8], N[(N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * 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 = N[(1.0 + 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]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-x}\\
\mathbf{if}\;t\_0 \cdot \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \leq 5 \cdot 10^{-8}:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(\left(1 + x\right) \bmod \left(\mathsf{fma}\left(-0.25 \cdot x, x, 1\right)\right)\right)}{e^{x}}\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 4.9999999999999998e-8Initial program 5.4%
Taylor expanded in x around 0
Applied rewrites4.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f644.6
Applied rewrites4.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f645.4
Applied rewrites5.4%
Taylor expanded in x around inf
Applied rewrites57.3%
if 4.9999999999999998e-8 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) Initial program 8.0%
Taylor expanded in x around 0
Applied rewrites90.8%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6490.8
Applied rewrites90.8%
Taylor expanded in x around 0
lower-+.f6495.6
Applied rewrites95.6%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
lower-/.f6495.6
Applied rewrites95.6%
Final simplification64.8%
(FPCore (x) :precision binary64 (if (<= x 5e-8) (* (fmod (exp x) (fma (* x x) -0.25 1.0)) (- 1.0 x)) (* (fmod 1.0 (* -0.25 (* x x))) (exp (- x)))))
double code(double x) {
double tmp;
if (x <= 5e-8) {
tmp = fmod(exp(x), fma((x * x), -0.25, 1.0)) * (1.0 - x);
} else {
tmp = fmod(1.0, (-0.25 * (x * x))) * exp(-x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 5e-8) tmp = Float64(rem(exp(x), fma(Float64(x * x), -0.25, 1.0)) * Float64(1.0 - x)); else tmp = Float64(rem(1.0, Float64(-0.25 * Float64(x * x))) * exp(Float64(-x))); end return tmp end
code[x_] := If[LessEqual[x, 5e-8], 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] * N[(1.0 - x), $MachinePrecision]), $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = N[(-0.25 * N[(x * x), $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5 \cdot 10^{-8}:\\
\;\;\;\;\left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right) \cdot \left(1 - x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod \left(-0.25 \cdot \left(x \cdot x\right)\right)\right) \cdot e^{-x}\\
\end{array}
\end{array}
if x < 4.9999999999999998e-8Initial program 7.2%
Taylor expanded in x around 0
associate-*r*N/A
neg-mul-1N/A
distribute-lft1-inN/A
lower-*.f64N/A
+-commutativeN/A
unsub-negN/A
lower--.f64N/A
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f646.6
Applied rewrites6.6%
Taylor expanded in x around 0
Applied rewrites6.6%
if 4.9999999999999998e-8 < 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%
Taylor expanded in x around inf
Applied rewrites100.0%
Final simplification23.0%
(FPCore (x) :precision binary64 (/ (fmod (+ 1.0 x) (fma (* -0.25 x) x 1.0)) (exp x)))
double code(double x) {
return fmod((1.0 + x), fma((-0.25 * x), x, 1.0)) / exp(x);
}
function code(x) return Float64(rem(Float64(1.0 + x), fma(Float64(-0.25 * x), x, 1.0)) / exp(x)) end
code[x_] := N[(N[With[{TMP1 = N[(1.0 + 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]
\begin{array}{l}
\\
\frac{\left(\left(1 + x\right) \bmod \left(\mathsf{fma}\left(-0.25 \cdot x, x, 1\right)\right)\right)}{e^{x}}
\end{array}
Initial program 5.9%
Taylor expanded in x around 0
Applied rewrites21.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6421.4
Applied rewrites21.4%
Taylor expanded in x around 0
lower-+.f6423.0
Applied rewrites23.0%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
lower-/.f6423.0
Applied rewrites23.0%
(FPCore (x) :precision binary64 (if (<= x 7e-159) (* (- x) (fmod (exp x) (fma (* x x) -0.25 1.0))) (* (fmod 1.0 (* -0.25 (* x x))) (exp (- x)))))
double code(double x) {
double tmp;
if (x <= 7e-159) {
tmp = -x * fmod(exp(x), fma((x * x), -0.25, 1.0));
} else {
tmp = fmod(1.0, (-0.25 * (x * x))) * exp(-x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 7e-159) tmp = Float64(Float64(-x) * rem(exp(x), fma(Float64(x * x), -0.25, 1.0))); else tmp = Float64(rem(1.0, Float64(-0.25 * Float64(x * x))) * exp(Float64(-x))); end return tmp end
code[x_] := If[LessEqual[x, 7e-159], N[((-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]), $MachinePrecision], N[(N[With[{TMP1 = 1.0, TMP2 = N[(-0.25 * N[(x * x), $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 7 \cdot 10^{-159}:\\
\;\;\;\;\left(-x\right) \cdot \left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(x \cdot x, -0.25, 1\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod \left(-0.25 \cdot \left(x \cdot x\right)\right)\right) \cdot e^{-x}\\
\end{array}
\end{array}
if x < 7.00000000000000005e-159Initial program 7.2%
Taylor expanded in x around 0
associate-*r*N/A
neg-mul-1N/A
distribute-lft1-inN/A
lower-*.f64N/A
+-commutativeN/A
unsub-negN/A
lower--.f64N/A
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f646.4
Applied rewrites6.4%
Taylor expanded in x around 0
Applied rewrites6.4%
Taylor expanded in x around inf
Applied rewrites5.0%
if 7.00000000000000005e-159 < x Initial program 4.0%
Taylor expanded in x around 0
Applied rewrites46.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6446.4
Applied rewrites46.4%
Taylor expanded in x around inf
Applied rewrites47.8%
Final simplification22.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 5.9%
Taylor expanded in x around 0
Applied rewrites21.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6421.4
Applied rewrites21.4%
Taylor expanded in x around 0
lower-+.f6423.0
Applied rewrites23.0%
(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 rewrites21.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6421.4
Applied rewrites21.4%
(FPCore (x) :precision binary64 (* (fmod 1.0 (* -0.25 (* x x))) (exp (- x))))
double code(double x) {
return fmod(1.0, (-0.25 * (x * x))) * exp(-x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = mod(1.0d0, ((-0.25d0) * (x * x))) * exp(-x)
end function
def code(x): return math.fmod(1.0, (-0.25 * (x * x))) * math.exp(-x)
function code(x) return Float64(rem(1.0, Float64(-0.25 * Float64(x * x))) * exp(Float64(-x))) end
code[x_] := N[(N[With[{TMP1 = 1.0, TMP2 = N[(-0.25 * N[(x * x), $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 \bmod \left(-0.25 \cdot \left(x \cdot x\right)\right)\right) \cdot e^{-x}
\end{array}
Initial program 5.9%
Taylor expanded in x around 0
Applied rewrites21.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6421.4
Applied rewrites21.4%
Taylor expanded in x around inf
Applied rewrites20.0%
Final simplification20.0%
herbie shell --seed 2024295
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))