
(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 (<= t_0 5e-247)
1.0
(if (<= t_0 2e+235)
(expm1
(log1p
(/
(fma y (* y -4.0) (pow x 2.0))
(+ (pow x 2.0) (* 4.0 (pow y 2.0))))))
(+ (* 0.5 (* (/ x y) (/ x y))) -1.0)))))
double code(double x, double y) {
double t_0 = y * (y * 4.0);
double tmp;
if (t_0 <= 5e-247) {
tmp = 1.0;
} else if (t_0 <= 2e+235) {
tmp = expm1(log1p((fma(y, (y * -4.0), pow(x, 2.0)) / (pow(x, 2.0) + (4.0 * pow(y, 2.0))))));
} else {
tmp = (0.5 * ((x / y) * (x / y))) + -1.0;
}
return tmp;
}
function code(x, y) t_0 = Float64(y * Float64(y * 4.0)) tmp = 0.0 if (t_0 <= 5e-247) tmp = 1.0; elseif (t_0 <= 2e+235) tmp = expm1(log1p(Float64(fma(y, Float64(y * -4.0), (x ^ 2.0)) / Float64((x ^ 2.0) + Float64(4.0 * (y ^ 2.0)))))); else tmp = Float64(Float64(0.5 * Float64(Float64(x / y) * Float64(x / y))) + -1.0); end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-247], 1.0, If[LessEqual[t$95$0, 2e+235], N[(Exp[N[Log[1 + N[(N[(y * N[(y * -4.0), $MachinePrecision] + N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision] / N[(N[Power[x, 2.0], $MachinePrecision] + N[(4.0 * N[Power[y, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]] - 1), $MachinePrecision], N[(N[(0.5 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
\mathbf{if}\;t\_0 \leq 5 \cdot 10^{-247}:\\
\;\;\;\;1\\
\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+235}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\mathsf{fma}\left(y, y \cdot -4, {x}^{2}\right)}{{x}^{2} + 4 \cdot {y}^{2}}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right) + -1\\
\end{array}
\end{array}
if (*.f64 (*.f64 y #s(literal 4 binary64)) y) < 4.99999999999999978e-247Initial program 61.0%
Taylor expanded in x around inf 86.8%
if 4.99999999999999978e-247 < (*.f64 (*.f64 y #s(literal 4 binary64)) y) < 2.0000000000000001e235Initial program 72.6%
expm1-log1p-u72.6%
*-un-lft-identity72.6%
sub-neg72.6%
+-commutative72.6%
*-commutative72.6%
distribute-rgt-neg-in72.6%
fma-define72.6%
distribute-rgt-neg-in72.6%
metadata-eval72.6%
pow272.6%
*-un-lft-identity72.6%
+-commutative72.6%
*-commutative72.6%
associate-*l*72.6%
fma-define72.6%
pow272.6%
pow272.6%
Applied egg-rr72.6%
fma-undefine72.6%
+-commutative72.6%
Applied egg-rr72.6%
if 2.0000000000000001e235 < (*.f64 (*.f64 y #s(literal 4 binary64)) y) Initial program 8.9%
Taylor expanded in x around 0 83.7%
unpow283.7%
unpow283.7%
times-frac89.3%
Applied egg-rr89.3%
Final simplification82.3%
(FPCore (x y)
:precision binary64
(let* ((t_0 (* y (* y 4.0))))
(if (<= t_0 5e-247)
1.0
(if (<= t_0 2e+235)
(/ (- (* x x) t_0) (+ t_0 (* x x)))
(+ (* 0.5 (* (/ x y) (/ x y))) -1.0)))))
double code(double x, double y) {
double t_0 = y * (y * 4.0);
double tmp;
if (t_0 <= 5e-247) {
tmp = 1.0;
} else if (t_0 <= 2e+235) {
tmp = ((x * x) - t_0) / (t_0 + (x * x));
} else {
tmp = (0.5 * ((x / y) * (x / y))) + -1.0;
}
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 (t_0 <= 5d-247) then
tmp = 1.0d0
else if (t_0 <= 2d+235) then
tmp = ((x * x) - t_0) / (t_0 + (x * x))
else
tmp = (0.5d0 * ((x / y) * (x / y))) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = y * (y * 4.0);
double tmp;
if (t_0 <= 5e-247) {
tmp = 1.0;
} else if (t_0 <= 2e+235) {
tmp = ((x * x) - t_0) / (t_0 + (x * x));
} else {
tmp = (0.5 * ((x / y) * (x / y))) + -1.0;
}
return tmp;
}
def code(x, y): t_0 = y * (y * 4.0) tmp = 0 if t_0 <= 5e-247: tmp = 1.0 elif t_0 <= 2e+235: tmp = ((x * x) - t_0) / (t_0 + (x * x)) else: tmp = (0.5 * ((x / y) * (x / y))) + -1.0 return tmp
function code(x, y) t_0 = Float64(y * Float64(y * 4.0)) tmp = 0.0 if (t_0 <= 5e-247) tmp = 1.0; elseif (t_0 <= 2e+235) tmp = Float64(Float64(Float64(x * x) - t_0) / Float64(t_0 + Float64(x * x))); else tmp = Float64(Float64(0.5 * Float64(Float64(x / y) * Float64(x / y))) + -1.0); end return tmp end
function tmp_2 = code(x, y) t_0 = y * (y * 4.0); tmp = 0.0; if (t_0 <= 5e-247) tmp = 1.0; elseif (t_0 <= 2e+235) tmp = ((x * x) - t_0) / (t_0 + (x * x)); else tmp = (0.5 * ((x / y) * (x / y))) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(y * N[(y * 4.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-247], 1.0, If[LessEqual[t$95$0, 2e+235], N[(N[(N[(x * x), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(t$95$0 + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
\mathbf{if}\;t\_0 \leq 5 \cdot 10^{-247}:\\
\;\;\;\;1\\
\mathbf{elif}\;t\_0 \leq 2 \cdot 10^{+235}:\\
\;\;\;\;\frac{x \cdot x - t\_0}{t\_0 + x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right) + -1\\
\end{array}
\end{array}
if (*.f64 (*.f64 y #s(literal 4 binary64)) y) < 4.99999999999999978e-247Initial program 61.0%
Taylor expanded in x around inf 86.8%
if 4.99999999999999978e-247 < (*.f64 (*.f64 y #s(literal 4 binary64)) y) < 2.0000000000000001e235Initial program 72.6%
if 2.0000000000000001e235 < (*.f64 (*.f64 y #s(literal 4 binary64)) y) Initial program 8.9%
Taylor expanded in x around 0 83.7%
unpow283.7%
unpow283.7%
times-frac89.3%
Applied egg-rr89.3%
Final simplification82.3%
(FPCore (x y) :precision binary64 (if (<= x 42000000000000.0) (+ (* 0.5 (* (/ x y) (/ x y))) -1.0) 1.0))
double code(double x, double y) {
double tmp;
if (x <= 42000000000000.0) {
tmp = (0.5 * ((x / y) * (x / y))) + -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 <= 42000000000000.0d0) then
tmp = (0.5d0 * ((x / y) * (x / y))) + (-1.0d0)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 42000000000000.0) {
tmp = (0.5 * ((x / y) * (x / y))) + -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 42000000000000.0: tmp = (0.5 * ((x / y) * (x / y))) + -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= 42000000000000.0) tmp = Float64(Float64(0.5 * Float64(Float64(x / y) * Float64(x / y))) + -1.0); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 42000000000000.0) tmp = (0.5 * ((x / y) * (x / y))) + -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 42000000000000.0], N[(N[(0.5 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 42000000000000:\\
\;\;\;\;0.5 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right) + -1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 4.2e13Initial program 50.0%
Taylor expanded in x around 0 55.8%
unpow255.8%
unpow255.8%
times-frac62.5%
Applied egg-rr62.5%
if 4.2e13 < x Initial program 46.8%
Taylor expanded in x around inf 83.1%
Final simplification67.5%
(FPCore (x y) :precision binary64 (if (<= x 1e+14) -1.0 1.0))
double code(double x, double y) {
double tmp;
if (x <= 1e+14) {
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 <= 1d+14) 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 <= 1e+14) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 1e+14: tmp = -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= 1e+14) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 1e+14) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 1e+14], -1.0, 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 10^{+14}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < 1e14Initial program 50.0%
Taylor expanded in x around 0 61.1%
if 1e14 < x Initial program 46.8%
Taylor expanded in x around inf 83.1%
(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 49.2%
Taylor expanded in x around 0 50.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 2024087
(FPCore (x y)
:name "Diagrams.TwoD.Arc:arcBetween from diagrams-lib-1.3.0.3"
:precision binary64
:alt
(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))))