
(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 4 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}
(FPCore (x y)
:precision binary64
(let* ((t_0 (* (- x y) (+ x y))))
(if (<= (/ t_0 (+ (* x x) (* y y))) 2.0)
(/ t_0 (fma y y (* x x)))
(fma (/ x y) (/ x (* y 0.5)) -1.0))))
double code(double x, double y) {
double t_0 = (x - y) * (x + y);
double tmp;
if ((t_0 / ((x * x) + (y * y))) <= 2.0) {
tmp = t_0 / fma(y, y, (x * x));
} else {
tmp = fma((x / y), (x / (y * 0.5)), -1.0);
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(x - y) * Float64(x + y)) tmp = 0.0 if (Float64(t_0 / Float64(Float64(x * x) + Float64(y * y))) <= 2.0) tmp = Float64(t_0 / fma(y, y, Float64(x * x))); else tmp = fma(Float64(x / y), Float64(x / Float64(y * 0.5)), -1.0); end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(x - y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(t$95$0 / N[(N[(x * x), $MachinePrecision] + N[(y * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], N[(t$95$0 / N[(y * y + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(x / y), $MachinePrecision] * N[(x / N[(y * 0.5), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(x - y\right) \cdot \left(x + y\right)\\
\mathbf{if}\;\frac{t\_0}{x \cdot x + y \cdot y} \leq 2:\\
\;\;\;\;\frac{t\_0}{\mathsf{fma}\left(y, y, x \cdot x\right)}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{y}, \frac{x}{y \cdot 0.5}, -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%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.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
sub-negN/A
*-commutativeN/A
metadata-evalN/A
associate-*l/N/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f6449.3
Applied rewrites49.3%
Applied rewrites76.0%
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(t_1 (fma (/ x y) (/ x (* y 0.5)) -1.0)))
(if (<= t_0 -0.5) t_1 (if (<= t_0 2.0) 1.0 t_1))))
double code(double x, double y) {
double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
double t_1 = fma((x / y), (x / (y * 0.5)), -1.0);
double tmp;
if (t_0 <= -0.5) {
tmp = t_1;
} else if (t_0 <= 2.0) {
tmp = 1.0;
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(Float64(x - y) * Float64(x + y)) / Float64(Float64(x * x) + Float64(y * y))) t_1 = fma(Float64(x / y), Float64(x / Float64(y * 0.5)), -1.0) tmp = 0.0 if (t_0 <= -0.5) tmp = t_1; elseif (t_0 <= 2.0) tmp = 1.0; else tmp = t_1; end return tmp end
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]}, Block[{t$95$1 = N[(N[(x / y), $MachinePrecision] * N[(x / N[(y * 0.5), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[t$95$0, -0.5], t$95$1, If[LessEqual[t$95$0, 2.0], 1.0, t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
t_1 := \mathsf{fma}\left(\frac{x}{y}, \frac{x}{y \cdot 0.5}, -1\right)\\
\mathbf{if}\;t\_0 \leq -0.5:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 2:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < -0.5 or 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 64.1%
Taylor expanded in x around 0
sub-negN/A
*-commutativeN/A
metadata-evalN/A
associate-*l/N/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f6480.8
Applied rewrites80.8%
Applied rewrites91.4%
if -0.5 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 100.0%
Taylor expanded in x around inf
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))) (if (<= t_0 -0.5) -1.0 (if (<= t_0 2.0) 1.0 -1.0))))
double code(double x, double y) {
double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
double tmp;
if (t_0 <= -0.5) {
tmp = -1.0;
} else if (t_0 <= 2.0) {
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) :: t_0
real(8) :: tmp
t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y))
if (t_0 <= (-0.5d0)) then
tmp = -1.0d0
else if (t_0 <= 2.0d0) then
tmp = 1.0d0
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
double tmp;
if (t_0 <= -0.5) {
tmp = -1.0;
} else if (t_0 <= 2.0) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y)) tmp = 0 if t_0 <= -0.5: tmp = -1.0 elif t_0 <= 2.0: tmp = 1.0 else: tmp = -1.0 return tmp
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 <= -0.5) tmp = -1.0; elseif (t_0 <= 2.0) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y)); tmp = 0.0; if (t_0 <= -0.5) tmp = -1.0; elseif (t_0 <= 2.0) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
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, -0.5], -1.0, If[LessEqual[t$95$0, 2.0], 1.0, -1.0]]]
\begin{array}{l}
\\
\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 -0.5:\\
\;\;\;\;-1\\
\mathbf{elif}\;t\_0 \leq 2:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < -0.5 or 2 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) Initial program 64.1%
Taylor expanded in x around 0
Applied rewrites90.1%
if -0.5 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 2Initial program 100.0%
Taylor expanded in x around inf
Applied rewrites100.0%
(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 72.3%
Taylor expanded in x around 0
Applied rewrites70.1%
(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 2024233
(FPCore (x y)
:name "Kahan p9 Example"
:precision binary64
:pre (and (and (< 0.0 x) (< x 1.0)) (< y 1.0))
:alt
(! :herbie-platform default (if (< 1/2 (fabs (/ x y)) 2) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1 (/ 2 (+ 1 (* (/ x y) (/ x y)))))))
(/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))