| Alternative 1 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 576 |
\[x \cdot \left(x \cdot \left(x \cdot 0.022222222222222223\right) + 0.3333333333333333\right)
\]

(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
(FPCore (x) :precision binary64 (/ 1.0 (+ (/ 3.0 x) (* x -0.2))))
double code(double x) {
return (1.0 / x) - (1.0 / tan(x));
}
double code(double x) {
return 1.0 / ((3.0 / x) + (x * -0.2));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 / ((3.0d0 / x) + (x * (-0.2d0)))
end function
public static double code(double x) {
return (1.0 / x) - (1.0 / Math.tan(x));
}
public static double code(double x) {
return 1.0 / ((3.0 / x) + (x * -0.2));
}
def code(x): return (1.0 / x) - (1.0 / math.tan(x))
def code(x): return 1.0 / ((3.0 / x) + (x * -0.2))
function code(x) return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x))) end
function code(x) return Float64(1.0 / Float64(Float64(3.0 / x) + Float64(x * -0.2))) end
function tmp = code(x) tmp = (1.0 / x) - (1.0 / tan(x)); end
function tmp = code(x) tmp = 1.0 / ((3.0 / x) + (x * -0.2)); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := N[(1.0 / N[(N[(3.0 / x), $MachinePrecision] + N[(x * -0.2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{x} - \frac{1}{\tan x}
\frac{1}{\frac{3}{x} + x \cdot -0.2}
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
| Original | 6.5% |
|---|---|
| Target | 99.9% |
| Herbie | 99.4% |
Initial program 6.8%
Taylor expanded in x around 0 99.4%
Applied egg-rr55.4%
[Start]99.4 | \[ 0.3333333333333333 \cdot x + 0.022222222222222223 \cdot {x}^{3}
\] |
|---|---|
flip-+ [=>]55.4 | \[ \color{blue}{\frac{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}}
\] |
clear-num [=>]55.3 | \[ \color{blue}{\frac{1}{\frac{0.3333333333333333 \cdot x - 0.022222222222222223 \cdot {x}^{3}}{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}}}
\] |
cancel-sign-sub-inv [=>]55.3 | \[ \frac{1}{\frac{\color{blue}{0.3333333333333333 \cdot x + \left(-0.022222222222222223\right) \cdot {x}^{3}}}{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}}
\] |
*-commutative [=>]55.3 | \[ \frac{1}{\frac{\color{blue}{x \cdot 0.3333333333333333} + \left(-0.022222222222222223\right) \cdot {x}^{3}}{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}}
\] |
fma-def [=>]55.3 | \[ \frac{1}{\frac{\color{blue}{\mathsf{fma}\left(x, 0.3333333333333333, \left(-0.022222222222222223\right) \cdot {x}^{3}\right)}}{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}}
\] |
metadata-eval [=>]55.3 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, \color{blue}{-0.022222222222222223} \cdot {x}^{3}\right)}{\left(0.3333333333333333 \cdot x\right) \cdot \left(0.3333333333333333 \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}}
\] |
swap-sqr [=>]55.4 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, -0.022222222222222223 \cdot {x}^{3}\right)}{\color{blue}{\left(0.3333333333333333 \cdot 0.3333333333333333\right) \cdot \left(x \cdot x\right)} - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}}
\] |
metadata-eval [=>]55.4 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, -0.022222222222222223 \cdot {x}^{3}\right)}{\color{blue}{0.1111111111111111} \cdot \left(x \cdot x\right) - \left(0.022222222222222223 \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}}
\] |
*-commutative [=>]55.4 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, -0.022222222222222223 \cdot {x}^{3}\right)}{0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)} \cdot \left(0.022222222222222223 \cdot {x}^{3}\right)}}
\] |
*-commutative [=>]55.4 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, -0.022222222222222223 \cdot {x}^{3}\right)}{0.1111111111111111 \cdot \left(x \cdot x\right) - \left({x}^{3} \cdot 0.022222222222222223\right) \cdot \color{blue}{\left({x}^{3} \cdot 0.022222222222222223\right)}}}
\] |
swap-sqr [=>]55.4 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, -0.022222222222222223 \cdot {x}^{3}\right)}{0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{\left({x}^{3} \cdot {x}^{3}\right) \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)}}}
\] |
pow-prod-up [=>]55.4 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, -0.022222222222222223 \cdot {x}^{3}\right)}{0.1111111111111111 \cdot \left(x \cdot x\right) - \color{blue}{{x}^{\left(3 + 3\right)}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)}}
\] |
metadata-eval [=>]55.4 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, -0.022222222222222223 \cdot {x}^{3}\right)}{0.1111111111111111 \cdot \left(x \cdot x\right) - {x}^{\color{blue}{6}} \cdot \left(0.022222222222222223 \cdot 0.022222222222222223\right)}}
\] |
metadata-eval [=>]55.4 | \[ \frac{1}{\frac{\mathsf{fma}\left(x, 0.3333333333333333, -0.022222222222222223 \cdot {x}^{3}\right)}{0.1111111111111111 \cdot \left(x \cdot x\right) - {x}^{6} \cdot \color{blue}{0.0004938271604938272}}}
\] |
Taylor expanded in x around 0 99.4%
Simplified99.6%
[Start]99.4 | \[ \frac{1}{3 \cdot \frac{1}{x} + -0.2 \cdot x}
\] |
|---|---|
associate-*r/ [=>]99.6 | \[ \frac{1}{\color{blue}{\frac{3 \cdot 1}{x}} + -0.2 \cdot x}
\] |
metadata-eval [=>]99.6 | \[ \frac{1}{\frac{\color{blue}{3}}{x} + -0.2 \cdot x}
\] |
*-commutative [=>]99.6 | \[ \frac{1}{\frac{3}{x} + \color{blue}{x \cdot -0.2}}
\] |
Final simplification99.6%
| Alternative 1 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 576 |
| Alternative 2 | |
|---|---|
| Accuracy | 98.9% |
| Cost | 320 |
| Alternative 3 | |
|---|---|
| Accuracy | 98.9% |
| Cost | 192 |
herbie shell --seed 2023161
(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))))