
(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 7 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)) (/ (hypot x y) (- x y))))
y = abs(y);
double code(double x, double y) {
return ((x + y) / hypot(x, y)) / (hypot(x, y) / (x - y));
}
y = Math.abs(y);
public static double code(double x, double y) {
return ((x + y) / Math.hypot(x, y)) / (Math.hypot(x, y) / (x - y));
}
y = abs(y) def code(x, y): return ((x + y) / math.hypot(x, y)) / (math.hypot(x, y) / (x - y))
y = abs(y) function code(x, y) return Float64(Float64(Float64(x + y) / hypot(x, y)) / Float64(hypot(x, y) / Float64(x - y))) end
y = abs(y) function tmp = code(x, y) tmp = ((x + y) / hypot(x, y)) / (hypot(x, y) / (x - y)); end
NOTE: y should be positive before calling this function code[x_, y_] := N[(N[(N[(x + y), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y = |y|\\
\\
\frac{\frac{x + y}{\mathsf{hypot}\left(x, y\right)}}{\frac{\mathsf{hypot}\left(x, y\right)}{x - y}}
\end{array}
Initial program 69.1%
add-sqr-sqrt69.1%
times-frac69.9%
hypot-def69.9%
hypot-def100.0%
Applied egg-rr100.0%
*-commutative100.0%
clear-num100.0%
un-div-inv100.0%
Applied egg-rr100.0%
Final simplification100.0%
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(x + y) / hypot(x, y)) * Float64(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[(x + y), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(x - y), $MachinePrecision] / N[Sqrt[x ^ 2 + y ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
y = |y|\\
\\
\frac{x + y}{\mathsf{hypot}\left(x, y\right)} \cdot \frac{x - y}{\mathsf{hypot}\left(x, y\right)}
\end{array}
Initial program 69.1%
add-sqr-sqrt69.1%
times-frac69.9%
hypot-def69.9%
hypot-def100.0%
Applied egg-rr100.0%
Final simplification100.0%
NOTE: y should be positive before calling this function
(FPCore (x y)
:precision binary64
(if (<= y 6.5e-166)
(+ 1.0 (/ -2.0 (/ (/ x y) (/ y x))))
(if (<= y 5e-69)
(/ (* (+ x y) (- x y)) (+ (* x x) (* y y)))
(+ (* 2.0 (* (/ x y) (/ x y))) -1.0))))y = abs(y);
double code(double x, double y) {
double tmp;
if (y <= 6.5e-166) {
tmp = 1.0 + (-2.0 / ((x / y) / (y / x)));
} else if (y <= 5e-69) {
tmp = ((x + y) * (x - y)) / ((x * x) + (y * y));
} else {
tmp = (2.0 * ((x / y) * (x / y))) + -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.5d-166) then
tmp = 1.0d0 + ((-2.0d0) / ((x / y) / (y / x)))
else if (y <= 5d-69) then
tmp = ((x + y) * (x - y)) / ((x * x) + (y * y))
else
tmp = (2.0d0 * ((x / y) * (x / y))) + (-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.5e-166) {
tmp = 1.0 + (-2.0 / ((x / y) / (y / x)));
} else if (y <= 5e-69) {
tmp = ((x + y) * (x - y)) / ((x * x) + (y * y));
} else {
tmp = (2.0 * ((x / y) * (x / y))) + -1.0;
}
return tmp;
}
y = abs(y) def code(x, y): tmp = 0 if y <= 6.5e-166: tmp = 1.0 + (-2.0 / ((x / y) / (y / x))) elif y <= 5e-69: tmp = ((x + y) * (x - y)) / ((x * x) + (y * y)) else: tmp = (2.0 * ((x / y) * (x / y))) + -1.0 return tmp
y = abs(y) function code(x, y) tmp = 0.0 if (y <= 6.5e-166) tmp = Float64(1.0 + Float64(-2.0 / Float64(Float64(x / y) / Float64(y / x)))); elseif (y <= 5e-69) tmp = Float64(Float64(Float64(x + y) * Float64(x - y)) / Float64(Float64(x * x) + Float64(y * y))); else tmp = Float64(Float64(2.0 * Float64(Float64(x / y) * Float64(x / y))) + -1.0); end return tmp end
y = abs(y) function tmp_2 = code(x, y) tmp = 0.0; if (y <= 6.5e-166) tmp = 1.0 + (-2.0 / ((x / y) / (y / x))); elseif (y <= 5e-69) tmp = ((x + y) * (x - y)) / ((x * x) + (y * y)); else tmp = (2.0 * ((x / y) * (x / y))) + -1.0; end tmp_2 = tmp; end
NOTE: y should be positive before calling this function code[x_, y_] := If[LessEqual[y, 6.5e-166], N[(1.0 + N[(-2.0 / N[(N[(x / y), $MachinePrecision] / N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 5e-69], N[(N[(N[(x + y), $MachinePrecision] * N[(x - y), $MachinePrecision]), $MachinePrecision] / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6.5 \cdot 10^{-166}:\\
\;\;\;\;1 + \frac{-2}{\frac{\frac{x}{y}}{\frac{y}{x}}}\\
\mathbf{elif}\;y \leq 5 \cdot 10^{-69}:\\
\;\;\;\;\frac{\left(x + y\right) \cdot \left(x - y\right)}{x \cdot x + y \cdot y}\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right) + -1\\
\end{array}
\end{array}
if y < 6.50000000000000019e-166Initial program 62.6%
add-sqr-sqrt62.6%
times-frac63.5%
hypot-def63.6%
hypot-def100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 27.5%
associate-*r/27.5%
associate-/l*27.5%
unpow227.5%
unpow227.5%
times-frac39.4%
unpow239.4%
Simplified39.4%
pow239.4%
clear-num39.4%
un-div-inv39.4%
Applied egg-rr39.4%
if 6.50000000000000019e-166 < y < 5.00000000000000033e-69Initial program 100.0%
if 5.00000000000000033e-69 < y Initial program 99.9%
Taylor expanded in x around 0 100.0%
unpow2100.0%
unpow2100.0%
times-frac100.0%
Applied egg-rr100.0%
Final simplification50.1%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (if (or (<= y 1.6e-156) (and (not (<= y 7.2e-146)) (<= y 5.6e-133))) (+ 1.0 (/ -2.0 (/ (/ x y) (/ y x)))) -1.0))
y = abs(y);
double code(double x, double y) {
double tmp;
if ((y <= 1.6e-156) || (!(y <= 7.2e-146) && (y <= 5.6e-133))) {
tmp = 1.0 + (-2.0 / ((x / y) / (y / x)));
} 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 <= 1.6d-156) .or. (.not. (y <= 7.2d-146)) .and. (y <= 5.6d-133)) then
tmp = 1.0d0 + ((-2.0d0) / ((x / y) / (y / x)))
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 <= 1.6e-156) || (!(y <= 7.2e-146) && (y <= 5.6e-133))) {
tmp = 1.0 + (-2.0 / ((x / y) / (y / x)));
} else {
tmp = -1.0;
}
return tmp;
}
y = abs(y) def code(x, y): tmp = 0 if (y <= 1.6e-156) or (not (y <= 7.2e-146) and (y <= 5.6e-133)): tmp = 1.0 + (-2.0 / ((x / y) / (y / x))) else: tmp = -1.0 return tmp
y = abs(y) function code(x, y) tmp = 0.0 if ((y <= 1.6e-156) || (!(y <= 7.2e-146) && (y <= 5.6e-133))) tmp = Float64(1.0 + Float64(-2.0 / Float64(Float64(x / y) / Float64(y / x)))); else tmp = -1.0; end return tmp end
y = abs(y) function tmp_2 = code(x, y) tmp = 0.0; if ((y <= 1.6e-156) || (~((y <= 7.2e-146)) && (y <= 5.6e-133))) tmp = 1.0 + (-2.0 / ((x / y) / (y / x))); else tmp = -1.0; end tmp_2 = tmp; end
NOTE: y should be positive before calling this function code[x_, y_] := If[Or[LessEqual[y, 1.6e-156], And[N[Not[LessEqual[y, 7.2e-146]], $MachinePrecision], LessEqual[y, 5.6e-133]]], N[(1.0 + N[(-2.0 / N[(N[(x / y), $MachinePrecision] / N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.6 \cdot 10^{-156} \lor \neg \left(y \leq 7.2 \cdot 10^{-146}\right) \land y \leq 5.6 \cdot 10^{-133}:\\
\;\;\;\;1 + \frac{-2}{\frac{\frac{x}{y}}{\frac{y}{x}}}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < 1.59999999999999991e-156 or 7.19999999999999957e-146 < y < 5.5999999999999997e-133Initial program 63.9%
add-sqr-sqrt63.9%
times-frac64.9%
hypot-def64.9%
hypot-def100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 29.7%
associate-*r/29.7%
associate-/l*29.7%
unpow229.7%
unpow229.7%
times-frac41.2%
unpow241.2%
Simplified41.2%
pow241.2%
clear-num41.2%
un-div-inv41.2%
Applied egg-rr41.2%
if 1.59999999999999991e-156 < y < 7.19999999999999957e-146 or 5.5999999999999997e-133 < y Initial program 100.0%
Taylor expanded in x around 0 81.3%
Final simplification47.0%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (if (or (<= y 6.5e-163) (and (not (<= y 1.48e-145)) (<= y 1.1e-130))) (+ 1.0 (/ -2.0 (/ (/ x y) (/ y x)))) (+ (* 2.0 (* (/ x y) (/ x y))) -1.0)))
y = abs(y);
double code(double x, double y) {
double tmp;
if ((y <= 6.5e-163) || (!(y <= 1.48e-145) && (y <= 1.1e-130))) {
tmp = 1.0 + (-2.0 / ((x / y) / (y / x)));
} else {
tmp = (2.0 * ((x / y) * (x / y))) + -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.5d-163) .or. (.not. (y <= 1.48d-145)) .and. (y <= 1.1d-130)) then
tmp = 1.0d0 + ((-2.0d0) / ((x / y) / (y / x)))
else
tmp = (2.0d0 * ((x / y) * (x / y))) + (-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.5e-163) || (!(y <= 1.48e-145) && (y <= 1.1e-130))) {
tmp = 1.0 + (-2.0 / ((x / y) / (y / x)));
} else {
tmp = (2.0 * ((x / y) * (x / y))) + -1.0;
}
return tmp;
}
y = abs(y) def code(x, y): tmp = 0 if (y <= 6.5e-163) or (not (y <= 1.48e-145) and (y <= 1.1e-130)): tmp = 1.0 + (-2.0 / ((x / y) / (y / x))) else: tmp = (2.0 * ((x / y) * (x / y))) + -1.0 return tmp
y = abs(y) function code(x, y) tmp = 0.0 if ((y <= 6.5e-163) || (!(y <= 1.48e-145) && (y <= 1.1e-130))) tmp = Float64(1.0 + Float64(-2.0 / Float64(Float64(x / y) / Float64(y / x)))); else tmp = Float64(Float64(2.0 * Float64(Float64(x / y) * Float64(x / y))) + -1.0); end return tmp end
y = abs(y) function tmp_2 = code(x, y) tmp = 0.0; if ((y <= 6.5e-163) || (~((y <= 1.48e-145)) && (y <= 1.1e-130))) tmp = 1.0 + (-2.0 / ((x / y) / (y / x))); else tmp = (2.0 * ((x / y) * (x / y))) + -1.0; end tmp_2 = tmp; end
NOTE: y should be positive before calling this function code[x_, y_] := If[Or[LessEqual[y, 6.5e-163], And[N[Not[LessEqual[y, 1.48e-145]], $MachinePrecision], LessEqual[y, 1.1e-130]]], N[(1.0 + N[(-2.0 / N[(N[(x / y), $MachinePrecision] / N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6.5 \cdot 10^{-163} \lor \neg \left(y \leq 1.48 \cdot 10^{-145}\right) \land y \leq 1.1 \cdot 10^{-130}:\\
\;\;\;\;1 + \frac{-2}{\frac{\frac{x}{y}}{\frac{y}{x}}}\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right) + -1\\
\end{array}
\end{array}
if y < 6.4999999999999999e-163 or 1.47999999999999995e-145 < y < 1.0999999999999999e-130Initial program 63.8%
add-sqr-sqrt63.8%
times-frac64.7%
hypot-def64.7%
hypot-def100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 29.4%
associate-*r/29.4%
associate-/l*29.4%
unpow229.4%
unpow229.4%
times-frac40.9%
unpow240.9%
Simplified40.9%
pow240.9%
clear-num40.9%
un-div-inv40.9%
Applied egg-rr40.9%
if 6.4999999999999999e-163 < y < 1.47999999999999995e-145 or 1.0999999999999999e-130 < y Initial program 100.0%
Taylor expanded in x around 0 80.4%
unpow280.4%
unpow280.4%
times-frac80.4%
Applied egg-rr80.4%
Final simplification46.8%
NOTE: y should be positive before calling this function (FPCore (x y) :precision binary64 (if (<= y 4.9e-157) 1.0 (if (<= y 1.06e-145) -1.0 (if (<= y 3.8e-132) 1.0 -1.0))))
y = abs(y);
double code(double x, double y) {
double tmp;
if (y <= 4.9e-157) {
tmp = 1.0;
} else if (y <= 1.06e-145) {
tmp = -1.0;
} else if (y <= 3.8e-132) {
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 <= 4.9d-157) then
tmp = 1.0d0
else if (y <= 1.06d-145) then
tmp = -1.0d0
else if (y <= 3.8d-132) 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 <= 4.9e-157) {
tmp = 1.0;
} else if (y <= 1.06e-145) {
tmp = -1.0;
} else if (y <= 3.8e-132) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
y = abs(y) def code(x, y): tmp = 0 if y <= 4.9e-157: tmp = 1.0 elif y <= 1.06e-145: tmp = -1.0 elif y <= 3.8e-132: tmp = 1.0 else: tmp = -1.0 return tmp
y = abs(y) function code(x, y) tmp = 0.0 if (y <= 4.9e-157) tmp = 1.0; elseif (y <= 1.06e-145) tmp = -1.0; elseif (y <= 3.8e-132) 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 <= 4.9e-157) tmp = 1.0; elseif (y <= 1.06e-145) tmp = -1.0; elseif (y <= 3.8e-132) 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, 4.9e-157], 1.0, If[LessEqual[y, 1.06e-145], -1.0, If[LessEqual[y, 3.8e-132], 1.0, -1.0]]]
\begin{array}{l}
y = |y|\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 4.9 \cdot 10^{-157}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 1.06 \cdot 10^{-145}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 3.8 \cdot 10^{-132}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < 4.8999999999999998e-157 or 1.05999999999999996e-145 < y < 3.7999999999999997e-132Initial program 63.9%
Taylor expanded in x around inf 39.3%
if 4.8999999999999998e-157 < y < 1.05999999999999996e-145 or 3.7999999999999997e-132 < y Initial program 100.0%
Taylor expanded in x around 0 81.3%
Final simplification45.4%
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 69.1%
Taylor expanded in x around 0 64.4%
Final simplification64.4%
(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 2023299
(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))))