
(FPCore (x y) :precision binary64 (let* ((t_0 (* (* y 4.0) y))) (/ (- (* x x) t_0) (+ (* x x) t_0))))
double code(double x, double y) {
double t_0 = (y * 4.0) * y;
return ((x * x) - t_0) / ((x * x) + t_0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
t_0 = (y * 4.0d0) * y
code = ((x * x) - t_0) / ((x * x) + t_0)
end function
public static double code(double x, double y) {
double t_0 = (y * 4.0) * y;
return ((x * x) - t_0) / ((x * x) + t_0);
}
def code(x, y): t_0 = (y * 4.0) * y return ((x * x) - t_0) / ((x * x) + t_0)
function code(x, y) t_0 = Float64(Float64(y * 4.0) * y) return Float64(Float64(Float64(x * x) - t_0) / Float64(Float64(x * x) + t_0)) end
function tmp = code(x, y) t_0 = (y * 4.0) * y; tmp = ((x * x) - t_0) / ((x * x) + t_0); end
code[x_, y_] := Block[{t$95$0 = N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]}, N[(N[(N[(x * x), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(y \cdot 4\right) \cdot y\\
\frac{x \cdot x - t_0}{x \cdot x + t_0}
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (let* ((t_0 (* (* y 4.0) y))) (/ (- (* x x) t_0) (+ (* x x) t_0))))
double code(double x, double y) {
double t_0 = (y * 4.0) * y;
return ((x * x) - t_0) / ((x * x) + t_0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
t_0 = (y * 4.0d0) * y
code = ((x * x) - t_0) / ((x * x) + t_0)
end function
public static double code(double x, double y) {
double t_0 = (y * 4.0) * y;
return ((x * x) - t_0) / ((x * x) + t_0);
}
def code(x, y): t_0 = (y * 4.0) * y return ((x * x) - t_0) / ((x * x) + t_0)
function code(x, y) t_0 = Float64(Float64(y * 4.0) * y) return Float64(Float64(Float64(x * x) - t_0) / Float64(Float64(x * x) + t_0)) end
function tmp = code(x, y) t_0 = (y * 4.0) * y; tmp = ((x * x) - t_0) / ((x * x) + t_0); end
code[x_, y_] := Block[{t$95$0 = N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]}, N[(N[(N[(x * x), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(y \cdot 4\right) \cdot y\\
\frac{x \cdot x - t_0}{x \cdot x + t_0}
\end{array}
\end{array}
(FPCore (x y)
:precision binary64
(let* ((t_0 (* y (* y 4.0))))
(if (<= (* x x) 1e-110)
(+ -1.0 (/ 0.25 (/ (/ y x) (/ x y))))
(if (<= (* x x) 5e+247)
(/ (- (* x x) t_0) (+ (* x x) t_0))
(+ 1.0 (* -8.0 (* (/ y x) (/ y x))))))))
double code(double x, double y) {
double t_0 = y * (y * 4.0);
double tmp;
if ((x * x) <= 1e-110) {
tmp = -1.0 + (0.25 / ((y / x) / (x / y)));
} else if ((x * x) <= 5e+247) {
tmp = ((x * x) - t_0) / ((x * x) + t_0);
} else {
tmp = 1.0 + (-8.0 * ((y / x) * (y / x)));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = y * (y * 4.0d0)
if ((x * x) <= 1d-110) then
tmp = (-1.0d0) + (0.25d0 / ((y / x) / (x / y)))
else if ((x * x) <= 5d+247) then
tmp = ((x * x) - t_0) / ((x * x) + t_0)
else
tmp = 1.0d0 + ((-8.0d0) * ((y / x) * (y / x)))
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = y * (y * 4.0);
double tmp;
if ((x * x) <= 1e-110) {
tmp = -1.0 + (0.25 / ((y / x) / (x / y)));
} else if ((x * x) <= 5e+247) {
tmp = ((x * x) - t_0) / ((x * x) + t_0);
} else {
tmp = 1.0 + (-8.0 * ((y / x) * (y / x)));
}
return tmp;
}
def code(x, y): t_0 = y * (y * 4.0) tmp = 0 if (x * x) <= 1e-110: tmp = -1.0 + (0.25 / ((y / x) / (x / y))) elif (x * x) <= 5e+247: tmp = ((x * x) - t_0) / ((x * x) + t_0) else: tmp = 1.0 + (-8.0 * ((y / x) * (y / x))) return tmp
function code(x, y) t_0 = Float64(y * Float64(y * 4.0)) tmp = 0.0 if (Float64(x * x) <= 1e-110) tmp = Float64(-1.0 + Float64(0.25 / Float64(Float64(y / x) / Float64(x / y)))); elseif (Float64(x * x) <= 5e+247) tmp = Float64(Float64(Float64(x * x) - t_0) / Float64(Float64(x * x) + t_0)); else tmp = Float64(1.0 + Float64(-8.0 * Float64(Float64(y / x) * Float64(y / x)))); end return tmp end
function tmp_2 = code(x, y) t_0 = y * (y * 4.0); tmp = 0.0; if ((x * x) <= 1e-110) tmp = -1.0 + (0.25 / ((y / x) / (x / y))); elseif ((x * x) <= 5e+247) tmp = ((x * x) - t_0) / ((x * x) + t_0); else tmp = 1.0 + (-8.0 * ((y / x) * (y / x))); end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(x * x), $MachinePrecision], 1e-110], N[(-1.0 + N[(0.25 / N[(N[(y / x), $MachinePrecision] / N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(x * x), $MachinePrecision], 5e+247], N[(N[(N[(x * x), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(-8.0 * N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
\mathbf{if}\;x \cdot x \leq 10^{-110}:\\
\;\;\;\;-1 + \frac{0.25}{\frac{\frac{y}{x}}{\frac{x}{y}}}\\
\mathbf{elif}\;x \cdot x \leq 5 \cdot 10^{+247}:\\
\;\;\;\;\frac{x \cdot x - t_0}{x \cdot x + t_0}\\
\mathbf{else}:\\
\;\;\;\;1 + -8 \cdot \left(\frac{y}{x} \cdot \frac{y}{x}\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 1.0000000000000001e-110Initial program 55.6%
Taylor expanded in x around 0 47.7%
*-commutative47.7%
Simplified47.7%
Taylor expanded in x around 0 71.4%
sub-neg71.4%
*-commutative71.4%
associate-*l/71.4%
associate-*r/71.4%
metadata-eval71.4%
+-commutative71.4%
associate-*r/71.4%
*-commutative71.4%
associate-/l*71.4%
unpow271.4%
unpow271.4%
times-frac79.3%
unpow279.3%
Simplified79.3%
pow279.3%
clear-num79.3%
un-div-inv79.3%
Applied egg-rr79.3%
if 1.0000000000000001e-110 < (*.f64 x x) < 5.00000000000000023e247Initial program 83.3%
if 5.00000000000000023e247 < (*.f64 x x) Initial program 8.0%
Taylor expanded in y around 0 74.9%
unpow274.9%
pow274.9%
times-frac83.5%
Applied egg-rr83.5%
Final simplification81.8%
(FPCore (x y) :precision binary64 (if (<= x 3.7e+15) (+ -1.0 (/ 0.25 (/ (/ y x) (/ x y)))) 1.0))
double code(double x, double y) {
double tmp;
if (x <= 3.7e+15) {
tmp = -1.0 + (0.25 / ((y / x) / (x / y)));
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 3.7d+15) then
tmp = (-1.0d0) + (0.25d0 / ((y / x) / (x / y)))
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 3.7e+15) {
tmp = -1.0 + (0.25 / ((y / x) / (x / y)));
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 3.7e+15: tmp = -1.0 + (0.25 / ((y / x) / (x / y))) else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= 3.7e+15) tmp = Float64(-1.0 + Float64(0.25 / Float64(Float64(y / x) / Float64(x / y)))); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 3.7e+15) tmp = -1.0 + (0.25 / ((y / x) / (x / y))); else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 3.7e+15], N[(-1.0 + N[(0.25 / N[(N[(y / x), $MachinePrecision] / N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 3.7 \cdot 10^{+15}:\\
\;\;\;\;-1 + \frac{0.25}{\frac{\frac{y}{x}}{\frac{x}{y}}}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 3.7e15Initial program 53.5%
Taylor expanded in x around 0 32.4%
*-commutative32.4%
Simplified32.4%
Taylor expanded in x around 0 50.1%
sub-neg50.1%
*-commutative50.1%
associate-*l/50.1%
associate-*r/50.1%
metadata-eval50.1%
+-commutative50.1%
associate-*r/50.1%
*-commutative50.1%
associate-/l*50.1%
unpow250.1%
unpow250.1%
times-frac56.5%
unpow256.5%
Simplified56.5%
pow256.5%
clear-num56.5%
un-div-inv56.5%
Applied egg-rr56.5%
if 3.7e15 < x Initial program 30.4%
Taylor expanded in x around inf 71.5%
Final simplification60.5%
(FPCore (x y) :precision binary64 (if (<= x 8.4e+15) (+ -1.0 (/ 0.25 (/ (/ y x) (/ x y)))) (+ 1.0 (* -8.0 (* (/ y x) (/ y x))))))
double code(double x, double y) {
double tmp;
if (x <= 8.4e+15) {
tmp = -1.0 + (0.25 / ((y / x) / (x / y)));
} else {
tmp = 1.0 + (-8.0 * ((y / x) * (y / x)));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 8.4d+15) then
tmp = (-1.0d0) + (0.25d0 / ((y / x) / (x / y)))
else
tmp = 1.0d0 + ((-8.0d0) * ((y / x) * (y / x)))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 8.4e+15) {
tmp = -1.0 + (0.25 / ((y / x) / (x / y)));
} else {
tmp = 1.0 + (-8.0 * ((y / x) * (y / x)));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 8.4e+15: tmp = -1.0 + (0.25 / ((y / x) / (x / y))) else: tmp = 1.0 + (-8.0 * ((y / x) * (y / x))) return tmp
function code(x, y) tmp = 0.0 if (x <= 8.4e+15) tmp = Float64(-1.0 + Float64(0.25 / Float64(Float64(y / x) / Float64(x / y)))); else tmp = Float64(1.0 + Float64(-8.0 * Float64(Float64(y / x) * Float64(y / x)))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 8.4e+15) tmp = -1.0 + (0.25 / ((y / x) / (x / y))); else tmp = 1.0 + (-8.0 * ((y / x) * (y / x))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 8.4e+15], N[(-1.0 + N[(0.25 / N[(N[(y / x), $MachinePrecision] / N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(-8.0 * N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 8.4 \cdot 10^{+15}:\\
\;\;\;\;-1 + \frac{0.25}{\frac{\frac{y}{x}}{\frac{x}{y}}}\\
\mathbf{else}:\\
\;\;\;\;1 + -8 \cdot \left(\frac{y}{x} \cdot \frac{y}{x}\right)\\
\end{array}
\end{array}
if x < 8.4e15Initial program 53.5%
Taylor expanded in x around 0 32.4%
*-commutative32.4%
Simplified32.4%
Taylor expanded in x around 0 50.1%
sub-neg50.1%
*-commutative50.1%
associate-*l/50.1%
associate-*r/50.1%
metadata-eval50.1%
+-commutative50.1%
associate-*r/50.1%
*-commutative50.1%
associate-/l*50.1%
unpow250.1%
unpow250.1%
times-frac56.5%
unpow256.5%
Simplified56.5%
pow256.5%
clear-num56.5%
un-div-inv56.5%
Applied egg-rr56.5%
if 8.4e15 < x Initial program 30.4%
Taylor expanded in y around 0 67.6%
unpow267.6%
pow267.6%
times-frac72.4%
Applied egg-rr72.4%
Final simplification60.8%
(FPCore (x y) :precision binary64 (if (<= x 1.35e+17) -1.0 1.0))
double code(double x, double y) {
double tmp;
if (x <= 1.35e+17) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 1.35d+17) then
tmp = -1.0d0
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 1.35e+17) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 1.35e+17: tmp = -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= 1.35e+17) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 1.35e+17) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 1.35e+17], -1.0, 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.35 \cdot 10^{+17}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 1.35e17Initial program 53.5%
Taylor expanded in x around 0 55.1%
if 1.35e17 < x Initial program 30.4%
Taylor expanded in x around inf 71.5%
Final simplification59.5%
(FPCore (x y) :precision binary64 -1.0)
double code(double x, double y) {
return -1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -1.0d0
end function
public static double code(double x, double y) {
return -1.0;
}
def code(x, y): return -1.0
function code(x, y) return -1.0 end
function tmp = code(x, y) tmp = -1.0; end
code[x_, y_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 47.2%
Taylor expanded in x around 0 48.4%
Final simplification48.4%
(FPCore (x y)
:precision binary64
(let* ((t_0 (* (* y y) 4.0))
(t_1 (+ (* x x) t_0))
(t_2 (/ t_0 t_1))
(t_3 (* (* y 4.0) y)))
(if (< (/ (- (* x x) t_3) (+ (* x x) t_3)) 0.9743233849626781)
(- (/ (* x x) t_1) t_2)
(- (pow (/ x (sqrt t_1)) 2.0) t_2))))
double code(double x, double y) {
double t_0 = (y * y) * 4.0;
double t_1 = (x * x) + t_0;
double t_2 = t_0 / t_1;
double t_3 = (y * 4.0) * y;
double tmp;
if ((((x * x) - t_3) / ((x * x) + t_3)) < 0.9743233849626781) {
tmp = ((x * x) / t_1) - t_2;
} else {
tmp = pow((x / sqrt(t_1)), 2.0) - t_2;
}
return tmp;
}
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) :: t_3
real(8) :: tmp
t_0 = (y * y) * 4.0d0
t_1 = (x * x) + t_0
t_2 = t_0 / t_1
t_3 = (y * 4.0d0) * y
if ((((x * x) - t_3) / ((x * x) + t_3)) < 0.9743233849626781d0) then
tmp = ((x * x) / t_1) - t_2
else
tmp = ((x / sqrt(t_1)) ** 2.0d0) - t_2
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (y * y) * 4.0;
double t_1 = (x * x) + t_0;
double t_2 = t_0 / t_1;
double t_3 = (y * 4.0) * y;
double tmp;
if ((((x * x) - t_3) / ((x * x) + t_3)) < 0.9743233849626781) {
tmp = ((x * x) / t_1) - t_2;
} else {
tmp = Math.pow((x / Math.sqrt(t_1)), 2.0) - t_2;
}
return tmp;
}
def code(x, y): t_0 = (y * y) * 4.0 t_1 = (x * x) + t_0 t_2 = t_0 / t_1 t_3 = (y * 4.0) * y tmp = 0 if (((x * x) - t_3) / ((x * x) + t_3)) < 0.9743233849626781: tmp = ((x * x) / t_1) - t_2 else: tmp = math.pow((x / math.sqrt(t_1)), 2.0) - t_2 return tmp
function code(x, y) t_0 = Float64(Float64(y * y) * 4.0) t_1 = Float64(Float64(x * x) + t_0) t_2 = Float64(t_0 / t_1) t_3 = Float64(Float64(y * 4.0) * y) tmp = 0.0 if (Float64(Float64(Float64(x * x) - t_3) / Float64(Float64(x * x) + t_3)) < 0.9743233849626781) tmp = Float64(Float64(Float64(x * x) / t_1) - t_2); else tmp = Float64((Float64(x / sqrt(t_1)) ^ 2.0) - t_2); end return tmp end
function tmp_2 = code(x, y) t_0 = (y * y) * 4.0; t_1 = (x * x) + t_0; t_2 = t_0 / t_1; t_3 = (y * 4.0) * y; tmp = 0.0; if ((((x * x) - t_3) / ((x * x) + t_3)) < 0.9743233849626781) tmp = ((x * x) / t_1) - t_2; else tmp = ((x / sqrt(t_1)) ^ 2.0) - t_2; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(y * y), $MachinePrecision] * 4.0), $MachinePrecision]}, Block[{t$95$1 = N[(N[(x * x), $MachinePrecision] + t$95$0), $MachinePrecision]}, Block[{t$95$2 = N[(t$95$0 / t$95$1), $MachinePrecision]}, Block[{t$95$3 = N[(N[(y * 4.0), $MachinePrecision] * y), $MachinePrecision]}, If[Less[N[(N[(N[(x * x), $MachinePrecision] - t$95$3), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + t$95$3), $MachinePrecision]), $MachinePrecision], 0.9743233849626781], N[(N[(N[(x * x), $MachinePrecision] / t$95$1), $MachinePrecision] - t$95$2), $MachinePrecision], N[(N[Power[N[(x / N[Sqrt[t$95$1], $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] - t$95$2), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(y \cdot y\right) \cdot 4\\
t_1 := x \cdot x + t_0\\
t_2 := \frac{t_0}{t_1}\\
t_3 := \left(y \cdot 4\right) \cdot y\\
\mathbf{if}\;\frac{x \cdot x - t_3}{x \cdot x + t_3} < 0.9743233849626781:\\
\;\;\;\;\frac{x \cdot x}{t_1} - t_2\\
\mathbf{else}:\\
\;\;\;\;{\left(\frac{x}{\sqrt{t_1}}\right)}^{2} - t_2\\
\end{array}
\end{array}
herbie shell --seed 2023313
(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))))