
(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 (fma (fma -0.16666666666666666 x 0.5) x -1.0) x 1.0)))
(if (<= x -4e-310)
(* (fmod (/ 1.0 t_0) 1.0) t_0)
(if (<= x 5e-5)
(* (fma (fma 0.5 x -1.0) x 1.0) (fmod (* (fma 0.5 x 1.0) x) 1.0))
(fmod 1.0 1.0)))))
double code(double x) {
double t_0 = fma(fma(fma(-0.16666666666666666, x, 0.5), x, -1.0), x, 1.0);
double tmp;
if (x <= -4e-310) {
tmp = fmod((1.0 / t_0), 1.0) * t_0;
} else if (x <= 5e-5) {
tmp = fma(fma(0.5, x, -1.0), x, 1.0) * fmod((fma(0.5, x, 1.0) * x), 1.0);
} else {
tmp = fmod(1.0, 1.0);
}
return tmp;
}
function code(x) t_0 = fma(fma(fma(-0.16666666666666666, x, 0.5), x, -1.0), x, 1.0) tmp = 0.0 if (x <= -4e-310) tmp = Float64(rem(Float64(1.0 / t_0), 1.0) * t_0); elseif (x <= 5e-5) tmp = Float64(fma(fma(0.5, x, -1.0), x, 1.0) * rem(Float64(fma(0.5, x, 1.0) * x), 1.0)); else tmp = rem(1.0, 1.0); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(N[(-0.16666666666666666 * x + 0.5), $MachinePrecision] * x + -1.0), $MachinePrecision] * x + 1.0), $MachinePrecision]}, If[LessEqual[x, -4e-310], N[(N[With[{TMP1 = N[(1.0 / t$95$0), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision], If[LessEqual[x, 5e-5], N[(N[(N[(0.5 * x + -1.0), $MachinePrecision] * x + 1.0), $MachinePrecision] * N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision], N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.16666666666666666, x, 0.5\right), x, -1\right), x, 1\right)\\
\mathbf{if}\;x \leq -4 \cdot 10^{-310}:\\
\;\;\;\;\left(\left(\frac{1}{t\_0}\right) \bmod 1\right) \cdot t\_0\\
\mathbf{elif}\;x \leq 5 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 1\right) \cdot \left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod 1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod 1\right)\\
\end{array}
\end{array}
if x < -3.999999999999988e-310Initial program 9.4%
Taylor expanded in x around 0
Applied rewrites9.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f647.5
Applied rewrites7.5%
/-rgt-identityN/A
clear-numN/A
lift-exp.f64N/A
exp-negN/A
lift-neg.f64N/A
lift-exp.f64N/A
lower-/.f647.6
Applied rewrites7.6%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f649.4
Applied rewrites9.4%
if -3.999999999999988e-310 < x < 5.00000000000000024e-5Initial program 7.9%
Taylor expanded in x around 0
Applied rewrites7.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f647.7
Applied rewrites7.7%
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.5%
if 5.00000000000000024e-5 < x Initial program 0.0%
Taylor expanded in x around 0
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f640.0
Applied rewrites0.0%
Taylor expanded in x around 0
Applied rewrites0.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Final simplification66.8%
(FPCore (x) :precision binary64 (if (<= (* (exp (- x)) (fmod (exp x) (sqrt (cos x)))) 5e-5) (* (fma (fma 0.5 x -1.0) x 1.0) (fmod (* (fma 0.5 x 1.0) x) 1.0)) (fmod (- x -1.0) 1.0)))
double code(double x) {
double tmp;
if ((exp(-x) * fmod(exp(x), sqrt(cos(x)))) <= 5e-5) {
tmp = fma(fma(0.5, x, -1.0), x, 1.0) * fmod((fma(0.5, x, 1.0) * x), 1.0);
} else {
tmp = fmod((x - -1.0), 1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(exp(Float64(-x)) * rem(exp(x), sqrt(cos(x)))) <= 5e-5) tmp = Float64(fma(fma(0.5, x, -1.0), x, 1.0) * rem(Float64(fma(0.5, x, 1.0) * x), 1.0)); else tmp = rem(Float64(x - -1.0), 1.0); end return tmp end
code[x_] := If[LessEqual[N[(N[Exp[(-x)], $MachinePrecision] * 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-5], N[(N[(N[(0.5 * x + -1.0), $MachinePrecision] * x + 1.0), $MachinePrecision] * N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision], N[With[{TMP1 = N[(x - -1.0), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;e^{-x} \cdot \left(\left(e^{x}\right) \bmod \left(\sqrt{\cos x}\right)\right) \leq 5 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 1\right) \cdot \left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod 1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(\left(x - -1\right) \bmod 1\right)\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 5.00000000000000024e-5Initial program 5.9%
Taylor expanded in x around 0
Applied rewrites5.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f645.7
Applied rewrites5.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f645.7
Applied rewrites5.7%
Taylor expanded in x around inf
Applied rewrites57.6%
if 5.00000000000000024e-5 < (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) Initial program 11.0%
Taylor expanded in x around 0
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f644.6
Applied rewrites4.6%
Taylor expanded in x around 0
Applied rewrites4.6%
Taylor expanded in x around 0
Applied rewrites89.8%
Final simplification64.4%
(FPCore (x)
:precision binary64
(let* ((t_0 (fma (fma 0.5 x -1.0) x 1.0)))
(if (<= x -4e-310)
(* (fmod (/ 1.0 (/ 1.0 (fma (fma 0.5 x 1.0) x 1.0))) 1.0) t_0)
(if (<= x 5e-5)
(* t_0 (fmod (* (fma 0.5 x 1.0) x) 1.0))
(fmod 1.0 1.0)))))
double code(double x) {
double t_0 = fma(fma(0.5, x, -1.0), x, 1.0);
double tmp;
if (x <= -4e-310) {
tmp = fmod((1.0 / (1.0 / fma(fma(0.5, x, 1.0), x, 1.0))), 1.0) * t_0;
} else if (x <= 5e-5) {
tmp = t_0 * fmod((fma(0.5, x, 1.0) * x), 1.0);
} else {
tmp = fmod(1.0, 1.0);
}
return tmp;
}
function code(x) t_0 = fma(fma(0.5, x, -1.0), x, 1.0) tmp = 0.0 if (x <= -4e-310) tmp = Float64(rem(Float64(1.0 / Float64(1.0 / fma(fma(0.5, x, 1.0), x, 1.0))), 1.0) * t_0); elseif (x <= 5e-5) tmp = Float64(t_0 * rem(Float64(fma(0.5, x, 1.0) * x), 1.0)); else tmp = rem(1.0, 1.0); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(0.5 * x + -1.0), $MachinePrecision] * x + 1.0), $MachinePrecision]}, If[LessEqual[x, -4e-310], N[(N[With[{TMP1 = N[(1.0 / N[(1.0 / N[(N[(0.5 * x + 1.0), $MachinePrecision] * x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision], If[LessEqual[x, 5e-5], N[(t$95$0 * N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision], N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 1\right)\\
\mathbf{if}\;x \leq -4 \cdot 10^{-310}:\\
\;\;\;\;\left(\left(\frac{1}{\frac{1}{\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, 1\right), x, 1\right)}}\right) \bmod 1\right) \cdot t\_0\\
\mathbf{elif}\;x \leq 5 \cdot 10^{-5}:\\
\;\;\;\;t\_0 \cdot \left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod 1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod 1\right)\\
\end{array}
\end{array}
if x < -3.999999999999988e-310Initial program 9.4%
Taylor expanded in x around 0
Applied rewrites9.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f647.3
Applied rewrites7.3%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f647.5
Applied rewrites7.5%
Applied rewrites7.6%
if -3.999999999999988e-310 < x < 5.00000000000000024e-5Initial program 7.9%
Taylor expanded in x around 0
Applied rewrites7.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f647.7
Applied rewrites7.7%
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.5%
if 5.00000000000000024e-5 < x Initial program 0.0%
Taylor expanded in x around 0
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f640.0
Applied rewrites0.0%
Taylor expanded in x around 0
Applied rewrites0.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Final simplification66.1%
(FPCore (x)
:precision binary64
(let* ((t_0 (fma (fma 0.5 x -1.0) x 1.0)))
(if (<= x -4e-310)
(* (fmod (fma (fma 0.5 x 1.0) x 1.0) 1.0) t_0)
(if (<= x 5e-5)
(* t_0 (fmod (* (fma 0.5 x 1.0) x) 1.0))
(fmod 1.0 1.0)))))
double code(double x) {
double t_0 = fma(fma(0.5, x, -1.0), x, 1.0);
double tmp;
if (x <= -4e-310) {
tmp = fmod(fma(fma(0.5, x, 1.0), x, 1.0), 1.0) * t_0;
} else if (x <= 5e-5) {
tmp = t_0 * fmod((fma(0.5, x, 1.0) * x), 1.0);
} else {
tmp = fmod(1.0, 1.0);
}
return tmp;
}
function code(x) t_0 = fma(fma(0.5, x, -1.0), x, 1.0) tmp = 0.0 if (x <= -4e-310) tmp = Float64(rem(fma(fma(0.5, x, 1.0), x, 1.0), 1.0) * t_0); elseif (x <= 5e-5) tmp = Float64(t_0 * rem(Float64(fma(0.5, x, 1.0) * x), 1.0)); else tmp = rem(1.0, 1.0); end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(0.5 * x + -1.0), $MachinePrecision] * x + 1.0), $MachinePrecision]}, If[LessEqual[x, -4e-310], N[(N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x + 1.0), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision], If[LessEqual[x, 5e-5], N[(t$95$0 * N[With[{TMP1 = N[(N[(0.5 * x + 1.0), $MachinePrecision] * x), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision], N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\mathsf{fma}\left(0.5, x, -1\right), x, 1\right)\\
\mathbf{if}\;x \leq -4 \cdot 10^{-310}:\\
\;\;\;\;\left(\left(\mathsf{fma}\left(\mathsf{fma}\left(0.5, x, 1\right), x, 1\right)\right) \bmod 1\right) \cdot t\_0\\
\mathbf{elif}\;x \leq 5 \cdot 10^{-5}:\\
\;\;\;\;t\_0 \cdot \left(\left(\mathsf{fma}\left(0.5, x, 1\right) \cdot x\right) \bmod 1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 \bmod 1\right)\\
\end{array}
\end{array}
if x < -3.999999999999988e-310Initial program 9.4%
Taylor expanded in x around 0
Applied rewrites9.4%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f647.3
Applied rewrites7.3%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f647.5
Applied rewrites7.5%
if -3.999999999999988e-310 < x < 5.00000000000000024e-5Initial program 7.9%
Taylor expanded in x around 0
Applied rewrites7.7%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f647.7
Applied rewrites7.7%
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.5%
if 5.00000000000000024e-5 < x Initial program 0.0%
Taylor expanded in x around 0
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f640.0
Applied rewrites0.0%
Taylor expanded in x around 0
Applied rewrites0.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Final simplification66.1%
(FPCore (x) :precision binary64 (fmod (- x -1.0) 1.0))
double code(double x) {
return fmod((x - -1.0), 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = mod((x - (-1.0d0)), 1.0d0)
end function
def code(x): return math.fmod((x - -1.0), 1.0)
function code(x) return rem(Float64(x - -1.0), 1.0) end
code[x_] := N[With[{TMP1 = N[(x - -1.0), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x - -1\right) \bmod 1\right)
\end{array}
Initial program 7.0%
Taylor expanded in x around 0
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f645.5
Applied rewrites5.5%
Taylor expanded in x around 0
Applied rewrites5.5%
Taylor expanded in x around 0
Applied rewrites23.5%
(FPCore (x) :precision binary64 (fmod 1.0 1.0))
double code(double x) {
return fmod(1.0, 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = mod(1.0d0, 1.0d0)
end function
def code(x): return math.fmod(1.0, 1.0)
function code(x) return rem(1.0, 1.0) end
code[x_] := N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]
\begin{array}{l}
\\
\left(1 \bmod 1\right)
\end{array}
Initial program 7.0%
Taylor expanded in x around 0
lower-fmod.f64N/A
lower-exp.f64N/A
lower-sqrt.f64N/A
lower-cos.f645.5
Applied rewrites5.5%
Taylor expanded in x around 0
Applied rewrites5.5%
Taylor expanded in x around 0
Applied rewrites22.3%
herbie shell --seed 2024283
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))