
(FPCore (x y) :precision binary64 (/ (* (* x 2.0) y) (- x y)))
double code(double x, double y) {
return ((x * 2.0) * y) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * 2.0d0) * y) / (x - y)
end function
public static double code(double x, double y) {
return ((x * 2.0) * y) / (x - y);
}
def code(x, y): return ((x * 2.0) * y) / (x - y)
function code(x, y) return Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y)) end
function tmp = code(x, y) tmp = ((x * 2.0) * y) / (x - y); end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(x \cdot 2\right) \cdot y}{x - y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (* (* x 2.0) y) (- x y)))
double code(double x, double y) {
return ((x * 2.0) * y) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = ((x * 2.0d0) * y) / (x - y)
end function
public static double code(double x, double y) {
return ((x * 2.0) * y) / (x - y);
}
def code(x, y): return ((x * 2.0) * y) / (x - y)
function code(x, y) return Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y)) end
function tmp = code(x, y) tmp = ((x * 2.0) * y) / (x - y); end
code[x_, y_] := N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(x \cdot 2\right) \cdot y}{x - y}
\end{array}
(FPCore (x y)
:precision binary64
(if (<= x -3.5e+174)
(* y 2.0)
(if (<= x 2.6e+105)
(* (* 2.0 x) (/ y (- x y)))
(* (fma (/ 2.0 x) y 2.0) y))))
double code(double x, double y) {
double tmp;
if (x <= -3.5e+174) {
tmp = y * 2.0;
} else if (x <= 2.6e+105) {
tmp = (2.0 * x) * (y / (x - y));
} else {
tmp = fma((2.0 / x), y, 2.0) * y;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= -3.5e+174) tmp = Float64(y * 2.0); elseif (x <= 2.6e+105) tmp = Float64(Float64(2.0 * x) * Float64(y / Float64(x - y))); else tmp = Float64(fma(Float64(2.0 / x), y, 2.0) * y); end return tmp end
code[x_, y_] := If[LessEqual[x, -3.5e+174], N[(y * 2.0), $MachinePrecision], If[LessEqual[x, 2.6e+105], N[(N[(2.0 * x), $MachinePrecision] * N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[(2.0 / x), $MachinePrecision] * y + 2.0), $MachinePrecision] * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.5 \cdot 10^{+174}:\\
\;\;\;\;y \cdot 2\\
\mathbf{elif}\;x \leq 2.6 \cdot 10^{+105}:\\
\;\;\;\;\left(2 \cdot x\right) \cdot \frac{y}{x - y}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{2}{x}, y, 2\right) \cdot y\\
\end{array}
\end{array}
if x < -3.5000000000000001e174Initial program 70.9%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f64100.0
Applied rewrites100.0%
if -3.5000000000000001e174 < x < 2.6000000000000002e105Initial program 78.9%
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6498.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6498.6
Applied rewrites98.6%
if 2.6000000000000002e105 < x Initial program 74.9%
Taylor expanded in y around 0
*-commutativeN/A
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6499.8
Applied rewrites99.8%
Final simplification98.9%
(FPCore (x y)
:precision binary64
(let* ((t_0 (* (/ -2.0 (- y x)) (* y x))) (t_1 (/ (* (* 2.0 x) y) (- x y))))
(if (<= t_1 (- INFINITY))
(* (fma (/ 2.0 x) y 2.0) y)
(if (<= t_1 -1e-299)
t_0
(if (<= t_1 0.0) (* y 2.0) (if (<= t_1 1e+127) t_0 (* -2.0 x)))))))
double code(double x, double y) {
double t_0 = (-2.0 / (y - x)) * (y * x);
double t_1 = ((2.0 * x) * y) / (x - y);
double tmp;
if (t_1 <= -((double) INFINITY)) {
tmp = fma((2.0 / x), y, 2.0) * y;
} else if (t_1 <= -1e-299) {
tmp = t_0;
} else if (t_1 <= 0.0) {
tmp = y * 2.0;
} else if (t_1 <= 1e+127) {
tmp = t_0;
} else {
tmp = -2.0 * x;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(-2.0 / Float64(y - x)) * Float64(y * x)) t_1 = Float64(Float64(Float64(2.0 * x) * y) / Float64(x - y)) tmp = 0.0 if (t_1 <= Float64(-Inf)) tmp = Float64(fma(Float64(2.0 / x), y, 2.0) * y); elseif (t_1 <= -1e-299) tmp = t_0; elseif (t_1 <= 0.0) tmp = Float64(y * 2.0); elseif (t_1 <= 1e+127) tmp = t_0; else tmp = Float64(-2.0 * x); end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(-2.0 / N[(y - x), $MachinePrecision]), $MachinePrecision] * N[(y * x), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(2.0 * x), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], N[(N[(N[(2.0 / x), $MachinePrecision] * y + 2.0), $MachinePrecision] * y), $MachinePrecision], If[LessEqual[t$95$1, -1e-299], t$95$0, If[LessEqual[t$95$1, 0.0], N[(y * 2.0), $MachinePrecision], If[LessEqual[t$95$1, 1e+127], t$95$0, N[(-2.0 * x), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-2}{y - x} \cdot \left(y \cdot x\right)\\
t_1 := \frac{\left(2 \cdot x\right) \cdot y}{x - y}\\
\mathbf{if}\;t\_1 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(\frac{2}{x}, y, 2\right) \cdot y\\
\mathbf{elif}\;t\_1 \leq -1 \cdot 10^{-299}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_1 \leq 0:\\
\;\;\;\;y \cdot 2\\
\mathbf{elif}\;t\_1 \leq 10^{+127}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot x\\
\end{array}
\end{array}
if (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) < -inf.0Initial program 4.4%
Taylor expanded in y around 0
*-commutativeN/A
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6479.6
Applied rewrites79.6%
if -inf.0 < (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) < -9.99999999999999992e-300 or -0.0 < (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) < 9.99999999999999955e126Initial program 99.8%
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
associate-*r*N/A
*-commutativeN/A
associate-/l*N/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
frac-2negN/A
metadata-evalN/A
metadata-evalN/A
lower-/.f64N/A
metadata-evalN/A
neg-sub0N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
associate--r+N/A
neg-sub0N/A
remove-double-negN/A
lower--.f6499.6
Applied rewrites99.6%
if -9.99999999999999992e-300 < (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) < -0.0Initial program 13.7%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6468.1
Applied rewrites68.1%
if 9.99999999999999955e126 < (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) Initial program 4.9%
Taylor expanded in y around inf
lower-*.f6469.9
Applied rewrites69.9%
Final simplification92.6%
(FPCore (x y) :precision binary64 (if (<= y -3.4e-11) (* -2.0 x) (if (<= y 8.8e+46) (* (fma (/ 2.0 x) y 2.0) y) (* -2.0 x))))
double code(double x, double y) {
double tmp;
if (y <= -3.4e-11) {
tmp = -2.0 * x;
} else if (y <= 8.8e+46) {
tmp = fma((2.0 / x), y, 2.0) * y;
} else {
tmp = -2.0 * x;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -3.4e-11) tmp = Float64(-2.0 * x); elseif (y <= 8.8e+46) tmp = Float64(fma(Float64(2.0 / x), y, 2.0) * y); else tmp = Float64(-2.0 * x); end return tmp end
code[x_, y_] := If[LessEqual[y, -3.4e-11], N[(-2.0 * x), $MachinePrecision], If[LessEqual[y, 8.8e+46], N[(N[(N[(2.0 / x), $MachinePrecision] * y + 2.0), $MachinePrecision] * y), $MachinePrecision], N[(-2.0 * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.4 \cdot 10^{-11}:\\
\;\;\;\;-2 \cdot x\\
\mathbf{elif}\;y \leq 8.8 \cdot 10^{+46}:\\
\;\;\;\;\mathsf{fma}\left(\frac{2}{x}, y, 2\right) \cdot y\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot x\\
\end{array}
\end{array}
if y < -3.3999999999999999e-11 or 8.8000000000000001e46 < y Initial program 77.8%
Taylor expanded in y around inf
lower-*.f6480.3
Applied rewrites80.3%
if -3.3999999999999999e-11 < y < 8.8000000000000001e46Initial program 77.2%
Taylor expanded in y around 0
*-commutativeN/A
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6475.6
Applied rewrites75.6%
(FPCore (x y) :precision binary64 (if (<= y -3.4e-11) (* -2.0 x) (if (<= y 2.7e+77) (* y 2.0) (* -2.0 x))))
double code(double x, double y) {
double tmp;
if (y <= -3.4e-11) {
tmp = -2.0 * x;
} else if (y <= 2.7e+77) {
tmp = y * 2.0;
} else {
tmp = -2.0 * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-3.4d-11)) then
tmp = (-2.0d0) * x
else if (y <= 2.7d+77) then
tmp = y * 2.0d0
else
tmp = (-2.0d0) * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -3.4e-11) {
tmp = -2.0 * x;
} else if (y <= 2.7e+77) {
tmp = y * 2.0;
} else {
tmp = -2.0 * x;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -3.4e-11: tmp = -2.0 * x elif y <= 2.7e+77: tmp = y * 2.0 else: tmp = -2.0 * x return tmp
function code(x, y) tmp = 0.0 if (y <= -3.4e-11) tmp = Float64(-2.0 * x); elseif (y <= 2.7e+77) tmp = Float64(y * 2.0); else tmp = Float64(-2.0 * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -3.4e-11) tmp = -2.0 * x; elseif (y <= 2.7e+77) tmp = y * 2.0; else tmp = -2.0 * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -3.4e-11], N[(-2.0 * x), $MachinePrecision], If[LessEqual[y, 2.7e+77], N[(y * 2.0), $MachinePrecision], N[(-2.0 * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.4 \cdot 10^{-11}:\\
\;\;\;\;-2 \cdot x\\
\mathbf{elif}\;y \leq 2.7 \cdot 10^{+77}:\\
\;\;\;\;y \cdot 2\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot x\\
\end{array}
\end{array}
if y < -3.3999999999999999e-11 or 2.6999999999999998e77 < y Initial program 77.9%
Taylor expanded in y around inf
lower-*.f6481.2
Applied rewrites81.2%
if -3.3999999999999999e-11 < y < 2.6999999999999998e77Initial program 77.2%
Taylor expanded in y around 0
*-commutativeN/A
lower-*.f6474.8
Applied rewrites74.8%
(FPCore (x y) :precision binary64 (* -2.0 x))
double code(double x, double y) {
return -2.0 * x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (-2.0d0) * x
end function
public static double code(double x, double y) {
return -2.0 * x;
}
def code(x, y): return -2.0 * x
function code(x, y) return Float64(-2.0 * x) end
function tmp = code(x, y) tmp = -2.0 * x; end
code[x_, y_] := N[(-2.0 * x), $MachinePrecision]
\begin{array}{l}
\\
-2 \cdot x
\end{array}
Initial program 77.5%
Taylor expanded in y around inf
lower-*.f6453.9
Applied rewrites53.9%
(FPCore (x y)
:precision binary64
(let* ((t_0 (* (/ (* 2.0 x) (- x y)) y)))
(if (< x -1.7210442634149447e+81)
t_0
(if (< x 83645045635564430.0) (/ (* x 2.0) (/ (- x y) y)) t_0))))
double code(double x, double y) {
double t_0 = ((2.0 * x) / (x - y)) * y;
double tmp;
if (x < -1.7210442634149447e+81) {
tmp = t_0;
} else if (x < 83645045635564430.0) {
tmp = (x * 2.0) / ((x - y) / y);
} else {
tmp = t_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 = ((2.0d0 * x) / (x - y)) * y
if (x < (-1.7210442634149447d+81)) then
tmp = t_0
else if (x < 83645045635564430.0d0) then
tmp = (x * 2.0d0) / ((x - y) / y)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = ((2.0 * x) / (x - y)) * y;
double tmp;
if (x < -1.7210442634149447e+81) {
tmp = t_0;
} else if (x < 83645045635564430.0) {
tmp = (x * 2.0) / ((x - y) / y);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = ((2.0 * x) / (x - y)) * y tmp = 0 if x < -1.7210442634149447e+81: tmp = t_0 elif x < 83645045635564430.0: tmp = (x * 2.0) / ((x - y) / y) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(Float64(2.0 * x) / Float64(x - y)) * y) tmp = 0.0 if (x < -1.7210442634149447e+81) tmp = t_0; elseif (x < 83645045635564430.0) tmp = Float64(Float64(x * 2.0) / Float64(Float64(x - y) / y)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = ((2.0 * x) / (x - y)) * y; tmp = 0.0; if (x < -1.7210442634149447e+81) tmp = t_0; elseif (x < 83645045635564430.0) tmp = (x * 2.0) / ((x - y) / y); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(2.0 * x), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]}, If[Less[x, -1.7210442634149447e+81], t$95$0, If[Less[x, 83645045635564430.0], N[(N[(x * 2.0), $MachinePrecision] / N[(N[(x - y), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{2 \cdot x}{x - y} \cdot y\\
\mathbf{if}\;x < -1.7210442634149447 \cdot 10^{+81}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x < 83645045635564430:\\
\;\;\;\;\frac{x \cdot 2}{\frac{x - y}{y}}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
herbie shell --seed 2024266
(FPCore (x y)
:name "Linear.Projection:perspective from linear-1.19.1.3, B"
:precision binary64
:alt
(! :herbie-platform default (if (< x -1721044263414944700000000000000000000000000000000000000000000000000000000000000000) (* (/ (* 2 x) (- x y)) y) (if (< x 83645045635564430) (/ (* x 2) (/ (- x y) y)) (* (/ (* 2 x) (- x y)) y))))
(/ (* (* x 2.0) y) (- x y)))