
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
double code(double x, double eps) {
return tan((x + eps)) - tan(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = tan((x + eps)) - tan(x)
end function
public static double code(double x, double eps) {
return Math.tan((x + eps)) - Math.tan(x);
}
def code(x, eps): return math.tan((x + eps)) - math.tan(x)
function code(x, eps) return Float64(tan(Float64(x + eps)) - tan(x)) end
function tmp = code(x, eps) tmp = tan((x + eps)) - tan(x); end
code[x_, eps_] := N[(N[Tan[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\tan \left(x + \varepsilon\right) - \tan x
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x eps) :precision binary64 (- (tan (+ x eps)) (tan x)))
double code(double x, double eps) {
return tan((x + eps)) - tan(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = tan((x + eps)) - tan(x)
end function
public static double code(double x, double eps) {
return Math.tan((x + eps)) - Math.tan(x);
}
def code(x, eps): return math.tan((x + eps)) - math.tan(x)
function code(x, eps) return Float64(tan(Float64(x + eps)) - tan(x)) end
function tmp = code(x, eps) tmp = tan((x + eps)) - tan(x); end
code[x_, eps_] := N[(N[Tan[N[(x + eps), $MachinePrecision]], $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\tan \left(x + \varepsilon\right) - \tan x
\end{array}
(FPCore (x eps)
:precision binary64
(let* ((t_0 (/ (pow (sin x) 2.0) (pow (cos x) 2.0)))
(t_1 (/ (sin x) (cos x)))
(t_2 (/ (pow (sin x) 3.0) (pow (cos x) 3.0)))
(t_3
(-
(/ (pow (sin x) 4.0) (pow (cos x) 4.0))
(* -0.3333333333333333 t_0))))
(+
(* (pow eps 2.0) (+ t_1 t_2))
(+
(*
(pow eps 4.0)
(+
(/ (* (sin x) (+ 0.3333333333333333 (pow (tan x) 2.0))) (cos x))
(-
(- (/ (* (sin x) t_3) (cos x)) (* t_2 -0.3333333333333333))
(* t_1 -0.3333333333333333))))
(+
(* eps (+ t_0 1.0))
(* (pow eps 3.0) (+ 0.3333333333333333 (+ t_0 t_3))))))))
double code(double x, double eps) {
double t_0 = pow(sin(x), 2.0) / pow(cos(x), 2.0);
double t_1 = sin(x) / cos(x);
double t_2 = pow(sin(x), 3.0) / pow(cos(x), 3.0);
double t_3 = (pow(sin(x), 4.0) / pow(cos(x), 4.0)) - (-0.3333333333333333 * t_0);
return (pow(eps, 2.0) * (t_1 + t_2)) + ((pow(eps, 4.0) * (((sin(x) * (0.3333333333333333 + pow(tan(x), 2.0))) / cos(x)) + ((((sin(x) * t_3) / cos(x)) - (t_2 * -0.3333333333333333)) - (t_1 * -0.3333333333333333)))) + ((eps * (t_0 + 1.0)) + (pow(eps, 3.0) * (0.3333333333333333 + (t_0 + t_3)))));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
real(8) :: t_0
real(8) :: t_1
real(8) :: t_2
real(8) :: t_3
t_0 = (sin(x) ** 2.0d0) / (cos(x) ** 2.0d0)
t_1 = sin(x) / cos(x)
t_2 = (sin(x) ** 3.0d0) / (cos(x) ** 3.0d0)
t_3 = ((sin(x) ** 4.0d0) / (cos(x) ** 4.0d0)) - ((-0.3333333333333333d0) * t_0)
code = ((eps ** 2.0d0) * (t_1 + t_2)) + (((eps ** 4.0d0) * (((sin(x) * (0.3333333333333333d0 + (tan(x) ** 2.0d0))) / cos(x)) + ((((sin(x) * t_3) / cos(x)) - (t_2 * (-0.3333333333333333d0))) - (t_1 * (-0.3333333333333333d0))))) + ((eps * (t_0 + 1.0d0)) + ((eps ** 3.0d0) * (0.3333333333333333d0 + (t_0 + t_3)))))
end function
public static double code(double x, double eps) {
double t_0 = Math.pow(Math.sin(x), 2.0) / Math.pow(Math.cos(x), 2.0);
double t_1 = Math.sin(x) / Math.cos(x);
double t_2 = Math.pow(Math.sin(x), 3.0) / Math.pow(Math.cos(x), 3.0);
double t_3 = (Math.pow(Math.sin(x), 4.0) / Math.pow(Math.cos(x), 4.0)) - (-0.3333333333333333 * t_0);
return (Math.pow(eps, 2.0) * (t_1 + t_2)) + ((Math.pow(eps, 4.0) * (((Math.sin(x) * (0.3333333333333333 + Math.pow(Math.tan(x), 2.0))) / Math.cos(x)) + ((((Math.sin(x) * t_3) / Math.cos(x)) - (t_2 * -0.3333333333333333)) - (t_1 * -0.3333333333333333)))) + ((eps * (t_0 + 1.0)) + (Math.pow(eps, 3.0) * (0.3333333333333333 + (t_0 + t_3)))));
}
def code(x, eps): t_0 = math.pow(math.sin(x), 2.0) / math.pow(math.cos(x), 2.0) t_1 = math.sin(x) / math.cos(x) t_2 = math.pow(math.sin(x), 3.0) / math.pow(math.cos(x), 3.0) t_3 = (math.pow(math.sin(x), 4.0) / math.pow(math.cos(x), 4.0)) - (-0.3333333333333333 * t_0) return (math.pow(eps, 2.0) * (t_1 + t_2)) + ((math.pow(eps, 4.0) * (((math.sin(x) * (0.3333333333333333 + math.pow(math.tan(x), 2.0))) / math.cos(x)) + ((((math.sin(x) * t_3) / math.cos(x)) - (t_2 * -0.3333333333333333)) - (t_1 * -0.3333333333333333)))) + ((eps * (t_0 + 1.0)) + (math.pow(eps, 3.0) * (0.3333333333333333 + (t_0 + t_3)))))
function code(x, eps) t_0 = Float64((sin(x) ^ 2.0) / (cos(x) ^ 2.0)) t_1 = Float64(sin(x) / cos(x)) t_2 = Float64((sin(x) ^ 3.0) / (cos(x) ^ 3.0)) t_3 = Float64(Float64((sin(x) ^ 4.0) / (cos(x) ^ 4.0)) - Float64(-0.3333333333333333 * t_0)) return Float64(Float64((eps ^ 2.0) * Float64(t_1 + t_2)) + Float64(Float64((eps ^ 4.0) * Float64(Float64(Float64(sin(x) * Float64(0.3333333333333333 + (tan(x) ^ 2.0))) / cos(x)) + Float64(Float64(Float64(Float64(sin(x) * t_3) / cos(x)) - Float64(t_2 * -0.3333333333333333)) - Float64(t_1 * -0.3333333333333333)))) + Float64(Float64(eps * Float64(t_0 + 1.0)) + Float64((eps ^ 3.0) * Float64(0.3333333333333333 + Float64(t_0 + t_3)))))) end
function tmp = code(x, eps) t_0 = (sin(x) ^ 2.0) / (cos(x) ^ 2.0); t_1 = sin(x) / cos(x); t_2 = (sin(x) ^ 3.0) / (cos(x) ^ 3.0); t_3 = ((sin(x) ^ 4.0) / (cos(x) ^ 4.0)) - (-0.3333333333333333 * t_0); tmp = ((eps ^ 2.0) * (t_1 + t_2)) + (((eps ^ 4.0) * (((sin(x) * (0.3333333333333333 + (tan(x) ^ 2.0))) / cos(x)) + ((((sin(x) * t_3) / cos(x)) - (t_2 * -0.3333333333333333)) - (t_1 * -0.3333333333333333)))) + ((eps * (t_0 + 1.0)) + ((eps ^ 3.0) * (0.3333333333333333 + (t_0 + t_3))))); end
code[x_, eps_] := Block[{t$95$0 = N[(N[Power[N[Sin[x], $MachinePrecision], 2.0], $MachinePrecision] / N[Power[N[Cos[x], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[x], $MachinePrecision] / N[Cos[x], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Power[N[Sin[x], $MachinePrecision], 3.0], $MachinePrecision] / N[Power[N[Cos[x], $MachinePrecision], 3.0], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[(N[Power[N[Sin[x], $MachinePrecision], 4.0], $MachinePrecision] / N[Power[N[Cos[x], $MachinePrecision], 4.0], $MachinePrecision]), $MachinePrecision] - N[(-0.3333333333333333 * t$95$0), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[Power[eps, 2.0], $MachinePrecision] * N[(t$95$1 + t$95$2), $MachinePrecision]), $MachinePrecision] + N[(N[(N[Power[eps, 4.0], $MachinePrecision] * N[(N[(N[(N[Sin[x], $MachinePrecision] * N[(0.3333333333333333 + N[Power[N[Tan[x], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Cos[x], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(N[(N[Sin[x], $MachinePrecision] * t$95$3), $MachinePrecision] / N[Cos[x], $MachinePrecision]), $MachinePrecision] - N[(t$95$2 * -0.3333333333333333), $MachinePrecision]), $MachinePrecision] - N[(t$95$1 * -0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(eps * N[(t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[Power[eps, 3.0], $MachinePrecision] * N[(0.3333333333333333 + N[(t$95$0 + t$95$3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{{\sin x}^{2}}{{\cos x}^{2}}\\
t_1 := \frac{\sin x}{\cos x}\\
t_2 := \frac{{\sin x}^{3}}{{\cos x}^{3}}\\
t_3 := \frac{{\sin x}^{4}}{{\cos x}^{4}} - -0.3333333333333333 \cdot t\_0\\
{\varepsilon}^{2} \cdot \left(t\_1 + t\_2\right) + \left({\varepsilon}^{4} \cdot \left(\frac{\sin x \cdot \left(0.3333333333333333 + {\tan x}^{2}\right)}{\cos x} + \left(\left(\frac{\sin x \cdot t\_3}{\cos x} - t\_2 \cdot -0.3333333333333333\right) - t\_1 \cdot -0.3333333333333333\right)\right) + \left(\varepsilon \cdot \left(t\_0 + 1\right) + {\varepsilon}^{3} \cdot \left(0.3333333333333333 + \left(t\_0 + t\_3\right)\right)\right)\right)
\end{array}
\end{array}
Initial program 63.8%
tan-sum64.0%
clear-num63.3%
Applied egg-rr63.3%
Taylor expanded in eps around 0 99.9%
expm1-log1p-u99.9%
expm1-udef99.9%
Applied egg-rr99.9%
expm1-def99.9%
expm1-log1p99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x eps) :precision binary64 (/ (sin eps) (* (cos x) (cos (+ eps x)))))
double code(double x, double eps) {
return sin(eps) / (cos(x) * cos((eps + x)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps) / (cos(x) * cos((eps + x)))
end function
public static double code(double x, double eps) {
return Math.sin(eps) / (Math.cos(x) * Math.cos((eps + x)));
}
def code(x, eps): return math.sin(eps) / (math.cos(x) * math.cos((eps + x)))
function code(x, eps) return Float64(sin(eps) / Float64(cos(x) * cos(Float64(eps + x)))) end
function tmp = code(x, eps) tmp = sin(eps) / (cos(x) * cos((eps + x))); end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] / N[(N[Cos[x], $MachinePrecision] * N[Cos[N[(eps + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin \varepsilon}{\cos x \cdot \cos \left(\varepsilon + x\right)}
\end{array}
Initial program 63.8%
*-un-lft-identity63.8%
*-commutative63.8%
tan-quot63.8%
div-inv63.7%
prod-diff63.7%
Applied egg-rr63.7%
+-commutative63.7%
fma-udef63.7%
*-rgt-identity63.7%
associate-+r+63.7%
unsub-neg63.7%
Simplified63.8%
+-lft-identity63.8%
+-commutative63.8%
tan-sum63.9%
tan-sum63.8%
tan-quot63.8%
frac-sub63.8%
Applied egg-rr63.8%
*-commutative63.8%
fma-neg63.8%
distribute-rgt-neg-out63.8%
*-commutative63.8%
*-commutative63.8%
Simplified63.8%
Taylor expanded in x around 0 99.9%
Final simplification99.9%
(FPCore (x eps) :precision binary64 (/ eps (* (cos x) (cos (+ eps x)))))
double code(double x, double eps) {
return eps / (cos(x) * cos((eps + x)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps / (cos(x) * cos((eps + x)))
end function
public static double code(double x, double eps) {
return eps / (Math.cos(x) * Math.cos((eps + x)));
}
def code(x, eps): return eps / (math.cos(x) * math.cos((eps + x)))
function code(x, eps) return Float64(eps / Float64(cos(x) * cos(Float64(eps + x)))) end
function tmp = code(x, eps) tmp = eps / (cos(x) * cos((eps + x))); end
code[x_, eps_] := N[(eps / N[(N[Cos[x], $MachinePrecision] * N[Cos[N[(eps + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\varepsilon}{\cos x \cdot \cos \left(\varepsilon + x\right)}
\end{array}
Initial program 63.8%
*-un-lft-identity63.8%
*-commutative63.8%
tan-quot63.8%
div-inv63.7%
prod-diff63.7%
Applied egg-rr63.7%
+-commutative63.7%
fma-udef63.7%
*-rgt-identity63.7%
associate-+r+63.7%
unsub-neg63.7%
Simplified63.8%
+-lft-identity63.8%
+-commutative63.8%
tan-sum63.9%
tan-sum63.8%
tan-quot63.8%
frac-sub63.8%
Applied egg-rr63.8%
*-commutative63.8%
fma-neg63.8%
distribute-rgt-neg-out63.8%
*-commutative63.8%
*-commutative63.8%
Simplified63.8%
Taylor expanded in eps around 0 63.9%
+-commutative63.9%
associate-+l+99.7%
*-commutative99.7%
unpow299.7%
unpow299.7%
cos-sin-sum99.7%
*-lft-identity99.7%
distribute-rgt1-in99.7%
metadata-eval99.7%
mul0-lft99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x eps) :precision binary64 (/ (sin eps) (cos eps)))
double code(double x, double eps) {
return sin(eps) / cos(eps);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps) / cos(eps)
end function
public static double code(double x, double eps) {
return Math.sin(eps) / Math.cos(eps);
}
def code(x, eps): return math.sin(eps) / math.cos(eps)
function code(x, eps) return Float64(sin(eps) / cos(eps)) end
function tmp = code(x, eps) tmp = sin(eps) / cos(eps); end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] / N[Cos[eps], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin \varepsilon}{\cos \varepsilon}
\end{array}
Initial program 63.8%
Taylor expanded in x around 0 98.2%
Final simplification98.2%
(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 63.8%
add-cube-cbrt59.4%
pow359.3%
Applied egg-rr59.3%
Taylor expanded in eps around 0 5.6%
pow-base-15.6%
*-lft-identity5.6%
+-inverses5.6%
Simplified5.6%
Final simplification5.6%
(FPCore (x eps) :precision binary64 (/ (sin eps) (* (cos x) (cos (+ x eps)))))
double code(double x, double eps) {
return sin(eps) / (cos(x) * cos((x + eps)));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = sin(eps) / (cos(x) * cos((x + eps)))
end function
public static double code(double x, double eps) {
return Math.sin(eps) / (Math.cos(x) * Math.cos((x + eps)));
}
def code(x, eps): return math.sin(eps) / (math.cos(x) * math.cos((x + eps)))
function code(x, eps) return Float64(sin(eps) / Float64(cos(x) * cos(Float64(x + eps)))) end
function tmp = code(x, eps) tmp = sin(eps) / (cos(x) * cos((x + eps))); end
code[x_, eps_] := N[(N[Sin[eps], $MachinePrecision] / N[(N[Cos[x], $MachinePrecision] * N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\sin \varepsilon}{\cos x \cdot \cos \left(x + \varepsilon\right)}
\end{array}
herbie shell --seed 2024031
(FPCore (x eps)
:name "2tan (problem 3.3.2)"
:precision binary64
:pre (and (and (and (<= -10000.0 x) (<= x 10000.0)) (< (* 1e-16 (fabs x)) eps)) (< eps (fabs x)))
:herbie-target
(/ (sin eps) (* (cos x) (cos (+ x eps))))
(- (tan (+ x eps)) (tan x)))