
(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 (/ (* x (fma (pow x 6.0) 1.0973936899862826e-5 0.037037037037037035)) (+ 0.1111111111111111 (* (* x x) -0.007407407407407408))))
double code(double x) {
return (x * fma(pow(x, 6.0), 1.0973936899862826e-5, 0.037037037037037035)) / (0.1111111111111111 + ((x * x) * -0.007407407407407408));
}
function code(x) return Float64(Float64(x * fma((x ^ 6.0), 1.0973936899862826e-5, 0.037037037037037035)) / Float64(0.1111111111111111 + Float64(Float64(x * x) * -0.007407407407407408))) end
code[x_] := N[(N[(x * N[(N[Power[x, 6.0], $MachinePrecision] * 1.0973936899862826e-5 + 0.037037037037037035), $MachinePrecision]), $MachinePrecision] / N[(0.1111111111111111 + N[(N[(x * x), $MachinePrecision] * -0.007407407407407408), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \mathsf{fma}\left({x}^{6}, 1.0973936899862826 \cdot 10^{-5}, 0.037037037037037035\right)}{0.1111111111111111 + \left(x \cdot x\right) \cdot -0.007407407407407408}
\end{array}
Initial program 6.1%
Taylor expanded in x around 0 99.5%
*-commutative99.5%
flip3-+98.1%
associate-*l/98.3%
Applied egg-rr99.6%
Taylor expanded in x around 0 99.6%
*-commutative99.6%
Simplified99.6%
unpow299.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (/ (* x (- 0.1111111111111111 (* (pow x 4.0) 0.0004938271604938272))) (+ 0.3333333333333333 (* (* x x) -0.022222222222222223))))
double code(double x) {
return (x * (0.1111111111111111 - (pow(x, 4.0) * 0.0004938271604938272))) / (0.3333333333333333 + ((x * x) * -0.022222222222222223));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x * (0.1111111111111111d0 - ((x ** 4.0d0) * 0.0004938271604938272d0))) / (0.3333333333333333d0 + ((x * x) * (-0.022222222222222223d0)))
end function
public static double code(double x) {
return (x * (0.1111111111111111 - (Math.pow(x, 4.0) * 0.0004938271604938272))) / (0.3333333333333333 + ((x * x) * -0.022222222222222223));
}
def code(x): return (x * (0.1111111111111111 - (math.pow(x, 4.0) * 0.0004938271604938272))) / (0.3333333333333333 + ((x * x) * -0.022222222222222223))
function code(x) return Float64(Float64(x * Float64(0.1111111111111111 - Float64((x ^ 4.0) * 0.0004938271604938272))) / Float64(0.3333333333333333 + Float64(Float64(x * x) * -0.022222222222222223))) end
function tmp = code(x) tmp = (x * (0.1111111111111111 - ((x ^ 4.0) * 0.0004938271604938272))) / (0.3333333333333333 + ((x * x) * -0.022222222222222223)); end
code[x_] := N[(N[(x * N[(0.1111111111111111 - N[(N[Power[x, 4.0], $MachinePrecision] * 0.0004938271604938272), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(0.3333333333333333 + N[(N[(x * x), $MachinePrecision] * -0.022222222222222223), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(0.1111111111111111 - {x}^{4} \cdot 0.0004938271604938272\right)}{0.3333333333333333 + \left(x \cdot x\right) \cdot -0.022222222222222223}
\end{array}
Initial program 6.1%
Taylor expanded in x around 0 99.5%
*-commutative99.5%
flip-+99.5%
associate-*l/99.6%
metadata-eval99.6%
*-commutative99.6%
*-commutative99.6%
swap-sqr99.6%
pow-prod-up99.6%
metadata-eval99.6%
metadata-eval99.6%
cancel-sign-sub-inv99.6%
metadata-eval99.6%
Applied egg-rr99.6%
unpow299.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (x) :precision binary64 (/ 1.0 (/ (+ 3.0 (* (pow x 2.0) -0.2)) x)))
double code(double x) {
return 1.0 / ((3.0 + (pow(x, 2.0) * -0.2)) / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / ((3.0d0 + ((x ** 2.0d0) * (-0.2d0))) / x)
end function
public static double code(double x) {
return 1.0 / ((3.0 + (Math.pow(x, 2.0) * -0.2)) / x);
}
def code(x): return 1.0 / ((3.0 + (math.pow(x, 2.0) * -0.2)) / x)
function code(x) return Float64(1.0 / Float64(Float64(3.0 + Float64((x ^ 2.0) * -0.2)) / x)) end
function tmp = code(x) tmp = 1.0 / ((3.0 + ((x ^ 2.0) * -0.2)) / x); end
code[x_] := N[(1.0 / N[(N[(3.0 + N[(N[Power[x, 2.0], $MachinePrecision] * -0.2), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{3 + {x}^{2} \cdot -0.2}{x}}
\end{array}
Initial program 6.1%
Taylor expanded in x around 0 99.5%
unpow299.6%
Applied egg-rr99.5%
Applied egg-rr99.3%
Taylor expanded in x around 0 99.6%
*-commutative99.6%
Simplified99.6%
(FPCore (x) :precision binary64 (* x (+ 0.3333333333333333 (* (* x x) 0.022222222222222223))))
double code(double x) {
return x * (0.3333333333333333 + ((x * x) * 0.022222222222222223));
}
real(8) function code(x)
real(8), intent (in) :: x
code = x * (0.3333333333333333d0 + ((x * x) * 0.022222222222222223d0))
end function
public static double code(double x) {
return x * (0.3333333333333333 + ((x * x) * 0.022222222222222223));
}
def code(x): return x * (0.3333333333333333 + ((x * x) * 0.022222222222222223))
function code(x) return Float64(x * Float64(0.3333333333333333 + Float64(Float64(x * x) * 0.022222222222222223))) end
function tmp = code(x) tmp = x * (0.3333333333333333 + ((x * x) * 0.022222222222222223)); end
code[x_] := N[(x * N[(0.3333333333333333 + N[(N[(x * x), $MachinePrecision] * 0.022222222222222223), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(0.3333333333333333 + \left(x \cdot x\right) \cdot 0.022222222222222223\right)
\end{array}
Initial program 6.1%
Taylor expanded in x around 0 99.5%
unpow299.6%
Applied egg-rr99.5%
Final simplification99.5%
(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 2024179
(FPCore (x)
:name "invcot (example 3.9)"
:precision binary64
:pre (and (< -0.026 x) (< x 0.026))
:alt
(! :herbie-platform default (if (< (fabs x) 13/500) (* (/ x 3) (+ 1 (/ (* x x) 15))) (- (/ 1 x) (/ 1 (tan x)))))
(- (/ 1.0 x) (/ 1.0 (tan x))))