
(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 3 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 (exp (- x))))
(if (<= x -1e-309)
(pow (pow (exp (- (* x x) (pow t_0 2.0))) (pow (+ t_0 x) -1.0)) -1.0)
(if (<= x 0.2)
(* (fmod (* (fma 0.5 x 1.0) x) (fma -0.25 (* x x) 1.0)) t_1)
t_1))))
double code(double x) {
double t_0 = log(fmod(exp(x), fma((-0.25 * x), x, 1.0)));
double t_1 = exp(-x);
double tmp;
if (x <= -1e-309) {
tmp = pow(pow(exp(((x * x) - pow(t_0, 2.0))), pow((t_0 + x), -1.0)), -1.0);
} else if (x <= 0.2) {
tmp = fmod((fma(0.5, x, 1.0) * x), fma(-0.25, (x * x), 1.0)) * t_1;
} else {
tmp = t_1;
}
return tmp;
}
function code(x) t_0 = log(rem(exp(x), fma(Float64(-0.25 * x), x, 1.0))) t_1 = exp(Float64(-x)) tmp = 0.0 if (x <= -1e-309) tmp = (exp(Float64(Float64(x * x) - (t_0 ^ 2.0))) ^ (Float64(t_0 + x) ^ -1.0)) ^ -1.0; elseif (x <= 0.2) tmp = Float64(rem(Float64(fma(0.5, x, 1.0) * x), fma(-0.25, Float64(x * x), 1.0)) * t_1); else tmp = t_1; 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[Exp[(-x)], $MachinePrecision]}, If[LessEqual[x, -1e-309], N[Power[N[Power[N[Exp[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], -1.0], $MachinePrecision], If[LessEqual[x, 0.2], N[(N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], TMP2 = N[(-0.25 * N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision], t$95$1]]]]
\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 := e^{-x}\\
\mathbf{if}\;x \leq -1 \cdot 10^{-309}:\\
\;\;\;\;{\left({\left(e^{x \cdot x - {t\_0}^{2}}\right)}^{\left({\left(t\_0 + x\right)}^{-1}\right)}\right)}^{-1}\\
\mathbf{elif}\;x \leq 0.2:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod \left(\mathsf{fma}\left(-0.25, x \cdot x, 1\right)\right)\right) \cdot t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if x < -1.000000000000002e-309Initial program 10.6%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
lower-/.f6410.6
Applied rewrites10.6%
Taylor expanded in x around 0
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
associate-*l*N/A
mul-1-negN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites10.6%
lift-/.f64N/A
clear-numN/A
lower-/.f64N/A
lower-/.f6410.6
Applied rewrites10.6%
lift-/.f64N/A
rem-exp-logN/A
lift-log.f64N/A
lift-exp.f64N/A
exp-diffN/A
flip--N/A
div-invN/A
exp-prodN/A
Applied rewrites100.0%
if -1.000000000000002e-309 < x < 0.20000000000000001Initial program 8.4%
Taylor expanded in x around 0
Applied rewrites5.5%
Taylor expanded in x around 0
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
associate-*l*N/A
mul-1-negN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites5.5%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f647.7
Applied rewrites7.7%
Taylor expanded in x around inf
Applied rewrites98.2%
if 0.20000000000000001 < x Initial program 0.0%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
lower-/.f640.0
Applied rewrites0.0%
Taylor expanded in x around 0
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
associate-*l*N/A
mul-1-negN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites0.0%
lift-/.f64N/A
clear-numN/A
inv-powN/A
pow-to-expN/A
lower-exp.f64N/A
lower-*.f64N/A
Applied rewrites0.0%
Taylor expanded in x around inf
neg-mul-1N/A
lower-neg.f64100.0
Applied rewrites100.0%
Final simplification99.3%
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))))
(if (or (<= x -1e-309) (not (<= x 0.2)))
t_0
(* (fmod (* (fma 0.5 x 1.0) x) (fma -0.25 (* x x) 1.0)) t_0))))
double code(double x) {
double t_0 = exp(-x);
double tmp;
if ((x <= -1e-309) || !(x <= 0.2)) {
tmp = t_0;
} else {
tmp = fmod((fma(0.5, x, 1.0) * x), fma(-0.25, (x * x), 1.0)) * t_0;
}
return tmp;
}
function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if ((x <= -1e-309) || !(x <= 0.2)) tmp = t_0; else tmp = Float64(rem(Float64(fma(0.5, x, 1.0) * x), fma(-0.25, Float64(x * x), 1.0)) * t_0); end return tmp end
code[x_] := Block[{t$95$0 = N[Exp[(-x)], $MachinePrecision]}, If[Or[LessEqual[x, -1e-309], N[Not[LessEqual[x, 0.2]], $MachinePrecision]], t$95$0, N[(N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], TMP2 = N[(-0.25 * N[(x * x), $MachinePrecision] + 1.0), $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 \cdot 10^{-309} \lor \neg \left(x \leq 0.2\right):\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod \left(\mathsf{fma}\left(-0.25, x \cdot x, 1\right)\right)\right) \cdot t\_0\\
\end{array}
\end{array}
if x < -1.000000000000002e-309 or 0.20000000000000001 < x Initial program 7.0%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
lower-/.f647.0
Applied rewrites7.0%
Taylor expanded in x around 0
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
associate-*l*N/A
mul-1-negN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites7.0%
lift-/.f64N/A
clear-numN/A
inv-powN/A
pow-to-expN/A
lower-exp.f64N/A
lower-*.f64N/A
Applied rewrites7.1%
Taylor expanded in x around inf
neg-mul-1N/A
lower-neg.f6495.8
Applied rewrites95.8%
if -1.000000000000002e-309 < x < 0.20000000000000001Initial program 8.4%
Taylor expanded in x around 0
Applied rewrites5.5%
Taylor expanded in x around 0
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
associate-*l*N/A
mul-1-negN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites5.5%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f647.7
Applied rewrites7.7%
Taylor expanded in x around inf
Applied rewrites98.2%
Final simplification96.7%
(FPCore (x) :precision binary64 (exp (- x)))
double code(double x) {
return exp(-x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = exp(-x)
end function
public static double code(double x) {
return Math.exp(-x);
}
def code(x): return math.exp(-x)
function code(x) return exp(Float64(-x)) end
function tmp = code(x) tmp = exp(-x); end
code[x_] := N[Exp[(-x)], $MachinePrecision]
\begin{array}{l}
\\
e^{-x}
\end{array}
Initial program 7.5%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
lower-/.f647.5
Applied rewrites7.5%
Taylor expanded in x around 0
+-commutativeN/A
metadata-evalN/A
metadata-evalN/A
lft-mult-inverseN/A
distribute-rgt-neg-outN/A
mul-1-negN/A
associate-*l*N/A
mul-1-negN/A
distribute-rgt-neg-inN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites7.3%
lift-/.f64N/A
clear-numN/A
inv-powN/A
pow-to-expN/A
lower-exp.f64N/A
lower-*.f64N/A
Applied rewrites7.3%
Taylor expanded in x around inf
neg-mul-1N/A
lower-neg.f6460.2
Applied rewrites60.2%
herbie shell --seed 2024320
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))