
(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 9 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 (/ (fma eps (* -0.16666666666666666 (* eps eps)) eps) (* (cos x) (fma eps (- (* eps (* (cos x) -0.5)) (sin x)) (cos x)))))
double code(double x, double eps) {
return fma(eps, (-0.16666666666666666 * (eps * eps)), eps) / (cos(x) * fma(eps, ((eps * (cos(x) * -0.5)) - sin(x)), cos(x)));
}
function code(x, eps) return Float64(fma(eps, Float64(-0.16666666666666666 * Float64(eps * eps)), eps) / Float64(cos(x) * fma(eps, Float64(Float64(eps * Float64(cos(x) * -0.5)) - sin(x)), cos(x)))) end
code[x_, eps_] := N[(N[(eps * N[(-0.16666666666666666 * N[(eps * eps), $MachinePrecision]), $MachinePrecision] + eps), $MachinePrecision] / N[(N[Cos[x], $MachinePrecision] * N[(eps * N[(N[(eps * N[(N[Cos[x], $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision] + N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(\varepsilon, -0.16666666666666666 \cdot \left(\varepsilon \cdot \varepsilon\right), \varepsilon\right)}{\cos x \cdot \mathsf{fma}\left(\varepsilon, \varepsilon \cdot \left(\cos x \cdot -0.5\right) - \sin x, \cos x\right)}
\end{array}
Initial program 62.1%
lift--.f64N/A
lift-tan.f64N/A
tan-quotN/A
lift-tan.f64N/A
tan-quotN/A
frac-subN/A
lower-/.f64N/A
sin-diffN/A
lower-sin.f64N/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6462.1
Applied rewrites62.1%
Taylor expanded in eps around 0
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.7
Applied rewrites99.7%
Taylor expanded in eps around 0
+-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower--.f64N/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-sin.f64N/A
lower-cos.f6499.7
Applied rewrites99.7%
(FPCore (x eps) :precision binary64 (/ (fma eps (* -0.16666666666666666 (* eps eps)) eps) (* (cos x) (cos (+ x eps)))))
double code(double x, double eps) {
return fma(eps, (-0.16666666666666666 * (eps * eps)), eps) / (cos(x) * cos((x + eps)));
}
function code(x, eps) return Float64(fma(eps, Float64(-0.16666666666666666 * Float64(eps * eps)), eps) / Float64(cos(x) * cos(Float64(x + eps)))) end
code[x_, eps_] := N[(N[(eps * N[(-0.16666666666666666 * N[(eps * eps), $MachinePrecision]), $MachinePrecision] + eps), $MachinePrecision] / N[(N[Cos[x], $MachinePrecision] * N[Cos[N[(x + eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(\varepsilon, -0.16666666666666666 \cdot \left(\varepsilon \cdot \varepsilon\right), \varepsilon\right)}{\cos x \cdot \cos \left(x + \varepsilon\right)}
\end{array}
Initial program 62.1%
lift--.f64N/A
lift-tan.f64N/A
tan-quotN/A
lift-tan.f64N/A
tan-quotN/A
frac-subN/A
lower-/.f64N/A
sin-diffN/A
lower-sin.f64N/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6462.1
Applied rewrites62.1%
Taylor expanded in eps around 0
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.7
Applied rewrites99.7%
(FPCore (x eps) :precision binary64 (fma (/ 1.0 (+ 0.5 (* 0.5 (cos (+ x x))))) eps (* (* eps eps) (fma eps 0.3333333333333333 x))))
double code(double x, double eps) {
return fma((1.0 / (0.5 + (0.5 * cos((x + x))))), eps, ((eps * eps) * fma(eps, 0.3333333333333333, x)));
}
function code(x, eps) return fma(Float64(1.0 / Float64(0.5 + Float64(0.5 * cos(Float64(x + x))))), eps, Float64(Float64(eps * eps) * fma(eps, 0.3333333333333333, x))) end
code[x_, eps_] := N[(N[(1.0 / N[(0.5 + N[(0.5 * N[Cos[N[(x + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * eps + N[(N[(eps * eps), $MachinePrecision] * N[(eps * 0.3333333333333333 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{1}{0.5 + 0.5 \cdot \cos \left(x + x\right)}, \varepsilon, \left(\varepsilon \cdot \varepsilon\right) \cdot \mathsf{fma}\left(\varepsilon, 0.3333333333333333, x\right)\right)
\end{array}
Initial program 62.1%
lift--.f64N/A
lift-tan.f64N/A
tan-quotN/A
lift-tan.f64N/A
tan-quotN/A
frac-subN/A
lower-/.f64N/A
sin-diffN/A
lower-sin.f64N/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6462.1
Applied rewrites62.1%
Taylor expanded in eps around 0
lower-*.f64N/A
lower-fma.f64N/A
Applied rewrites99.6%
Taylor expanded in x around 0
Applied rewrites99.2%
Applied rewrites99.2%
(FPCore (x eps) :precision binary64 (* eps (fma eps (fma eps 0.3333333333333333 x) (/ 1.0 (+ 0.5 (* 0.5 (cos (+ x x))))))))
double code(double x, double eps) {
return eps * fma(eps, fma(eps, 0.3333333333333333, x), (1.0 / (0.5 + (0.5 * cos((x + x))))));
}
function code(x, eps) return Float64(eps * fma(eps, fma(eps, 0.3333333333333333, x), Float64(1.0 / Float64(0.5 + Float64(0.5 * cos(Float64(x + x))))))) end
code[x_, eps_] := N[(eps * N[(eps * N[(eps * 0.3333333333333333 + x), $MachinePrecision] + N[(1.0 / N[(0.5 + N[(0.5 * N[Cos[N[(x + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \mathsf{fma}\left(\varepsilon, \mathsf{fma}\left(\varepsilon, 0.3333333333333333, x\right), \frac{1}{0.5 + 0.5 \cdot \cos \left(x + x\right)}\right)
\end{array}
Initial program 62.1%
lift--.f64N/A
lift-tan.f64N/A
tan-quotN/A
lift-tan.f64N/A
tan-quotN/A
frac-subN/A
lower-/.f64N/A
sin-diffN/A
lower-sin.f64N/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6462.1
Applied rewrites62.1%
Taylor expanded in eps around 0
lower-*.f64N/A
lower-fma.f64N/A
Applied rewrites99.6%
Taylor expanded in x around 0
Applied rewrites99.2%
Applied rewrites99.2%
Final simplification99.2%
(FPCore (x eps)
:precision binary64
(*
eps
(fma
eps
(fma eps 0.3333333333333333 x)
(/
1.0
(fma
(* x x)
(fma (* x x) (fma (* x x) -0.044444444444444446 0.3333333333333333) -1.0)
1.0)))))
double code(double x, double eps) {
return eps * fma(eps, fma(eps, 0.3333333333333333, x), (1.0 / fma((x * x), fma((x * x), fma((x * x), -0.044444444444444446, 0.3333333333333333), -1.0), 1.0)));
}
function code(x, eps) return Float64(eps * fma(eps, fma(eps, 0.3333333333333333, x), Float64(1.0 / fma(Float64(x * x), fma(Float64(x * x), fma(Float64(x * x), -0.044444444444444446, 0.3333333333333333), -1.0), 1.0)))) end
code[x_, eps_] := N[(eps * N[(eps * N[(eps * 0.3333333333333333 + x), $MachinePrecision] + N[(1.0 / N[(N[(x * x), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] * -0.044444444444444446 + 0.3333333333333333), $MachinePrecision] + -1.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \mathsf{fma}\left(\varepsilon, \mathsf{fma}\left(\varepsilon, 0.3333333333333333, x\right), \frac{1}{\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(x \cdot x, -0.044444444444444446, 0.3333333333333333\right), -1\right), 1\right)}\right)
\end{array}
Initial program 62.1%
lift--.f64N/A
lift-tan.f64N/A
tan-quotN/A
lift-tan.f64N/A
tan-quotN/A
frac-subN/A
lower-/.f64N/A
sin-diffN/A
lower-sin.f64N/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6462.1
Applied rewrites62.1%
Taylor expanded in eps around 0
lower-*.f64N/A
lower-fma.f64N/A
Applied rewrites99.6%
Taylor expanded in x around 0
Applied rewrites99.2%
Taylor expanded in x around 0
Applied rewrites98.8%
(FPCore (x eps) :precision binary64 (* eps (fma eps (fma eps 0.3333333333333333 x) (/ 1.0 (fma (* x x) (fma 0.3333333333333333 (* x x) -1.0) 1.0)))))
double code(double x, double eps) {
return eps * fma(eps, fma(eps, 0.3333333333333333, x), (1.0 / fma((x * x), fma(0.3333333333333333, (x * x), -1.0), 1.0)));
}
function code(x, eps) return Float64(eps * fma(eps, fma(eps, 0.3333333333333333, x), Float64(1.0 / fma(Float64(x * x), fma(0.3333333333333333, Float64(x * x), -1.0), 1.0)))) end
code[x_, eps_] := N[(eps * N[(eps * N[(eps * 0.3333333333333333 + x), $MachinePrecision] + N[(1.0 / N[(N[(x * x), $MachinePrecision] * N[(0.3333333333333333 * N[(x * x), $MachinePrecision] + -1.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon \cdot \mathsf{fma}\left(\varepsilon, \mathsf{fma}\left(\varepsilon, 0.3333333333333333, x\right), \frac{1}{\mathsf{fma}\left(x \cdot x, \mathsf{fma}\left(0.3333333333333333, x \cdot x, -1\right), 1\right)}\right)
\end{array}
Initial program 62.1%
lift--.f64N/A
lift-tan.f64N/A
tan-quotN/A
lift-tan.f64N/A
tan-quotN/A
frac-subN/A
lower-/.f64N/A
sin-diffN/A
lower-sin.f64N/A
lower--.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-cos.f64N/A
lower-cos.f6462.1
Applied rewrites62.1%
Taylor expanded in eps around 0
lower-*.f64N/A
lower-fma.f64N/A
Applied rewrites99.6%
Taylor expanded in x around 0
Applied rewrites99.2%
Taylor expanded in x around 0
Applied rewrites98.7%
(FPCore (x eps) :precision binary64 (fma eps (fma eps (fma eps 0.3333333333333333 x) (* x x)) eps))
double code(double x, double eps) {
return fma(eps, fma(eps, fma(eps, 0.3333333333333333, x), (x * x)), eps);
}
function code(x, eps) return fma(eps, fma(eps, fma(eps, 0.3333333333333333, x), Float64(x * x)), eps) end
code[x_, eps_] := N[(eps * N[(eps * N[(eps * 0.3333333333333333 + x), $MachinePrecision] + N[(x * x), $MachinePrecision]), $MachinePrecision] + eps), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon, \mathsf{fma}\left(\varepsilon, \mathsf{fma}\left(\varepsilon, 0.3333333333333333, x\right), x \cdot x\right), \varepsilon\right)
\end{array}
Initial program 62.1%
Taylor expanded in eps around 0
Applied rewrites99.6%
Taylor expanded in x around 0
Applied rewrites98.3%
Taylor expanded in eps around 0
Applied rewrites98.3%
Taylor expanded in x around 0
Applied rewrites98.4%
(FPCore (x eps) :precision binary64 (fma eps (* x (+ x eps)) eps))
double code(double x, double eps) {
return fma(eps, (x * (x + eps)), eps);
}
function code(x, eps) return fma(eps, Float64(x * Float64(x + eps)), eps) end
code[x_, eps_] := N[(eps * N[(x * N[(x + eps), $MachinePrecision]), $MachinePrecision] + eps), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon, x \cdot \left(x + \varepsilon\right), \varepsilon\right)
\end{array}
Initial program 62.1%
Taylor expanded in eps around 0
Applied rewrites99.6%
Taylor expanded in x around 0
Applied rewrites98.3%
Taylor expanded in eps around 0
Applied rewrites98.3%
Final simplification98.3%
(FPCore (x eps) :precision binary64 (fma eps (* x x) eps))
double code(double x, double eps) {
return fma(eps, (x * x), eps);
}
function code(x, eps) return fma(eps, Float64(x * x), eps) end
code[x_, eps_] := N[(eps * N[(x * x), $MachinePrecision] + eps), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\varepsilon, x \cdot x, \varepsilon\right)
\end{array}
Initial program 62.1%
Taylor expanded in eps around 0
Applied rewrites99.6%
Taylor expanded in x around 0
Applied rewrites98.3%
Taylor expanded in eps around 0
Applied rewrites98.2%
(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}
(FPCore (x eps) :precision binary64 (- (/ (+ (tan x) (tan eps)) (- 1.0 (* (tan x) (tan eps)))) (tan x)))
double code(double x, double eps) {
return ((tan(x) + tan(eps)) / (1.0 - (tan(x) * tan(eps)))) - tan(x);
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = ((tan(x) + tan(eps)) / (1.0d0 - (tan(x) * tan(eps)))) - tan(x)
end function
public static double code(double x, double eps) {
return ((Math.tan(x) + Math.tan(eps)) / (1.0 - (Math.tan(x) * Math.tan(eps)))) - Math.tan(x);
}
def code(x, eps): return ((math.tan(x) + math.tan(eps)) / (1.0 - (math.tan(x) * math.tan(eps)))) - math.tan(x)
function code(x, eps) return Float64(Float64(Float64(tan(x) + tan(eps)) / Float64(1.0 - Float64(tan(x) * tan(eps)))) - tan(x)) end
function tmp = code(x, eps) tmp = ((tan(x) + tan(eps)) / (1.0 - (tan(x) * tan(eps)))) - tan(x); end
code[x_, eps_] := N[(N[(N[(N[Tan[x], $MachinePrecision] + N[Tan[eps], $MachinePrecision]), $MachinePrecision] / N[(1.0 - N[(N[Tan[x], $MachinePrecision] * N[Tan[eps], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[Tan[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\tan x + \tan \varepsilon}{1 - \tan x \cdot \tan \varepsilon} - \tan x
\end{array}
(FPCore (x eps) :precision binary64 (+ eps (* (* eps (tan x)) (tan x))))
double code(double x, double eps) {
return eps + ((eps * tan(x)) * tan(x));
}
real(8) function code(x, eps)
real(8), intent (in) :: x
real(8), intent (in) :: eps
code = eps + ((eps * tan(x)) * tan(x))
end function
public static double code(double x, double eps) {
return eps + ((eps * Math.tan(x)) * Math.tan(x));
}
def code(x, eps): return eps + ((eps * math.tan(x)) * math.tan(x))
function code(x, eps) return Float64(eps + Float64(Float64(eps * tan(x)) * tan(x))) end
function tmp = code(x, eps) tmp = eps + ((eps * tan(x)) * tan(x)); end
code[x_, eps_] := N[(eps + N[(N[(eps * N[Tan[x], $MachinePrecision]), $MachinePrecision] * N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\varepsilon + \left(\varepsilon \cdot \tan x\right) \cdot \tan x
\end{array}
herbie shell --seed 2024234
(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)))
:alt
(! :herbie-platform default (/ (sin eps) (* (cos x) (cos (+ x eps)))))
:alt
(! :herbie-platform default (- (/ (+ (tan x) (tan eps)) (- 1 (* (tan x) (tan eps)))) (tan x)))
:alt
(! :herbie-platform default (+ eps (* eps (tan x) (tan x))))
(- (tan (+ x eps)) (tan x)))