
(FPCore (x eps) :precision binary64 (- (sin (+ x eps)) (sin x)))
double code(double x, double eps) {
return sin((x + eps)) - sin(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin((x + eps)) - sin(x)
end function
public static double code(double x, double eps) {
return Math.sin((x + eps)) - Math.sin(x);
}
def code(x, eps): return math.sin((x + eps)) - math.sin(x)
function code(x, eps) return Float64(sin(Float64(x + eps)) - sin(x)) end
function tmp = code(x, eps) tmp = sin((x + eps)) - sin(x); end
code[x_, eps_] := N[(N[Sin[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \left(x + \varepsilon\right) - \sin x
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (- (sin (+ x eps)) (sin x)))
double code(double x, double eps) {
return sin((x + eps)) - sin(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin((x + eps)) - sin(x)
end function
public static double code(double x, double eps) {
return Math.sin((x + eps)) - Math.sin(x);
}
def code(x, eps): return math.sin((x + eps)) - math.sin(x)
function code(x, eps) return Float64(sin(Float64(x + eps)) - sin(x)) end
function tmp = code(x, eps) tmp = sin((x + eps)) - sin(x); end
code[x_, eps_] := N[(N[Sin[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \left(x + \varepsilon\right) - \sin x
\end{array}
(FPCore (x eps) :precision binary64 (fma (* (sin x) (- (sin eps))) (tan (* eps 0.5)) (* (sin eps) (cos x))))
double code(double x, double eps) {
return fma((sin(x) * -sin(eps)), tan((eps * 0.5)), (sin(eps) * cos(x)));
}
function code(x, eps) return fma(Float64(sin(x) * Float64(-sin(eps))), tan(Float64(eps * 0.5)), Float64(sin(eps) * cos(x))) end
code[x_, eps_] := N[(N[(N[Sin[x], $MachinePrecision] * (-N[Sin[eps], $MachinePrecision])), $MachinePrecision] * N[Tan[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] + N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin x \cdot \left(-\sin \varepsilon\right), \tan \left(\varepsilon \cdot 0.5\right), \sin \varepsilon \cdot \cos x\right)
\end{array}
Initial program 40.5%
sin-sum64.5%
associate--l+64.5%
Applied egg-rr64.5%
+-commutative64.5%
sub-neg64.5%
associate-+l+99.2%
*-commutative99.2%
neg-mul-199.2%
*-commutative99.2%
distribute-rgt-out99.2%
+-commutative99.2%
Simplified99.2%
flip-+99.1%
metadata-eval99.1%
sub-1-cos99.4%
pow299.4%
Applied egg-rr99.4%
Taylor expanded in eps around inf 99.4%
mul-1-neg99.4%
unpow299.4%
+-commutative99.4%
associate-*r/99.4%
distribute-lft-neg-in99.4%
+-commutative99.4%
hang-0p-tan99.6%
Simplified99.6%
+-commutative99.6%
associate-*r*99.6%
fma-def99.6%
div-inv99.6%
metadata-eval99.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (x eps) :precision binary64 (- (* (sin eps) (cos x)) (* (sin eps) (* (sin x) (tan (* eps 0.5))))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) - (sin(eps) * (sin(x) * tan((eps * 0.5))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (sin(eps) * cos(x)) - (sin(eps) * (sin(x) * tan((eps * 0.5d0))))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) - (Math.sin(eps) * (Math.sin(x) * Math.tan((eps * 0.5))));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) - (math.sin(eps) * (math.sin(x) * math.tan((eps * 0.5))))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) - Float64(sin(eps) * Float64(sin(x) * tan(Float64(eps * 0.5))))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) - (sin(eps) * (sin(x) * tan((eps * 0.5)))); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[eps], $MachinePrecision] * N[(N[Sin[x], $MachinePrecision] * N[Tan[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x - \sin \varepsilon \cdot \left(\sin x \cdot \tan \left(\varepsilon \cdot 0.5\right)\right)
\end{array}
Initial program 40.5%
sin-sum64.5%
associate--l+64.5%
Applied egg-rr64.5%
+-commutative64.5%
sub-neg64.5%
associate-+l+99.2%
*-commutative99.2%
neg-mul-199.2%
*-commutative99.2%
distribute-rgt-out99.2%
+-commutative99.2%
Simplified99.2%
flip-+99.1%
metadata-eval99.1%
sub-1-cos99.4%
pow299.4%
Applied egg-rr99.4%
Taylor expanded in eps around inf 99.4%
mul-1-neg99.4%
unpow299.4%
+-commutative99.4%
associate-*r/99.4%
distribute-lft-neg-in99.4%
+-commutative99.4%
hang-0p-tan99.6%
Simplified99.6%
expm1-log1p-u97.7%
expm1-udef97.2%
div-inv97.2%
metadata-eval97.2%
Applied egg-rr97.2%
expm1-def97.7%
expm1-log1p99.6%
*-commutative99.6%
distribute-lft-neg-in99.6%
distribute-rgt-neg-in99.6%
associate-*l*99.6%
*-commutative99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (x eps) :precision binary64 (+ (* (sin eps) (cos x)) (- (* (sin x) (cos eps)) (sin x))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) + ((sin(x) * cos(eps)) - sin(x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (sin(eps) * cos(x)) + ((sin(x) * cos(eps)) - sin(x))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) + ((Math.sin(x) * Math.cos(eps)) - Math.sin(x));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) + ((math.sin(x) * math.cos(eps)) - math.sin(x))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) + Float64(Float64(sin(x) * cos(eps)) - sin(x))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) + ((sin(x) * cos(eps)) - sin(x)); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Sin[x], $MachinePrecision] * N[Cos[eps], $MachinePrecision]), $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x + \left(\sin x \cdot \cos \varepsilon - \sin x\right)
\end{array}
Initial program 40.5%
sin-sum64.5%
associate--l+64.5%
Applied egg-rr64.5%
+-commutative64.5%
sub-neg64.5%
associate-+l+99.2%
*-commutative99.2%
neg-mul-199.2%
*-commutative99.2%
distribute-rgt-out99.2%
+-commutative99.2%
Simplified99.2%
flip-+99.1%
metadata-eval99.1%
sub-1-cos99.4%
pow299.4%
Applied egg-rr99.4%
unpow299.4%
sub-1-cos99.1%
metadata-eval99.1%
flip-+99.2%
distribute-rgt-in99.2%
neg-mul-199.2%
Applied egg-rr99.2%
Final simplification99.2%
(FPCore (x eps) :precision binary64 (+ (* (sin eps) (cos x)) (* (sin x) (+ (cos eps) -1.0))))
double code(double x, double eps) {
return (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + -1.0));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + (-1.0d0)))
end function
public static double code(double x, double eps) {
return (Math.sin(eps) * Math.cos(x)) + (Math.sin(x) * (Math.cos(eps) + -1.0));
}
def code(x, eps): return (math.sin(eps) * math.cos(x)) + (math.sin(x) * (math.cos(eps) + -1.0))
function code(x, eps) return Float64(Float64(sin(eps) * cos(x)) + Float64(sin(x) * Float64(cos(eps) + -1.0))) end
function tmp = code(x, eps) tmp = (sin(eps) * cos(x)) + (sin(x) * (cos(eps) + -1.0)); end
code[x_, eps_] := N[(N[(N[Sin[eps], $MachinePrecision] * N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[x], $MachinePrecision] * N[(N[Cos[eps], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon \cdot \cos x + \sin x \cdot \left(\cos \varepsilon + -1\right)
\end{array}
Initial program 40.5%
sin-sum64.5%
associate--l+64.5%
Applied egg-rr64.5%
+-commutative64.5%
sub-neg64.5%
associate-+l+99.2%
*-commutative99.2%
neg-mul-199.2%
*-commutative99.2%
distribute-rgt-out99.2%
+-commutative99.2%
Simplified99.2%
Final simplification99.2%
(FPCore (x eps) :precision binary64 (* 2.0 (* (sin (* eps 0.5)) (cos (* 0.5 (+ eps (+ x x)))))))
double code(double x, double eps) {
return 2.0 * (sin((eps * 0.5)) * cos((0.5 * (eps + (x + x)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (sin((eps * 0.5d0)) * cos((0.5d0 * (eps + (x + x)))))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.sin((eps * 0.5)) * Math.cos((0.5 * (eps + (x + x)))));
}
def code(x, eps): return 2.0 * (math.sin((eps * 0.5)) * math.cos((0.5 * (eps + (x + x)))))
function code(x, eps) return Float64(2.0 * Float64(sin(Float64(eps * 0.5)) * cos(Float64(0.5 * Float64(eps + Float64(x + x)))))) end
function tmp = code(x, eps) tmp = 2.0 * (sin((eps * 0.5)) * cos((0.5 * (eps + (x + x))))); end
code[x_, eps_] := N[(2.0 * N[(N[Sin[N[(eps * 0.5), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(0.5 * N[(eps + N[(x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\sin \left(\varepsilon \cdot 0.5\right) \cdot \cos \left(0.5 \cdot \left(\varepsilon + \left(x + x\right)\right)\right)\right)
\end{array}
Initial program 40.5%
diff-sin40.1%
div-inv40.1%
metadata-eval40.1%
div-inv40.1%
+-commutative40.1%
metadata-eval40.1%
Applied egg-rr40.1%
*-commutative40.1%
+-commutative40.1%
associate--l+76.2%
+-inverses76.2%
distribute-lft-in76.2%
metadata-eval76.2%
*-commutative76.2%
associate-+r+76.1%
+-commutative76.1%
Simplified76.1%
Final simplification76.1%
(FPCore (x eps) :precision binary64 (if (<= eps -0.235) (sin eps) (if (<= eps 0.044) (* eps (cos x)) (sin eps))))
double code(double x, double eps) {
double tmp;
if (eps <= -0.235) {
tmp = sin(eps);
} else if (eps <= 0.044) {
tmp = eps * cos(x);
} else {
tmp = sin(eps);
}
return tmp;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: tmp
if (eps <= (-0.235d0)) then
tmp = sin(eps)
else if (eps <= 0.044d0) then
tmp = eps * cos(x)
else
tmp = sin(eps)
end if
code = tmp
end function
public static double code(double x, double eps) {
double tmp;
if (eps <= -0.235) {
tmp = Math.sin(eps);
} else if (eps <= 0.044) {
tmp = eps * Math.cos(x);
} else {
tmp = Math.sin(eps);
}
return tmp;
}
def code(x, eps): tmp = 0 if eps <= -0.235: tmp = math.sin(eps) elif eps <= 0.044: tmp = eps * math.cos(x) else: tmp = math.sin(eps) return tmp
function code(x, eps) tmp = 0.0 if (eps <= -0.235) tmp = sin(eps); elseif (eps <= 0.044) tmp = Float64(eps * cos(x)); else tmp = sin(eps); end return tmp end
function tmp_2 = code(x, eps) tmp = 0.0; if (eps <= -0.235) tmp = sin(eps); elseif (eps <= 0.044) tmp = eps * cos(x); else tmp = sin(eps); end tmp_2 = tmp; end
code[x_, eps_] := If[LessEqual[eps, -0.235], N[Sin[eps], $MachinePrecision], If[LessEqual[eps, 0.044], N[(eps * N[Cos[x], $MachinePrecision]), $MachinePrecision], N[Sin[eps], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\varepsilon \leq -0.235:\\
\;\;\;\;\sin \varepsilon\\
\mathbf{elif}\;\varepsilon \leq 0.044:\\
\;\;\;\;\varepsilon \cdot \cos x\\
\mathbf{else}:\\
\;\;\;\;\sin \varepsilon\\
\end{array}
\end{array}
if eps < -0.23499999999999999 or 0.043999999999999997 < eps Initial program 52.5%
Taylor expanded in x around 0 53.5%
if -0.23499999999999999 < eps < 0.043999999999999997Initial program 29.4%
Taylor expanded in eps around 0 97.5%
Final simplification76.4%
(FPCore (x eps) :precision binary64 (sin eps))
double code(double x, double eps) {
return sin(eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps)
end function
public static double code(double x, double eps) {
return Math.sin(eps);
}
def code(x, eps): return math.sin(eps)
function code(x, eps) return sin(eps) end
function tmp = code(x, eps) tmp = sin(eps); end
code[x_, eps_] := N[Sin[eps], $MachinePrecision]
\begin{array}{l}
\\
\sin \varepsilon
\end{array}
Initial program 40.5%
Taylor expanded in x around 0 52.4%
Final simplification52.4%
(FPCore (x eps) :precision binary64 0.0)
double code(double x, double eps) {
return 0.0;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 0.0d0
end function
public static double code(double x, double eps) {
return 0.0;
}
def code(x, eps): return 0.0
function code(x, eps) return 0.0 end
function tmp = code(x, eps) tmp = 0.0; end
code[x_, eps_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 40.5%
add-cube-cbrt39.3%
pow339.3%
Applied egg-rr39.3%
Taylor expanded in eps around 0 4.2%
pow-base-14.2%
*-lft-identity4.2%
+-inverses4.2%
Simplified4.2%
Final simplification4.2%
(FPCore (x eps) :precision binary64 eps)
double code(double x, double eps) {
return eps;
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps
end function
public static double code(double x, double eps) {
return eps;
}
def code(x, eps): return eps
function code(x, eps) return eps end
function tmp = code(x, eps) tmp = eps; end
code[x_, eps_] := eps
\begin{array}{l}
\\
\varepsilon
\end{array}
Initial program 40.5%
Taylor expanded in x around 0 52.4%
Taylor expanded in eps around 0 28.3%
Final simplification28.3%
(FPCore (x eps) :precision binary64 (* 2.0 (* (cos (+ x (/ eps 2.0))) (sin (/ eps 2.0)))))
double code(double x, double eps) {
return 2.0 * (cos((x + (eps / 2.0))) * sin((eps / 2.0)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = 2.0d0 * (cos((x + (eps / 2.0d0))) * sin((eps / 2.0d0)))
end function
public static double code(double x, double eps) {
return 2.0 * (Math.cos((x + (eps / 2.0))) * Math.sin((eps / 2.0)));
}
def code(x, eps): return 2.0 * (math.cos((x + (eps / 2.0))) * math.sin((eps / 2.0)))
function code(x, eps) return Float64(2.0 * Float64(cos(Float64(x + Float64(eps / 2.0))) * sin(Float64(eps / 2.0)))) end
function tmp = code(x, eps) tmp = 2.0 * (cos((x + (eps / 2.0))) * sin((eps / 2.0))); end
code[x_, eps_] := N[(2.0 * N[(N[Cos[N[(x + N[(eps / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(eps / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
2 \cdot \left(\cos \left(x + \frac{\varepsilon}{2}\right) \cdot \sin \left(\frac{\varepsilon}{2}\right)\right)
\end{array}
herbie shell --seed 2023213
(FPCore (x eps)
:name "2sin (example 3.3)"
:precision binary64
:herbie-target
(* 2.0 (* (cos (+ x (/ eps 2.0))) (sin (/ eps 2.0))))
(- (sin (+ x eps)) (sin x)))