| Alternative 1 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 6912 |
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
(FPCore (x) :precision binary64 (* (fma 0.3333333333333333 x 2.0) (/ x (* (fma 0.3333333333333333 x 2.0) 3.0))))
double code(double x) {
return (1.0 / x) - (1.0 / tan(x));
}
double code(double x) {
return fma(0.3333333333333333, x, 2.0) * (x / (fma(0.3333333333333333, x, 2.0) * 3.0));
}
function code(x) return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x))) end
function code(x) return Float64(fma(0.3333333333333333, x, 2.0) * Float64(x / Float64(fma(0.3333333333333333, x, 2.0) * 3.0))) end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := N[(N[(0.3333333333333333 * x + 2.0), $MachinePrecision] * N[(x / N[(N[(0.3333333333333333 * x + 2.0), $MachinePrecision] * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{x} - \frac{1}{\tan x}
\mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot 3}
| Original | 6.3% |
|---|---|
| Target | 99.9% |
| Herbie | 99.5% |
Initial program 6.3%
Taylor expanded in x around 0 99.0%
Applied egg-rr7.6%
[Start]99.0 | \[ 0.3333333333333333 \cdot x
\] |
|---|---|
expm1-log1p-u [=>]99.0 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(0.3333333333333333 \cdot x\right)\right)}
\] |
expm1-udef [=>]7.6 | \[ \color{blue}{e^{\mathsf{log1p}\left(0.3333333333333333 \cdot x\right)} - 1}
\] |
log1p-udef [=>]7.6 | \[ e^{\color{blue}{\log \left(1 + 0.3333333333333333 \cdot x\right)}} - 1
\] |
add-exp-log [<=]7.6 | \[ \color{blue}{\left(1 + 0.3333333333333333 \cdot x\right)} - 1
\] |
Applied egg-rr7.5%
[Start]7.6 | \[ \left(1 + 0.3333333333333333 \cdot x\right) - 1
\] |
|---|---|
flip-- [=>]7.5 | \[ \color{blue}{\frac{\left(1 + 0.3333333333333333 \cdot x\right) \cdot \left(1 + 0.3333333333333333 \cdot x\right) - 1 \cdot 1}{\left(1 + 0.3333333333333333 \cdot x\right) + 1}}
\] |
div-inv [=>]7.5 | \[ \color{blue}{\left(\left(1 + 0.3333333333333333 \cdot x\right) \cdot \left(1 + 0.3333333333333333 \cdot x\right) - 1 \cdot 1\right) \cdot \frac{1}{\left(1 + 0.3333333333333333 \cdot x\right) + 1}}
\] |
metadata-eval [=>]7.5 | \[ \left(\left(1 + 0.3333333333333333 \cdot x\right) \cdot \left(1 + 0.3333333333333333 \cdot x\right) - \color{blue}{1}\right) \cdot \frac{1}{\left(1 + 0.3333333333333333 \cdot x\right) + 1}
\] |
sub-neg [=>]7.5 | \[ \color{blue}{\left(\left(1 + 0.3333333333333333 \cdot x\right) \cdot \left(1 + 0.3333333333333333 \cdot x\right) + \left(-1\right)\right)} \cdot \frac{1}{\left(1 + 0.3333333333333333 \cdot x\right) + 1}
\] |
pow2 [=>]7.5 | \[ \left(\color{blue}{{\left(1 + 0.3333333333333333 \cdot x\right)}^{2}} + \left(-1\right)\right) \cdot \frac{1}{\left(1 + 0.3333333333333333 \cdot x\right) + 1}
\] |
+-commutative [=>]7.5 | \[ \left({\color{blue}{\left(0.3333333333333333 \cdot x + 1\right)}}^{2} + \left(-1\right)\right) \cdot \frac{1}{\left(1 + 0.3333333333333333 \cdot x\right) + 1}
\] |
fma-def [=>]7.5 | \[ \left({\color{blue}{\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}}^{2} + \left(-1\right)\right) \cdot \frac{1}{\left(1 + 0.3333333333333333 \cdot x\right) + 1}
\] |
metadata-eval [=>]7.5 | \[ \left({\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + \color{blue}{-1}\right) \cdot \frac{1}{\left(1 + 0.3333333333333333 \cdot x\right) + 1}
\] |
+-commutative [=>]7.5 | \[ \left({\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + -1\right) \cdot \frac{1}{\color{blue}{\left(0.3333333333333333 \cdot x + 1\right)} + 1}
\] |
associate-+l+ [=>]7.5 | \[ \left({\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + -1\right) \cdot \frac{1}{\color{blue}{0.3333333333333333 \cdot x + \left(1 + 1\right)}}
\] |
metadata-eval [=>]7.5 | \[ \left({\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + -1\right) \cdot \frac{1}{0.3333333333333333 \cdot x + \color{blue}{2}}
\] |
Simplified98.8%
[Start]7.5 | \[ \left({\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + -1\right) \cdot \frac{1}{0.3333333333333333 \cdot x + 2}
\] |
|---|---|
associate-*r/ [=>]7.5 | \[ \color{blue}{\frac{\left({\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + -1\right) \cdot 1}{0.3333333333333333 \cdot x + 2}}
\] |
*-rgt-identity [=>]7.5 | \[ \frac{\color{blue}{{\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right)\right)}^{2} + -1}}{0.3333333333333333 \cdot x + 2}
\] |
unpow2 [=>]7.5 | \[ \frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 1\right) \cdot \mathsf{fma}\left(0.3333333333333333, x, 1\right)} + -1}{0.3333333333333333 \cdot x + 2}
\] |
difference-of-sqr--1 [=>]7.6 | \[ \frac{\color{blue}{\left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) + 1\right) \cdot \left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) - 1\right)}}{0.3333333333333333 \cdot x + 2}
\] |
fma-udef [=>]7.6 | \[ \frac{\left(\color{blue}{\left(0.3333333333333333 \cdot x + 1\right)} + 1\right) \cdot \left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) - 1\right)}{0.3333333333333333 \cdot x + 2}
\] |
associate-+l+ [=>]7.6 | \[ \frac{\color{blue}{\left(0.3333333333333333 \cdot x + \left(1 + 1\right)\right)} \cdot \left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) - 1\right)}{0.3333333333333333 \cdot x + 2}
\] |
metadata-eval [=>]7.6 | \[ \frac{\left(0.3333333333333333 \cdot x + \color{blue}{2}\right) \cdot \left(\mathsf{fma}\left(0.3333333333333333, x, 1\right) - 1\right)}{0.3333333333333333 \cdot x + 2}
\] |
fma-udef [=>]7.6 | \[ \frac{\left(0.3333333333333333 \cdot x + 2\right) \cdot \left(\color{blue}{\left(0.3333333333333333 \cdot x + 1\right)} - 1\right)}{0.3333333333333333 \cdot x + 2}
\] |
associate--l+ [=>]99.0 | \[ \frac{\left(0.3333333333333333 \cdot x + 2\right) \cdot \color{blue}{\left(0.3333333333333333 \cdot x + \left(1 - 1\right)\right)}}{0.3333333333333333 \cdot x + 2}
\] |
metadata-eval [=>]99.0 | \[ \frac{\left(0.3333333333333333 \cdot x + 2\right) \cdot \left(0.3333333333333333 \cdot x + \color{blue}{0}\right)}{0.3333333333333333 \cdot x + 2}
\] |
+-rgt-identity [=>]99.0 | \[ \frac{\left(0.3333333333333333 \cdot x + 2\right) \cdot \color{blue}{\left(0.3333333333333333 \cdot x\right)}}{0.3333333333333333 \cdot x + 2}
\] |
associate-/l* [=>]98.8 | \[ \color{blue}{\frac{0.3333333333333333 \cdot x + 2}{\frac{0.3333333333333333 \cdot x + 2}{0.3333333333333333 \cdot x}}}
\] |
fma-def [=>]98.8 | \[ \frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}}{\frac{0.3333333333333333 \cdot x + 2}{0.3333333333333333 \cdot x}}
\] |
fma-def [=>]98.8 | \[ \frac{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}{\frac{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}}{0.3333333333333333 \cdot x}}
\] |
Applied egg-rr99.5%
[Start]98.8 | \[ \frac{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}{\frac{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}{0.3333333333333333 \cdot x}}
\] |
|---|---|
div-inv [=>]98.8 | \[ \color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \frac{1}{\frac{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}{0.3333333333333333 \cdot x}}}
\] |
clear-num [<=]99.0 | \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \color{blue}{\frac{0.3333333333333333 \cdot x}{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}}
\] |
*-commutative [=>]99.0 | \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \frac{\color{blue}{x \cdot 0.3333333333333333}}{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}
\] |
associate-/l* [=>]99.5 | \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \color{blue}{\frac{x}{\frac{\mathsf{fma}\left(0.3333333333333333, x, 2\right)}{0.3333333333333333}}}
\] |
div-inv [=>]99.5 | \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \frac{x}{\color{blue}{\mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \frac{1}{0.3333333333333333}}}
\] |
metadata-eval [=>]99.5 | \[ \mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \frac{x}{\mathsf{fma}\left(0.3333333333333333, x, 2\right) \cdot \color{blue}{3}}
\] |
Final simplification99.5%
| Alternative 1 | |
|---|---|
| Accuracy | 99.4% |
| Cost | 6912 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.1% |
| Cost | 6656 |
| Alternative 3 | |
|---|---|
| Accuracy | 99.0% |
| Cost | 192 |
herbie shell --seed 2023147
(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))))