
(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 (or (<= x -3.6e+249) (not (<= x 1.35e+166))) (+ y y) (* (* (/ y (- x y)) x) 2.0)))
double code(double x, double y) {
double tmp;
if ((x <= -3.6e+249) || !(x <= 1.35e+166)) {
tmp = y + y;
} else {
tmp = ((y / (x - y)) * x) * 2.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-3.6d+249)) .or. (.not. (x <= 1.35d+166))) then
tmp = y + y
else
tmp = ((y / (x - y)) * x) * 2.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -3.6e+249) || !(x <= 1.35e+166)) {
tmp = y + y;
} else {
tmp = ((y / (x - y)) * x) * 2.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -3.6e+249) or not (x <= 1.35e+166): tmp = y + y else: tmp = ((y / (x - y)) * x) * 2.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -3.6e+249) || !(x <= 1.35e+166)) tmp = Float64(y + y); else tmp = Float64(Float64(Float64(y / Float64(x - y)) * x) * 2.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -3.6e+249) || ~((x <= 1.35e+166))) tmp = y + y; else tmp = ((y / (x - y)) * x) * 2.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -3.6e+249], N[Not[LessEqual[x, 1.35e+166]], $MachinePrecision]], N[(y + y), $MachinePrecision], N[(N[(N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision] * 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.6 \cdot 10^{+249} \lor \neg \left(x \leq 1.35 \cdot 10^{+166}\right):\\
\;\;\;\;y + y\\
\mathbf{else}:\\
\;\;\;\;\left(\frac{y}{x - y} \cdot x\right) \cdot 2\\
\end{array}
\end{array}
if x < -3.5999999999999997e249 or 1.35000000000000006e166 < x Initial program 79.8%
Taylor expanded in x around inf
lower-*.f6489.7
Applied rewrites89.7%
Applied rewrites89.7%
if -3.5999999999999997e249 < x < 1.35000000000000006e166Initial program 75.8%
lift-/.f64N/A
lift-*.f64N/A
associate-/l*N/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6498.3
Applied rewrites98.3%
Final simplification97.0%
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ (* (+ x x) y) (- x y))) (t_1 (/ (* (* x 2.0) y) (- x y))))
(if (<= t_1 (- INFINITY))
(* -2.0 x)
(if (<= t_1 -5e-308)
t_0
(if (<= t_1 0.0)
(* (fma x (/ x y) x) -2.0)
(if (<= t_1 4e+139) t_0 (* -2.0 x)))))))
double code(double x, double y) {
double t_0 = ((x + x) * y) / (x - y);
double t_1 = ((x * 2.0) * y) / (x - y);
double tmp;
if (t_1 <= -((double) INFINITY)) {
tmp = -2.0 * x;
} else if (t_1 <= -5e-308) {
tmp = t_0;
} else if (t_1 <= 0.0) {
tmp = fma(x, (x / y), x) * -2.0;
} else if (t_1 <= 4e+139) {
tmp = t_0;
} else {
tmp = -2.0 * x;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(Float64(x + x) * y) / Float64(x - y)) t_1 = Float64(Float64(Float64(x * 2.0) * y) / Float64(x - y)) tmp = 0.0 if (t_1 <= Float64(-Inf)) tmp = Float64(-2.0 * x); elseif (t_1 <= -5e-308) tmp = t_0; elseif (t_1 <= 0.0) tmp = Float64(fma(x, Float64(x / y), x) * -2.0); elseif (t_1 <= 4e+139) tmp = t_0; else tmp = Float64(-2.0 * x); end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(N[(x + x), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], N[(-2.0 * x), $MachinePrecision], If[LessEqual[t$95$1, -5e-308], t$95$0, If[LessEqual[t$95$1, 0.0], N[(N[(x * N[(x / y), $MachinePrecision] + x), $MachinePrecision] * -2.0), $MachinePrecision], If[LessEqual[t$95$1, 4e+139], t$95$0, N[(-2.0 * x), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\left(x + x\right) \cdot y}{x - y}\\
t_1 := \frac{\left(x \cdot 2\right) \cdot y}{x - y}\\
\mathbf{if}\;t\_1 \leq -\infty:\\
\;\;\;\;-2 \cdot x\\
\mathbf{elif}\;t\_1 \leq -5 \cdot 10^{-308}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_1 \leq 0:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{x}{y}, x\right) \cdot -2\\
\mathbf{elif}\;t\_1 \leq 4 \cdot 10^{+139}:\\
\;\;\;\;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.0 or 4.00000000000000013e139 < (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) Initial program 4.8%
Taylor expanded in x around 0
lower-*.f6478.5
Applied rewrites78.5%
if -inf.0 < (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) < -4.99999999999999955e-308 or 0.0 < (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) < 4.00000000000000013e139Initial program 99.1%
lift-*.f64N/A
*-commutativeN/A
count-2-revN/A
lower-+.f6499.1
Applied rewrites99.1%
if -4.99999999999999955e-308 < (/.f64 (*.f64 (*.f64 x #s(literal 2 binary64)) y) (-.f64 x y)) < 0.0Initial program 8.1%
Taylor expanded in y around inf
distribute-lft-outN/A
*-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6464.3
Applied rewrites64.3%
(FPCore (x y) :precision binary64 (if (or (<= y -9e-45) (not (<= y 140000.0))) (* -2.0 x) (+ y y)))
double code(double x, double y) {
double tmp;
if ((y <= -9e-45) || !(y <= 140000.0)) {
tmp = -2.0 * x;
} else {
tmp = y + y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-9d-45)) .or. (.not. (y <= 140000.0d0))) then
tmp = (-2.0d0) * x
else
tmp = y + y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -9e-45) || !(y <= 140000.0)) {
tmp = -2.0 * x;
} else {
tmp = y + y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -9e-45) or not (y <= 140000.0): tmp = -2.0 * x else: tmp = y + y return tmp
function code(x, y) tmp = 0.0 if ((y <= -9e-45) || !(y <= 140000.0)) tmp = Float64(-2.0 * x); else tmp = Float64(y + y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -9e-45) || ~((y <= 140000.0))) tmp = -2.0 * x; else tmp = y + y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -9e-45], N[Not[LessEqual[y, 140000.0]], $MachinePrecision]], N[(-2.0 * x), $MachinePrecision], N[(y + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -9 \cdot 10^{-45} \lor \neg \left(y \leq 140000\right):\\
\;\;\;\;-2 \cdot x\\
\mathbf{else}:\\
\;\;\;\;y + y\\
\end{array}
\end{array}
if y < -8.9999999999999997e-45 or 1.4e5 < y Initial program 80.1%
Taylor expanded in x around 0
lower-*.f6484.1
Applied rewrites84.1%
if -8.9999999999999997e-45 < y < 1.4e5Initial program 72.6%
Taylor expanded in x around inf
lower-*.f6472.3
Applied rewrites72.3%
Applied rewrites72.3%
Final simplification78.3%
(FPCore (x y) :precision binary64 (+ y y))
double code(double x, double y) {
return y + y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y + y
end function
public static double code(double x, double y) {
return y + y;
}
def code(x, y): return y + y
function code(x, y) return Float64(y + y) end
function tmp = code(x, y) tmp = y + y; end
code[x_, y_] := N[(y + y), $MachinePrecision]
\begin{array}{l}
\\
y + y
\end{array}
Initial program 76.4%
Taylor expanded in x around inf
lower-*.f6444.8
Applied rewrites44.8%
Applied rewrites44.8%
(FPCore (x y) :precision binary64 (+ x x))
double code(double x, double y) {
return x + x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + x
end function
public static double code(double x, double y) {
return x + x;
}
def code(x, y): return x + x
function code(x, y) return Float64(x + x) end
function tmp = code(x, y) tmp = x + x; end
code[x_, y_] := N[(x + x), $MachinePrecision]
\begin{array}{l}
\\
x + x
\end{array}
Initial program 76.4%
Taylor expanded in x around inf
lower-*.f6444.8
Applied rewrites44.8%
Applied rewrites44.8%
Applied rewrites3.4%
(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 2024338
(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)))