
(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
(if (<= (* (exp (- x)) (fmod (exp x) (sqrt (cos x)))) 2.0)
(*
(/ 1.0 (exp x))
(fmod
(exp x)
(fma
(fma
(fma -0.003298611111111111 (* x x) -0.010416666666666666)
(* x x)
-0.25)
(* x x)
1.0)))
(* 1.0 (fmod 1.0 1.0))))
double code(double x) {
double tmp;
if ((exp(-x) * fmod(exp(x), sqrt(cos(x)))) <= 2.0) {
tmp = (1.0 / exp(x)) * fmod(exp(x), fma(fma(fma(-0.003298611111111111, (x * x), -0.010416666666666666), (x * x), -0.25), (x * x), 1.0));
} else {
tmp = 1.0 * fmod(1.0, 1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(exp(Float64(-x)) * rem(exp(x), sqrt(cos(x)))) <= 2.0) tmp = Float64(Float64(1.0 / exp(x)) * rem(exp(x), fma(fma(fma(-0.003298611111111111, Float64(x * x), -0.010416666666666666), Float64(x * x), -0.25), Float64(x * x), 1.0))); else tmp = Float64(1.0 * rem(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], 2.0], N[(N[(1.0 / N[Exp[x], $MachinePrecision]), $MachinePrecision] * N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(N[(-0.003298611111111111 * N[(x * x), $MachinePrecision] + -0.010416666666666666), $MachinePrecision] * N[(x * x), $MachinePrecision] + -0.25), $MachinePrecision] * N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision], N[(1.0 * N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $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 2:\\
\;\;\;\;\frac{1}{e^{x}} \cdot \left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.003298611111111111, x \cdot x, -0.010416666666666666\right), x \cdot x, -0.25\right), x \cdot x, 1\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;1 \cdot \left(1 \bmod 1\right)\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 2Initial program 9.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
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f649.4
Applied rewrites9.4%
lift-exp.f64N/A
lift-neg.f64N/A
exp-negN/A
lift-exp.f64N/A
lower-/.f649.4
Applied rewrites9.4%
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 rewrites98.0%
Taylor expanded in x around 0
Applied rewrites97.9%
Taylor expanded in x around 0
Applied rewrites98.0%
Final simplification26.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (exp (- x))))
(if (<= (* t_0 (fmod (exp x) (sqrt (cos x)))) 2.0)
(*
(fmod
(exp x)
(fma
(fma
(fma -0.003298611111111111 (* x x) -0.010416666666666666)
(* x x)
-0.25)
(* x x)
1.0))
t_0)
(* 1.0 (fmod 1.0 1.0)))))
double code(double x) {
double t_0 = exp(-x);
double tmp;
if ((t_0 * fmod(exp(x), sqrt(cos(x)))) <= 2.0) {
tmp = fmod(exp(x), fma(fma(fma(-0.003298611111111111, (x * x), -0.010416666666666666), (x * x), -0.25), (x * x), 1.0)) * t_0;
} else {
tmp = 1.0 * fmod(1.0, 1.0);
}
return tmp;
}
function code(x) t_0 = exp(Float64(-x)) tmp = 0.0 if (Float64(t_0 * rem(exp(x), sqrt(cos(x)))) <= 2.0) tmp = Float64(rem(exp(x), fma(fma(fma(-0.003298611111111111, Float64(x * x), -0.010416666666666666), Float64(x * x), -0.25), Float64(x * x), 1.0)) * t_0); else tmp = Float64(1.0 * rem(1.0, 1.0)); 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], 2.0], N[(N[With[{TMP1 = N[Exp[x], $MachinePrecision], TMP2 = N[(N[(N[(-0.003298611111111111 * N[(x * x), $MachinePrecision] + -0.010416666666666666), $MachinePrecision] * N[(x * x), $MachinePrecision] + -0.25), $MachinePrecision] * N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision], N[(1.0 * N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $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 2:\\
\;\;\;\;\left(\left(e^{x}\right) \bmod \left(\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-0.003298611111111111, x \cdot x, -0.010416666666666666\right), x \cdot x, -0.25\right), x \cdot x, 1\right)\right)\right) \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;1 \cdot \left(1 \bmod 1\right)\\
\end{array}
\end{array}
if (*.f64 (fmod.f64 (exp.f64 x) (sqrt.f64 (cos.f64 x))) (exp.f64 (neg.f64 x))) < 2Initial program 9.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
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f649.4
Applied rewrites9.4%
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 rewrites98.0%
Taylor expanded in x around 0
Applied rewrites97.9%
Taylor expanded in x around 0
Applied rewrites98.0%
Final simplification26.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (fma (fma 0.5 x -1.0) x 1.0)))
(if (<= x 0.01)
(* (fmod (/ 1.0 t_0) (sqrt (cos x))) t_0)
(* 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 <= 0.01) {
tmp = fmod((1.0 / t_0), sqrt(cos(x))) * t_0;
} else {
tmp = 1.0 * 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 <= 0.01) tmp = Float64(rem(Float64(1.0 / t_0), sqrt(cos(x))) * t_0); else tmp = Float64(1.0 * 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, 0.01], N[(N[With[{TMP1 = N[(1.0 / t$95$0), $MachinePrecision], TMP2 = N[Sqrt[N[Cos[x], $MachinePrecision]], $MachinePrecision]}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * t$95$0), $MachinePrecision], N[(1.0 * N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $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 0.01:\\
\;\;\;\;\left(\left(\frac{1}{t\_0}\right) \bmod \left(\sqrt{\cos x}\right)\right) \cdot t\_0\\
\mathbf{else}:\\
\;\;\;\;1 \cdot \left(1 \bmod 1\right)\\
\end{array}
\end{array}
if x < 0.0100000000000000002Initial program 9.4%
remove-double-divN/A
lift-exp.f64N/A
exp-negN/A
lift-neg.f64N/A
lift-exp.f64N/A
lower-/.f649.4
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.f648.2
Applied rewrites8.2%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f649.4
Applied rewrites9.4%
if 0.0100000000000000002 < x Initial program 0.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites100.0%
Final simplification26.0%
(FPCore (x) :precision binary64 (* (- 1.0 x) (fmod (- x -1.0) 1.0)))
double code(double x) {
return (1.0 - x) * fmod((x - -1.0), 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 - x) * mod((x - (-1.0d0)), 1.0d0)
end function
def code(x): return (1.0 - x) * math.fmod((x - -1.0), 1.0)
function code(x) return Float64(Float64(1.0 - x) * rem(Float64(x - -1.0), 1.0)) end
code[x_] := N[(N[(1.0 - x), $MachinePrecision] * N[With[{TMP1 = N[(x - -1.0), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 - x\right) \cdot \left(\left(x - -1\right) \bmod 1\right)
\end{array}
Initial program 7.6%
Taylor expanded in x around 0
Applied rewrites6.9%
Taylor expanded in x around 0
Applied rewrites5.5%
Taylor expanded in x around 0
+-commutativeN/A
metadata-evalN/A
sub-negN/A
lower--.f6423.5
Applied rewrites23.5%
Taylor expanded in x around 0
neg-mul-1N/A
unsub-negN/A
lower--.f6423.9
Applied rewrites23.9%
Final simplification23.9%
(FPCore (x) :precision binary64 (* (fmod (- x -1.0) 1.0) 1.0))
double code(double x) {
return fmod((x - -1.0), 1.0) * 1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = mod((x - (-1.0d0)), 1.0d0) * 1.0d0
end function
def code(x): return math.fmod((x - -1.0), 1.0) * 1.0
function code(x) return Float64(rem(Float64(x - -1.0), 1.0) * 1.0) end
code[x_] := N[(N[With[{TMP1 = N[(x - -1.0), $MachinePrecision], TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision] * 1.0), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x - -1\right) \bmod 1\right) \cdot 1
\end{array}
Initial program 7.6%
Taylor expanded in x around 0
Applied rewrites6.9%
Taylor expanded in x around 0
Applied rewrites5.5%
Taylor expanded in x around 0
+-commutativeN/A
metadata-evalN/A
sub-negN/A
lower--.f6423.5
Applied rewrites23.5%
(FPCore (x) :precision binary64 (* 1.0 (fmod 1.0 1.0)))
double code(double x) {
return 1.0 * fmod(1.0, 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 * mod(1.0d0, 1.0d0)
end function
def code(x): return 1.0 * math.fmod(1.0, 1.0)
function code(x) return Float64(1.0 * rem(1.0, 1.0)) end
code[x_] := N[(1.0 * N[With[{TMP1 = 1.0, TMP2 = 1.0}, Mod[Abs[TMP1], Abs[TMP2]] * Sign[TMP1]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 \cdot \left(1 \bmod 1\right)
\end{array}
Initial program 7.6%
Taylor expanded in x around 0
Applied rewrites22.4%
Taylor expanded in x around 0
Applied rewrites22.0%
Taylor expanded in x around 0
Applied rewrites22.0%
Final simplification22.0%
herbie shell --seed 2024270
(FPCore (x)
:name "expfmod (used to be hard to sample)"
:precision binary64
(* (fmod (exp x) (sqrt (cos x))) (exp (- x))))