
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x - y) * (x + y)) / ((x * x) + (y * y))
end function
public static double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
def code(x, y): return ((x - y) * (x + y)) / ((x * x) + (y * y))
function code(x, y) return Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) end
function tmp = code(x, y) tmp = ((x - y) * (x + y)) / ((x * x) + (y * y)); end
code[x_, y_] := N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x - y) * (x + y)) / ((x * x) + (y * y))
end function
public static double code(double x, double y) {
return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
def code(x, y): return ((x - y) * (x + y)) / ((x * x) + (y * y))
function code(x, y) return Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) end
function tmp = code(x, y) tmp = ((x - y) * (x + y)) / ((x * x) + (y * y)); end
code[x_, y_] := N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\end{array}
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (/ (* (/ (- x y) (hypot x y)) (+ x y)) (hypot x y)))
y = abs(y);
double code(double x, double y) {
return (((x - y) / hypot(x, y)) * (x + y)) / hypot(x, y);
}
y = Math.abs(y);
public static double code(double x, double y) {
return (((x - y) / Math.hypot(x, y)) * (x + y)) / Math.hypot(x, y);
}
y = abs(y) def code(x, y): return (((x - y) / math.hypot(x, y)) * (x + y)) / math.hypot(x, y)
y = abs(y) function code(x, y) return Float64(Float64(Float64(Float64(x - y) / hypot(x, y)) * Float64(x + y)) / hypot(x, y)) end
y = abs(y) function tmp = code(x, y) tmp = (((x - y) / hypot(x, y)) * (x + y)) / hypot(x, y); end
NOTE: y should be positive before calling this function code[x_, y_] := N[(N[(N[(N[(x - y), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y = |y|\\
\\
\frac{\frac{x - y}{\mathsf{hypot}\left(x, y\right)} \cdot \left(x + y\right)}{\mathsf{hypot}\left(x, y\right)}
\end{array}
Initial program 67.9%
add-sqr-sqrt67.9%
times-frac68.7%
hypot-def68.7%
hypot-def99.9%
Applied egg-rr99.9%
associate-*r/100.0%
+-commutative100.0%
Simplified100.0%
Final simplification100.0%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))) (if (<= t_0 2.0) t_0 (/ (- (* (/ x y) (* x 1.5)) y) (hypot x y)))))
y = abs(y);
double code(double x, double y) {
double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
double tmp;
if (t_0 <= 2.0) {
tmp = t_0;
} else {
tmp = (((x / y) * (x * 1.5)) - y) / hypot(x, y);
}
return tmp;
}
y = Math.abs(y);
public static double code(double x, double y) {
double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
double tmp;
if (t_0 <= 2.0) {
tmp = t_0;
} else {
tmp = (((x / y) * (x * 1.5)) - y) / Math.hypot(x, y);
}
return tmp;
}
y = abs(y) def code(x, y): t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y)) tmp = 0 if t_0 <= 2.0: tmp = t_0 else: tmp = (((x / y) * (x * 1.5)) - y) / math.hypot(x, y) return tmp
y = abs(y) function code(x, y) t_0 = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) tmp = 0.0 if (t_0 <= 2.0) tmp = t_0; else tmp = Float64(Float64(Float64(Float64(x / y) * Float64(x * 1.5)) - y) / hypot(x, y)); end return tmp end
y = abs(y) function tmp_2 = code(x, y) t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y)); tmp = 0.0; if (t_0 <= 2.0) tmp = t_0; else tmp = (((x / y) * (x * 1.5)) - y) / hypot(x, y); end tmp_2 = tmp; end
NOTE: y should be positive before calling this function
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$0, N[(N[(N[(N[(x / y), $MachinePrecision] * N[(x * 1.5), $MachinePrecision]), $MachinePrecision] - y), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{x}{y} \cdot \left(x \cdot 1.5\right) - y}{\mathsf{hypot}\left(x, y\right)}\\
\end{array}
\end{array}
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 100.0%
if 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 0.0%
add-sqr-sqrt0.0%
times-frac3.1%
hypot-def3.1%
hypot-def99.9%
Applied egg-rr99.9%
associate-*r/100.0%
+-commutative100.0%
Simplified100.0%
clear-num99.9%
inv-pow99.9%
Applied egg-rr99.9%
unpow-199.9%
Simplified99.9%
Taylor expanded in x around 0 8.0%
mul-1-neg8.0%
unsub-neg8.0%
*-commutative8.0%
unpow28.0%
associate-*l/9.4%
associate-*l*9.4%
Simplified9.4%
Final simplification71.0%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))) (if (<= t_0 2.0) t_0 (fma 2.0 (* (/ x y) (/ x y)) -1.0))))
y = abs(y);
double code(double x, double y) {
double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
double tmp;
if (t_0 <= 2.0) {
tmp = t_0;
} else {
tmp = fma(2.0, ((x / y) * (x / y)), -1.0);
}
return tmp;
}
y = abs(y) function code(x, y) t_0 = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) tmp = 0.0 if (t_0 <= 2.0) tmp = t_0; else tmp = fma(2.0, Float64(Float64(x / y) * Float64(x / y)), -1.0); end return tmp end
NOTE: y should be positive before calling this function
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$0, N[(2.0 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, \frac{x}{y} \cdot \frac{x}{y}, -1\right)\\
\end{array}
\end{array}
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 100.0%
if 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 0.0%
add-sqr-sqrt0.0%
times-frac3.1%
hypot-def3.1%
hypot-def99.9%
Applied egg-rr99.9%
associate-*r/100.0%
+-commutative100.0%
Simplified100.0%
Taylor expanded in x around 0 61.0%
fma-neg61.0%
unpow261.0%
unpow261.0%
times-frac81.5%
metadata-eval81.5%
Simplified81.5%
Final simplification94.0%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))) (if (<= t_0 2.0) t_0 (* (+ (/ x y) -1.0) (/ (+ x y) y)))))
y = abs(y);
double code(double x, double y) {
double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
double tmp;
if (t_0 <= 2.0) {
tmp = t_0;
} else {
tmp = ((x / y) + -1.0) * ((x + y) / y);
}
return tmp;
}
NOTE: y should be positive before calling this function
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 = ((x - y) * (x + y)) / ((x * x) + (y * y))
if (t_0 <= 2.0d0) then
tmp = t_0
else
tmp = ((x / y) + (-1.0d0)) * ((x + y) / y)
end if
code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
double tmp;
if (t_0 <= 2.0) {
tmp = t_0;
} else {
tmp = ((x / y) + -1.0) * ((x + y) / y);
}
return tmp;
}
y = abs(y) def code(x, y): t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y)) tmp = 0 if t_0 <= 2.0: tmp = t_0 else: tmp = ((x / y) + -1.0) * ((x + y) / y) return tmp
y = abs(y) function code(x, y) t_0 = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) tmp = 0.0 if (t_0 <= 2.0) tmp = t_0; else tmp = Float64(Float64(Float64(x / y) + -1.0) * Float64(Float64(x + y) / y)); end return tmp end
y = abs(y) function tmp_2 = code(x, y) t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y)); tmp = 0.0; if (t_0 <= 2.0) tmp = t_0; else tmp = ((x / y) + -1.0) * ((x + y) / y); end tmp_2 = tmp; end
NOTE: y should be positive before calling this function
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 2.0], t$95$0, N[(N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision] * N[(N[(x + y), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
\mathbf{if}\;t_0 \leq 2:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\left(\frac{x}{y} + -1\right) \cdot \frac{x + y}{y}\\
\end{array}
\end{array}
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 100.0%
if 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 0.0%
Taylor expanded in x around 0 0.0%
unpow20.0%
Simplified0.0%
times-frac80.8%
Applied egg-rr80.8%
div-sub80.8%
sub-neg80.8%
*-inverses80.8%
metadata-eval80.8%
+-commutative80.8%
Simplified80.8%
Final simplification93.8%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (if (<= y 8.5e-171) (+ 1.0 (* -2.0 (* (/ y x) (/ y x)))) (+ -1.0 (/ x (* y (/ y x))))))
y = abs(y);
double code(double x, double y) {
double tmp;
if (y <= 8.5e-171) {
tmp = 1.0 + (-2.0 * ((y / x) * (y / x)));
} else {
tmp = -1.0 + (x / (y * (y / x)));
}
return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 8.5d-171) then
tmp = 1.0d0 + ((-2.0d0) * ((y / x) * (y / x)))
else
tmp = (-1.0d0) + (x / (y * (y / x)))
end if
code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
double tmp;
if (y <= 8.5e-171) {
tmp = 1.0 + (-2.0 * ((y / x) * (y / x)));
} else {
tmp = -1.0 + (x / (y * (y / x)));
}
return tmp;
}
y = abs(y) def code(x, y): tmp = 0 if y <= 8.5e-171: tmp = 1.0 + (-2.0 * ((y / x) * (y / x))) else: tmp = -1.0 + (x / (y * (y / x))) return tmp
y = abs(y) function code(x, y) tmp = 0.0 if (y <= 8.5e-171) tmp = Float64(1.0 + Float64(-2.0 * Float64(Float64(y / x) * Float64(y / x)))); else tmp = Float64(-1.0 + Float64(x / Float64(y * Float64(y / x)))); end return tmp end
y = abs(y) function tmp_2 = code(x, y) tmp = 0.0; if (y <= 8.5e-171) tmp = 1.0 + (-2.0 * ((y / x) * (y / x))); else tmp = -1.0 + (x / (y * (y / x))); end tmp_2 = tmp; end
NOTE: y should be positive before calling this function code[x_, y_] := If[LessEqual[y, 8.5e-171], N[(1.0 + N[(-2.0 * N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-1.0 + N[(x / N[(y * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 8.5 \cdot 10^{-171}:\\
\;\;\;\;1 + -2 \cdot \left(\frac{y}{x} \cdot \frac{y}{x}\right)\\
\mathbf{else}:\\
\;\;\;\;-1 + \frac{x}{y \cdot \frac{y}{x}}\\
\end{array}
\end{array}
if y < 8.50000000000000032e-171Initial program 61.3%
associate-*r/62.2%
+-commutative62.2%
fma-def62.2%
Simplified62.2%
Taylor expanded in y around 0 25.6%
unpow225.6%
unpow225.6%
Simplified25.6%
times-frac33.2%
Applied egg-rr33.2%
if 8.50000000000000032e-171 < y Initial program 94.1%
Taylor expanded in x around 0 68.1%
unpow268.1%
Simplified68.1%
Taylor expanded in x around 0 68.1%
sub-neg68.1%
metadata-eval68.1%
unpow268.1%
unpow268.1%
associate-/l*68.1%
Simplified68.1%
associate-/l*73.9%
associate-/r/73.9%
Applied egg-rr73.9%
Final simplification41.4%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (if (<= y 1.35e-170) 1.0 (+ -1.0 (/ x (* y (/ y x))))))
y = abs(y);
double code(double x, double y) {
double tmp;
if (y <= 1.35e-170) {
tmp = 1.0;
} else {
tmp = -1.0 + (x / (y * (y / x)));
}
return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 1.35d-170) then
tmp = 1.0d0
else
tmp = (-1.0d0) + (x / (y * (y / x)))
end if
code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
double tmp;
if (y <= 1.35e-170) {
tmp = 1.0;
} else {
tmp = -1.0 + (x / (y * (y / x)));
}
return tmp;
}
y = abs(y) def code(x, y): tmp = 0 if y <= 1.35e-170: tmp = 1.0 else: tmp = -1.0 + (x / (y * (y / x))) return tmp
y = abs(y) function code(x, y) tmp = 0.0 if (y <= 1.35e-170) tmp = 1.0; else tmp = Float64(-1.0 + Float64(x / Float64(y * Float64(y / x)))); end return tmp end
y = abs(y) function tmp_2 = code(x, y) tmp = 0.0; if (y <= 1.35e-170) tmp = 1.0; else tmp = -1.0 + (x / (y * (y / x))); end tmp_2 = tmp; end
NOTE: y should be positive before calling this function code[x_, y_] := If[LessEqual[y, 1.35e-170], 1.0, N[(-1.0 + N[(x / N[(y * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.35 \cdot 10^{-170}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1 + \frac{x}{y \cdot \frac{y}{x}}\\
\end{array}
\end{array}
if y < 1.3499999999999999e-170Initial program 61.3%
associate-*r/62.2%
+-commutative62.2%
fma-def62.2%
Simplified62.2%
Taylor expanded in x around inf 31.1%
if 1.3499999999999999e-170 < y Initial program 94.1%
Taylor expanded in x around 0 68.1%
unpow268.1%
Simplified68.1%
Taylor expanded in x around 0 68.1%
sub-neg68.1%
metadata-eval68.1%
unpow268.1%
unpow268.1%
associate-/l*68.1%
Simplified68.1%
associate-/l*73.9%
associate-/r/73.9%
Applied egg-rr73.9%
Final simplification39.8%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (if (<= y 6.2e-169) 1.0 -1.0))
y = abs(y);
double code(double x, double y) {
double tmp;
if (y <= 6.2e-169) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 6.2d-169) then
tmp = 1.0d0
else
tmp = -1.0d0
end if
code = tmp
end function
y = Math.abs(y);
public static double code(double x, double y) {
double tmp;
if (y <= 6.2e-169) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
y = abs(y) def code(x, y): tmp = 0 if y <= 6.2e-169: tmp = 1.0 else: tmp = -1.0 return tmp
y = abs(y) function code(x, y) tmp = 0.0 if (y <= 6.2e-169) tmp = 1.0; else tmp = -1.0; end return tmp end
y = abs(y) function tmp_2 = code(x, y) tmp = 0.0; if (y <= 6.2e-169) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
NOTE: y should be positive before calling this function code[x_, y_] := If[LessEqual[y, 6.2e-169], 1.0, -1.0]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6.2 \cdot 10^{-169}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < 6.2000000000000004e-169Initial program 61.2%
associate-*r/62.1%
+-commutative62.1%
fma-def62.1%
Simplified62.1%
Taylor expanded in x around inf 31.3%
if 6.2000000000000004e-169 < y Initial program 95.9%
associate-*r/94.6%
+-commutative94.6%
fma-def94.6%
Simplified94.6%
Taylor expanded in x around 0 73.3%
Final simplification39.5%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 -1.0)
y = abs(y);
double code(double x, double y) {
return -1.0;
}
NOTE: y should be positive before calling this function
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -1.0d0
end function
y = Math.abs(y);
public static double code(double x, double y) {
return -1.0;
}
y = abs(y) def code(x, y): return -1.0
y = abs(y) function code(x, y) return -1.0 end
y = abs(y) function tmp = code(x, y) tmp = -1.0; end
NOTE: y should be positive before calling this function code[x_, y_] := -1.0
\begin{array}{l}
y = |y|\\
\\
-1
\end{array}
Initial program 67.9%
associate-*r/68.4%
+-commutative68.4%
fma-def68.4%
Simplified68.4%
Taylor expanded in x around 0 68.9%
Final simplification68.9%
(FPCore (x y)
:precision binary64
(let* ((t_0 (fabs (/ x y))))
(if (and (< 0.5 t_0) (< t_0 2.0))
(/ (* (- x y) (+ x y)) (+ (* x x) (* y y)))
(- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))))
double code(double x, double y) {
double t_0 = fabs((x / y));
double tmp;
if ((0.5 < t_0) && (t_0 < 2.0)) {
tmp = ((x - y) * (x + y)) / ((x * x) + (y * y));
} else {
tmp = 1.0 - (2.0 / (1.0 + ((x / y) * (x / y))));
}
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 = abs((x / y))
if ((0.5d0 < t_0) .and. (t_0 < 2.0d0)) then
tmp = ((x - y) * (x + y)) / ((x * x) + (y * y))
else
tmp = 1.0d0 - (2.0d0 / (1.0d0 + ((x / y) * (x / y))))
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = Math.abs((x / y));
double tmp;
if ((0.5 < t_0) && (t_0 < 2.0)) {
tmp = ((x - y) * (x + y)) / ((x * x) + (y * y));
} else {
tmp = 1.0 - (2.0 / (1.0 + ((x / y) * (x / y))));
}
return tmp;
}
def code(x, y): t_0 = math.fabs((x / y)) tmp = 0 if (0.5 < t_0) and (t_0 < 2.0): tmp = ((x - y) * (x + y)) / ((x * x) + (y * y)) else: tmp = 1.0 - (2.0 / (1.0 + ((x / y) * (x / y)))) return tmp
function code(x, y) t_0 = abs(Float64(x / y)) tmp = 0.0 if ((0.5 < t_0) && (t_0 < 2.0)) tmp = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))); else tmp = Float64(1.0 - Float64(2.0 / Float64(1.0 + Float64(Float64(x / y) * Float64(x / y))))); end return tmp end
function tmp_2 = code(x, y) t_0 = abs((x / y)); tmp = 0.0; if ((0.5 < t_0) && (t_0 < 2.0)) tmp = ((x - y) * (x + y)) / ((x * x) + (y * y)); else tmp = 1.0 - (2.0 / (1.0 + ((x / y) * (x / y)))); end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision]}, If[And[Less[0.5, t$95$0], Less[t$95$0, 2.0]], N[(N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 - N[(2.0 / N[(1.0 + N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left|\frac{x}{y}\right|\\
\mathbf{if}\;0.5 < t_0 \land t_0 < 2:\\
\;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
\mathbf{else}:\\
\;\;\;\;1 - \frac{2}{1 + \frac{x}{y} \cdot \frac{x}{y}}\\
\end{array}
\end{array}
herbie shell --seed 2023200
(FPCore (x y)
:name "Kahan p9 Example"
:precision binary64
:pre (and (and (< 0.0 x) (< x 1.0)) (< y 1.0))
:herbie-target
(if (and (< 0.5 (fabs (/ x y))) (< (fabs (/ x y)) 2.0)) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))
(/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))