
(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 (fma (* x x) -0.25 1.0)) (t_1 (exp (- x))))
(if (<= x -4.4e-78)
(*
(fmod
(exp x)
(/
(* (pow x 4.0) (- -1.1302806712962962e-6 (* (pow x -6.0) 0.015625)))
(+
(/ (fma 0.0625 (pow x -2.0) -0.0026041666666666665) (* x x))
0.00010850694444444444)))
t_1)
(if (<= x 0.1)
(* (fmod (* (fma 0.5 x 1.0) x) t_0) t_1)
(* (fmod 1.0 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 (x <= -4.4e-78) {
tmp = fmod(exp(x), ((pow(x, 4.0) * (-1.1302806712962962e-6 - (pow(x, -6.0) * 0.015625))) / ((fma(0.0625, pow(x, -2.0), -0.0026041666666666665) / (x * x)) + 0.00010850694444444444))) * t_1;
} else if (x <= 0.1) {
tmp = fmod((fma(0.5, x, 1.0) * x), t_0) * t_1;
} else {
tmp = fmod(1.0, 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 (x <= -4.4e-78) tmp = Float64(rem(exp(x), Float64(Float64((x ^ 4.0) * Float64(-1.1302806712962962e-6 - Float64((x ^ -6.0) * 0.015625))) / Float64(Float64(fma(0.0625, (x ^ -2.0), -0.0026041666666666665) / Float64(x * x)) + 0.00010850694444444444))) * t_1); elseif (x <= 0.1) tmp = Float64(rem(Float64(fma(0.5, x, 1.0) * x), t_0) * t_1); else tmp = Float64(rem(1.0, 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[x, -4.4e-78], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(N[Power[x, 4.0], $MachinePrecision] * N[(-1.1302806712962962e-6 - N[(N[Power[x, -6.0], $MachinePrecision] * 0.015625), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(N[(0.0625 * N[Power[x, -2.0], $MachinePrecision] + -0.0026041666666666665), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision] + 0.00010850694444444444), $MachinePrecision]), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$1), $MachinePrecision], If[LessEqual[x, 0.1], 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 = 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}\;x \leq -4.4 \cdot 10^{-78}:\\
\;\;\;\;\left(\left(e^{x}\right) \bmod \left(\frac{{x}^{4} \cdot \left(-1.1302806712962962 \cdot 10^{-6} - {x}^{-6} \cdot 0.015625\right)}{\frac{\mathsf{fma}\left(0.0625, {x}^{-2}, -0.0026041666666666665\right)}{x \cdot x} + 0.00010850694444444444}\right)\right) \cdot t\_1\\
\mathbf{elif}\;x \leq 0.1:\\
\;\;\;\;\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 t\_0\right) \cdot t\_1\\
\end{array}
\end{array}
if x < -4.3999999999999998e-78Initial program 12.9%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6412.9
Applied rewrites12.9%
Taylor expanded in x around inf
Applied rewrites9.0%
Applied rewrites48.3%
if -4.3999999999999998e-78 < x < 0.10000000000000001Initial program 6.8%
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.f646.0
Applied rewrites6.0%
Taylor expanded in x around inf
Applied rewrites55.2%
if 0.10000000000000001 < 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.1)
(* (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.1) {
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.1) 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.1], 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.1:\\
\;\;\;\;\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.10000000000000001Initial program 6.3%
Taylor expanded in x around 0
Applied rewrites4.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f644.4
Applied rewrites4.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f645.6
Applied rewrites5.6%
Taylor expanded in x around inf
Applied rewrites49.0%
if 0.10000000000000001 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) Initial program 5.9%
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-+.f6493.0
Applied rewrites93.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(-0.25, Float64(x * x), 1.0)) / exp(x)) end
code[x_] := N[(N[With[{TMP1 = N[(1.0 + x), $MachinePrecision], TMP2 = N[(-0.25 * N[(x * x), $MachinePrecision] + 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, x \cdot x, 1\right)\right)\right)}{e^{x}}
\end{array}
Initial program 6.2%
Taylor expanded in x around 0
Applied rewrites21.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6421.6
Applied rewrites21.6%
Taylor expanded in x around 0
lower-+.f6422.9
Applied rewrites22.9%
lift-*.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
un-div-invN/A
lower-/.f6422.9
Applied rewrites22.9%
(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 rewrites21.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6421.6
Applied rewrites21.6%
Taylor expanded in x around 0
lower-+.f6422.9
Applied rewrites22.9%
(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 6.2%
Taylor expanded in x around 0
Applied rewrites21.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6421.6
Applied rewrites21.6%
(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.f644.8
Applied rewrites4.8%
Taylor expanded in x around 0
Applied rewrites4.8%
herbie shell --seed 2024305
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))