| Alternative 1 | |
|---|---|
| Accuracy | 73.5% |
| Cost | 1497 |

(FPCore (x y) :precision binary64 (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))
(FPCore (x y)
:precision binary64
(let* ((t_0 (* y (* y 4.0)))
(t_1 (/ (- (* x x) t_0) (+ (* x x) t_0)))
(t_2 (+ (* (/ (/ x y) (/ y x)) 0.5) -1.0)))
(if (<= (* x x) 0.0)
t_2
(if (<= (* x x) 2e-217)
t_1
(if (<= (* x x) 2e-140)
t_2
(if (<= (* x x) 5e+82) t_1 (+ (* (/ (/ y x) (/ x y)) -8.0) 1.0)))))))double code(double x, double y) {
return ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y));
}
double code(double x, double y) {
double t_0 = y * (y * 4.0);
double t_1 = ((x * x) - t_0) / ((x * x) + t_0);
double t_2 = (((x / y) / (y / x)) * 0.5) + -1.0;
double tmp;
if ((x * x) <= 0.0) {
tmp = t_2;
} else if ((x * x) <= 2e-217) {
tmp = t_1;
} else if ((x * x) <= 2e-140) {
tmp = t_2;
} else if ((x * x) <= 5e+82) {
tmp = t_1;
} else {
tmp = (((y / x) / (x / y)) * -8.0) + 1.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * x) - ((y * 4.0d0) * y)) / ((x * x) + ((y * 4.0d0) * y))
end function
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: t_1
real(8) :: t_2
real(8) :: tmp
t_0 = y * (y * 4.0d0)
t_1 = ((x * x) - t_0) / ((x * x) + t_0)
t_2 = (((x / y) / (y / x)) * 0.5d0) + (-1.0d0)
if ((x * x) <= 0.0d0) then
tmp = t_2
else if ((x * x) <= 2d-217) then
tmp = t_1
else if ((x * x) <= 2d-140) then
tmp = t_2
else if ((x * x) <= 5d+82) then
tmp = t_1
else
tmp = (((y / x) / (x / y)) * (-8.0d0)) + 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
return ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y));
}
public static double code(double x, double y) {
double t_0 = y * (y * 4.0);
double t_1 = ((x * x) - t_0) / ((x * x) + t_0);
double t_2 = (((x / y) / (y / x)) * 0.5) + -1.0;
double tmp;
if ((x * x) <= 0.0) {
tmp = t_2;
} else if ((x * x) <= 2e-217) {
tmp = t_1;
} else if ((x * x) <= 2e-140) {
tmp = t_2;
} else if ((x * x) <= 5e+82) {
tmp = t_1;
} else {
tmp = (((y / x) / (x / y)) * -8.0) + 1.0;
}
return tmp;
}
def code(x, y): return ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y))
def code(x, y): t_0 = y * (y * 4.0) t_1 = ((x * x) - t_0) / ((x * x) + t_0) t_2 = (((x / y) / (y / x)) * 0.5) + -1.0 tmp = 0 if (x * x) <= 0.0: tmp = t_2 elif (x * x) <= 2e-217: tmp = t_1 elif (x * x) <= 2e-140: tmp = t_2 elif (x * x) <= 5e+82: tmp = t_1 else: tmp = (((y / x) / (x / y)) * -8.0) + 1.0 return tmp
function code(x, y) return Float64(Float64(Float64(x * x) - Float64(Float64(y * 4.0) * y)) / Float64(Float64(x * x) + Float64(Float64(y * 4.0) * y))) end
function code(x, y) t_0 = Float64(y * Float64(y * 4.0)) t_1 = Float64(Float64(Float64(x * x) - t_0) / Float64(Float64(x * x) + t_0)) t_2 = Float64(Float64(Float64(Float64(x / y) / Float64(y / x)) * 0.5) + -1.0) tmp = 0.0 if (Float64(x * x) <= 0.0) tmp = t_2; elseif (Float64(x * x) <= 2e-217) tmp = t_1; elseif (Float64(x * x) <= 2e-140) tmp = t_2; elseif (Float64(x * x) <= 5e+82) tmp = t_1; else tmp = Float64(Float64(Float64(Float64(y / x) / Float64(x / y)) * -8.0) + 1.0); end return tmp end
function tmp = code(x, y) tmp = ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y)); end
function tmp_2 = code(x, y) t_0 = y * (y * 4.0); t_1 = ((x * x) - t_0) / ((x * x) + t_0); t_2 = (((x / y) / (y / x)) * 0.5) + -1.0; tmp = 0.0; if ((x * x) <= 0.0) tmp = t_2; elseif ((x * x) <= 2e-217) tmp = t_1; elseif ((x * x) <= 2e-140) tmp = t_2; elseif ((x * x) <= 5e+82) tmp = t_1; else tmp = (((y / x) / (x / y)) * -8.0) + 1.0; end tmp_2 = tmp; end
code[x_, y_] := N[(N[(N[(x * x), $MachinePrecision] - N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := Block[{t$95$0 = N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(x * x), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[(N[(x / y), $MachinePrecision] / N[(y / x), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[N[(x * x), $MachinePrecision], 0.0], t$95$2, If[LessEqual[N[(x * x), $MachinePrecision], 2e-217], t$95$1, If[LessEqual[N[(x * x), $MachinePrecision], 2e-140], t$95$2, If[LessEqual[N[(x * x), $MachinePrecision], 5e+82], t$95$1, N[(N[(N[(N[(y / x), $MachinePrecision] / N[(x / y), $MachinePrecision]), $MachinePrecision] * -8.0), $MachinePrecision] + 1.0), $MachinePrecision]]]]]]]]
\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}
\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\
t_2 := \frac{\frac{x}{y}}{\frac{y}{x}} \cdot 0.5 + -1\\
\mathbf{if}\;x \cdot x \leq 0:\\
\;\;\;\;t_2\\
\mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-217}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;x \cdot x \leq 2 \cdot 10^{-140}:\\
\;\;\;\;t_2\\
\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+82}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{y}{x}}{\frac{x}{y}} \cdot -8 + 1\\
\end{array}
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
| Original | 50.8% |
|---|---|
| Target | 51.3% |
| Herbie | 79.9% |
if (*.f64 x x) < 0.0 or 2.00000000000000016e-217 < (*.f64 x x) < 2e-140Initial program 59.5%
Taylor expanded in x around 0 82.4%
Simplified93.2%
[Start]82.4 | \[ 0.5 \cdot \frac{{x}^{2}}{{y}^{2}} - 1
\] |
|---|---|
fma-neg [=>]82.4 | \[ \color{blue}{\mathsf{fma}\left(0.5, \frac{{x}^{2}}{{y}^{2}}, -1\right)}
\] |
unpow2 [=>]82.4 | \[ \mathsf{fma}\left(0.5, \frac{\color{blue}{x \cdot x}}{{y}^{2}}, -1\right)
\] |
unpow2 [=>]82.4 | \[ \mathsf{fma}\left(0.5, \frac{x \cdot x}{\color{blue}{y \cdot y}}, -1\right)
\] |
times-frac [=>]93.2 | \[ \mathsf{fma}\left(0.5, \color{blue}{\frac{x}{y} \cdot \frac{x}{y}}, -1\right)
\] |
metadata-eval [=>]93.2 | \[ \mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, \color{blue}{-1}\right)
\] |
Applied egg-rr93.2%
[Start]93.2 | \[ \mathsf{fma}\left(0.5, \frac{x}{y} \cdot \frac{x}{y}, -1\right)
\] |
|---|---|
fma-udef [=>]93.2 | \[ \color{blue}{0.5 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right) + -1}
\] |
*-commutative [=>]93.2 | \[ \color{blue}{\left(\frac{x}{y} \cdot \frac{x}{y}\right) \cdot 0.5} + -1
\] |
pow2 [=>]93.2 | \[ \color{blue}{{\left(\frac{x}{y}\right)}^{2}} \cdot 0.5 + -1
\] |
Applied egg-rr93.2%
[Start]93.2 | \[ {\left(\frac{x}{y}\right)}^{2} \cdot 0.5 + -1
\] |
|---|---|
unpow2 [=>]93.2 | \[ \color{blue}{\left(\frac{x}{y} \cdot \frac{x}{y}\right)} \cdot 0.5 + -1
\] |
clear-num [=>]93.2 | \[ \left(\frac{x}{y} \cdot \color{blue}{\frac{1}{\frac{y}{x}}}\right) \cdot 0.5 + -1
\] |
un-div-inv [=>]93.2 | \[ \color{blue}{\frac{\frac{x}{y}}{\frac{y}{x}}} \cdot 0.5 + -1
\] |
if 0.0 < (*.f64 x x) < 2.00000000000000016e-217 or 2e-140 < (*.f64 x x) < 5.00000000000000015e82Initial program 93.7%
if 5.00000000000000015e82 < (*.f64 x x) Initial program 27.5%
Taylor expanded in x around inf 76.4%
Simplified83.4%
[Start]76.4 | \[ \left(1 + -4 \cdot \frac{{y}^{2}}{{x}^{2}}\right) - 4 \cdot \frac{{y}^{2}}{{x}^{2}}
\] |
|---|---|
associate--l+ [=>]76.4 | \[ \color{blue}{1 + \left(-4 \cdot \frac{{y}^{2}}{{x}^{2}} - 4 \cdot \frac{{y}^{2}}{{x}^{2}}\right)}
\] |
distribute-rgt-out-- [=>]76.4 | \[ 1 + \color{blue}{\frac{{y}^{2}}{{x}^{2}} \cdot \left(-4 - 4\right)}
\] |
metadata-eval [=>]76.4 | \[ 1 + \frac{{y}^{2}}{{x}^{2}} \cdot \color{blue}{-8}
\] |
*-commutative [<=]76.4 | \[ 1 + \color{blue}{-8 \cdot \frac{{y}^{2}}{{x}^{2}}}
\] |
+-commutative [<=]76.4 | \[ \color{blue}{-8 \cdot \frac{{y}^{2}}{{x}^{2}} + 1}
\] |
*-commutative [=>]76.4 | \[ \color{blue}{\frac{{y}^{2}}{{x}^{2}} \cdot -8} + 1
\] |
fma-def [=>]76.4 | \[ \color{blue}{\mathsf{fma}\left(\frac{{y}^{2}}{{x}^{2}}, -8, 1\right)}
\] |
unpow2 [=>]76.4 | \[ \mathsf{fma}\left(\frac{\color{blue}{y \cdot y}}{{x}^{2}}, -8, 1\right)
\] |
unpow2 [=>]76.4 | \[ \mathsf{fma}\left(\frac{y \cdot y}{\color{blue}{x \cdot x}}, -8, 1\right)
\] |
times-frac [=>]83.4 | \[ \mathsf{fma}\left(\color{blue}{\frac{y}{x} \cdot \frac{y}{x}}, -8, 1\right)
\] |
Applied egg-rr83.4%
[Start]83.4 | \[ \mathsf{fma}\left(\frac{y}{x} \cdot \frac{y}{x}, -8, 1\right)
\] |
|---|---|
fma-udef [=>]83.4 | \[ \color{blue}{\left(\frac{y}{x} \cdot \frac{y}{x}\right) \cdot -8 + 1}
\] |
pow2 [=>]83.4 | \[ \color{blue}{{\left(\frac{y}{x}\right)}^{2}} \cdot -8 + 1
\] |
Applied egg-rr83.4%
[Start]83.4 | \[ {\left(\frac{y}{x}\right)}^{2} \cdot -8 + 1
\] |
|---|---|
unpow2 [=>]83.4 | \[ \color{blue}{\left(\frac{y}{x} \cdot \frac{y}{x}\right)} \cdot -8 + 1
\] |
clear-num [=>]83.4 | \[ \left(\frac{y}{x} \cdot \color{blue}{\frac{1}{\frac{x}{y}}}\right) \cdot -8 + 1
\] |
un-div-inv [=>]83.4 | \[ \color{blue}{\frac{\frac{y}{x}}{\frac{x}{y}}} \cdot -8 + 1
\] |
Final simplification89.1%
| Alternative 1 | |
|---|---|
| Accuracy | 73.5% |
| Cost | 1497 |
| Alternative 2 | |
|---|---|
| Accuracy | 73.0% |
| Cost | 1232 |
| Alternative 3 | |
|---|---|
| Accuracy | 72.4% |
| Cost | 856 |
| Alternative 4 | |
|---|---|
| Accuracy | 49.4% |
| Cost | 64 |
herbie shell --seed 2023160
(FPCore (x y)
:name "Diagrams.TwoD.Arc:arcBetween from diagrams-lib-1.3.0.3"
:precision binary64
:herbie-target
(if (< (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))) 0.9743233849626781) (- (/ (* x x) (+ (* x x) (* (* y y) 4.0))) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))) (- (pow (/ x (sqrt (+ (* x x) (* (* y y) 4.0)))) 2.0) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))))
(/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))