
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
double code(double x) {
return (1.0 / x) - (1.0 / tan(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
public static double code(double x) {
return (1.0 / x) - (1.0 / Math.tan(x));
}
def code(x): return (1.0 / x) - (1.0 / math.tan(x))
function code(x) return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x))) end
function tmp = code(x) tmp = (1.0 / x) - (1.0 / tan(x)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x} - \frac{1}{\tan x}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
double code(double x) {
return (1.0 / x) - (1.0 / tan(x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
public static double code(double x) {
return (1.0 / x) - (1.0 / Math.tan(x));
}
def code(x): return (1.0 / x) - (1.0 / math.tan(x))
function code(x) return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x))) end
function tmp = code(x) tmp = (1.0 / x) - (1.0 / tan(x)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x} - \frac{1}{\tan x}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (fma x 0.3333333333333333 (* (pow x 3.0) -0.022222222222222223))))
(-
(/ x (/ t_0 (* x 0.1111111111111111)))
(/ (pow x 6.0) (/ t_0 0.0004938271604938272)))))
double code(double x) {
double t_0 = fma(x, 0.3333333333333333, (pow(x, 3.0) * -0.022222222222222223));
return (x / (t_0 / (x * 0.1111111111111111))) - (pow(x, 6.0) / (t_0 / 0.0004938271604938272));
}
function code(x) t_0 = fma(x, 0.3333333333333333, Float64((x ^ 3.0) * -0.022222222222222223)) return Float64(Float64(x / Float64(t_0 / Float64(x * 0.1111111111111111))) - Float64((x ^ 6.0) / Float64(t_0 / 0.0004938271604938272))) end
code[x_] := Block[{t$95$0 = N[(x * 0.3333333333333333 + N[(N[Power[x, 3.0], $MachinePrecision] * -0.022222222222222223), $MachinePrecision]), $MachinePrecision]}, N[(N[(x / N[(t$95$0 / N[(x * 0.1111111111111111), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[Power[x, 6.0], $MachinePrecision] / N[(t$95$0 / 0.0004938271604938272), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(x, 0.3333333333333333, {x}^{3} \cdot -0.022222222222222223\right)\\
\frac{x}{\frac{t_0}{x \cdot 0.1111111111111111}} - \frac{{x}^{6}}{\frac{t_0}{0.0004938271604938272}}
\end{array}
\end{array}
Initial program 6.6%
Taylor expanded in x around 0 99.5%
+-commutative99.5%
fma-def99.5%
Simplified99.5%
fma-udef99.5%
flip-+54.8%
swap-sqr54.8%
metadata-eval54.8%
swap-sqr54.8%
metadata-eval54.8%
pow-prod-up54.8%
metadata-eval54.8%
Applied egg-rr54.8%
Taylor expanded in x around 0 54.8%
unpow254.8%
*-commutative54.8%
associate-*r*54.8%
Simplified54.8%
div-sub54.8%
cancel-sign-sub-inv54.8%
metadata-eval54.8%
*-commutative54.8%
fma-udef54.8%
*-commutative54.8%
cancel-sign-sub-inv54.8%
metadata-eval54.8%
*-commutative54.8%
fma-udef54.8%
Applied egg-rr54.8%
associate-/l*99.7%
metadata-eval99.7%
distribute-rgt-neg-in99.7%
fma-neg99.7%
*-commutative99.7%
fma-neg99.7%
distribute-rgt-neg-in99.7%
metadata-eval99.7%
associate-/l*99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (+ (* 0.0021164021164021165 (pow x 5.0)) (+ (* (pow x 3.0) 0.022222222222222223) (* x 0.3333333333333333))))
double code(double x) {
return (0.0021164021164021165 * pow(x, 5.0)) + ((pow(x, 3.0) * 0.022222222222222223) + (x * 0.3333333333333333));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (0.0021164021164021165d0 * (x ** 5.0d0)) + (((x ** 3.0d0) * 0.022222222222222223d0) + (x * 0.3333333333333333d0))
end function
public static double code(double x) {
return (0.0021164021164021165 * Math.pow(x, 5.0)) + ((Math.pow(x, 3.0) * 0.022222222222222223) + (x * 0.3333333333333333));
}
def code(x): return (0.0021164021164021165 * math.pow(x, 5.0)) + ((math.pow(x, 3.0) * 0.022222222222222223) + (x * 0.3333333333333333))
function code(x) return Float64(Float64(0.0021164021164021165 * (x ^ 5.0)) + Float64(Float64((x ^ 3.0) * 0.022222222222222223) + Float64(x * 0.3333333333333333))) end
function tmp = code(x) tmp = (0.0021164021164021165 * (x ^ 5.0)) + (((x ^ 3.0) * 0.022222222222222223) + (x * 0.3333333333333333)); end
code[x_] := N[(N[(0.0021164021164021165 * N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Power[x, 3.0], $MachinePrecision] * 0.022222222222222223), $MachinePrecision] + N[(x * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.0021164021164021165 \cdot {x}^{5} + \left({x}^{3} \cdot 0.022222222222222223 + x \cdot 0.3333333333333333\right)
\end{array}
Initial program 6.6%
Taylor expanded in x around 0 99.5%
Final simplification99.5%
(FPCore (x) :precision binary64 (+ (* (pow x 3.0) 0.022222222222222223) (* x 0.3333333333333333)))
double code(double x) {
return (pow(x, 3.0) * 0.022222222222222223) + (x * 0.3333333333333333);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((x ** 3.0d0) * 0.022222222222222223d0) + (x * 0.3333333333333333d0)
end function
public static double code(double x) {
return (Math.pow(x, 3.0) * 0.022222222222222223) + (x * 0.3333333333333333);
}
def code(x): return (math.pow(x, 3.0) * 0.022222222222222223) + (x * 0.3333333333333333)
function code(x) return Float64(Float64((x ^ 3.0) * 0.022222222222222223) + Float64(x * 0.3333333333333333)) end
function tmp = code(x) tmp = ((x ^ 3.0) * 0.022222222222222223) + (x * 0.3333333333333333); end
code[x_] := N[(N[(N[Power[x, 3.0], $MachinePrecision] * 0.022222222222222223), $MachinePrecision] + N[(x * 0.3333333333333333), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{x}^{3} \cdot 0.022222222222222223 + x \cdot 0.3333333333333333
\end{array}
Initial program 6.6%
Taylor expanded in x around 0 99.5%
Final simplification99.5%
(FPCore (x) :precision binary64 (* x 0.3333333333333333))
double code(double x) {
return x * 0.3333333333333333;
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * 0.3333333333333333d0
end function
public static double code(double x) {
return x * 0.3333333333333333;
}
def code(x): return x * 0.3333333333333333
function code(x) return Float64(x * 0.3333333333333333) end
function tmp = code(x) tmp = x * 0.3333333333333333; end
code[x_] := N[(x * 0.3333333333333333), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.3333333333333333
\end{array}
Initial program 6.6%
Taylor expanded in x around 0 98.9%
Final simplification98.9%
(FPCore (x) :precision binary64 (if (< (fabs x) 0.026) (* (/ x 3.0) (+ 1.0 (/ (* x x) 15.0))) (- (/ 1.0 x) (/ 1.0 (tan x)))))
double code(double x) {
double tmp;
if (fabs(x) < 0.026) {
tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
} else {
tmp = (1.0 / x) - (1.0 / tan(x));
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (abs(x) < 0.026d0) then
tmp = (x / 3.0d0) * (1.0d0 + ((x * x) / 15.0d0))
else
tmp = (1.0d0 / x) - (1.0d0 / tan(x))
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (Math.abs(x) < 0.026) {
tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
} else {
tmp = (1.0 / x) - (1.0 / Math.tan(x));
}
return tmp;
}
def code(x): tmp = 0 if math.fabs(x) < 0.026: tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0)) else: tmp = (1.0 / x) - (1.0 / math.tan(x)) return tmp
function code(x) tmp = 0.0 if (abs(x) < 0.026) tmp = Float64(Float64(x / 3.0) * Float64(1.0 + Float64(Float64(x * x) / 15.0))); else tmp = Float64(Float64(1.0 / x) - Float64(1.0 / tan(x))); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (abs(x) < 0.026) tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0)); else tmp = (1.0 / x) - (1.0 / tan(x)); end tmp_2 = tmp; end
code[x_] := If[Less[N[Abs[x], $MachinePrecision], 0.026], N[(N[(x / 3.0), $MachinePrecision] * N[(1.0 + N[(N[(x * x), $MachinePrecision] / 15.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| < 0.026:\\
\;\;\;\;\frac{x}{3} \cdot \left(1 + \frac{x \cdot x}{15}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x} - \frac{1}{\tan x}\\
\end{array}
\end{array}
herbie shell --seed 2023280
(FPCore (x)
:name "invcot (example 3.9)"
:precision binary64
:pre (and (< -0.026 x) (< x 0.026))
:herbie-target
(if (< (fabs x) 0.026) (* (/ x 3.0) (+ 1.0 (/ (* x x) 15.0))) (- (/ 1.0 x) (/ 1.0 (tan x))))
(- (/ 1.0 x) (/ 1.0 (tan x))))