
(FPCore (x y) :precision binary64 (/ (+ x y) (- x y)))
double code(double x, double y) {
return (x + y) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / (x - y)
end function
public static double code(double x, double y) {
return (x + y) / (x - y);
}
def code(x, y): return (x + y) / (x - y)
function code(x, y) return Float64(Float64(x + y) / Float64(x - y)) end
function tmp = code(x, y) tmp = (x + y) / (x - y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{x - y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (+ x y) (- x y)))
double code(double x, double y) {
return (x + y) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / (x - y)
end function
public static double code(double x, double y) {
return (x + y) / (x - y);
}
def code(x, y): return (x + y) / (x - y)
function code(x, y) return Float64(Float64(x + y) / Float64(x - y)) end
function tmp = code(x, y) tmp = (x + y) / (x - y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{x - y}
\end{array}
(FPCore (x y) :precision binary64 (let* ((t_0 (/ (+ y x) (- x y))) (t_1 (+ 1.0 t_0))) (* (/ 1.0 (+ 2.0 t_0)) (+ -1.0 (+ t_1 (* t_0 t_1))))))
double code(double x, double y) {
double t_0 = (y + x) / (x - y);
double t_1 = 1.0 + t_0;
return (1.0 / (2.0 + t_0)) * (-1.0 + (t_1 + (t_0 * t_1)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: t_1
t_0 = (y + x) / (x - y)
t_1 = 1.0d0 + t_0
code = (1.0d0 / (2.0d0 + t_0)) * ((-1.0d0) + (t_1 + (t_0 * t_1)))
end function
public static double code(double x, double y) {
double t_0 = (y + x) / (x - y);
double t_1 = 1.0 + t_0;
return (1.0 / (2.0 + t_0)) * (-1.0 + (t_1 + (t_0 * t_1)));
}
def code(x, y): t_0 = (y + x) / (x - y) t_1 = 1.0 + t_0 return (1.0 / (2.0 + t_0)) * (-1.0 + (t_1 + (t_0 * t_1)))
function code(x, y) t_0 = Float64(Float64(y + x) / Float64(x - y)) t_1 = Float64(1.0 + t_0) return Float64(Float64(1.0 / Float64(2.0 + t_0)) * Float64(-1.0 + Float64(t_1 + Float64(t_0 * t_1)))) end
function tmp = code(x, y) t_0 = (y + x) / (x - y); t_1 = 1.0 + t_0; tmp = (1.0 / (2.0 + t_0)) * (-1.0 + (t_1 + (t_0 * t_1))); end
code[x_, y_] := Block[{t$95$0 = N[(N[(y + x), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 + t$95$0), $MachinePrecision]}, N[(N[(1.0 / N[(2.0 + t$95$0), $MachinePrecision]), $MachinePrecision] * N[(-1.0 + N[(t$95$1 + N[(t$95$0 * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y + x}{x - y}\\
t_1 := 1 + t\_0\\
\frac{1}{2 + t\_0} \cdot \left(-1 + \left(t\_1 + t\_0 \cdot t\_1\right)\right)
\end{array}
\end{array}
Initial program 99.9%
expm1-log1p-u98.8%
Applied egg-rr98.8%
expm1-define98.7%
flip--98.7%
div-inv98.7%
metadata-eval98.7%
sub-neg98.7%
pow298.7%
log1p-undefine98.8%
rem-exp-log98.8%
metadata-eval98.8%
+-commutative98.8%
log1p-undefine98.8%
Applied egg-rr99.9%
*-commutative99.9%
associate-+r+99.9%
metadata-eval99.9%
+-commutative99.9%
+-commutative99.9%
+-commutative99.9%
Simplified99.9%
+-commutative99.9%
+-commutative99.9%
pow299.9%
+-commutative99.9%
distribute-rgt-in99.9%
*-un-lft-identity99.9%
+-commutative99.9%
+-commutative99.9%
+-commutative99.9%
+-commutative99.9%
+-commutative99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (let* ((t_0 (/ (+ y x) (- x y))) (t_1 (+ 2.0 t_0))) (* (/ 1.0 t_1) (* t_0 t_1))))
double code(double x, double y) {
double t_0 = (y + x) / (x - y);
double t_1 = 2.0 + t_0;
return (1.0 / t_1) * (t_0 * t_1);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: t_1
t_0 = (y + x) / (x - y)
t_1 = 2.0d0 + t_0
code = (1.0d0 / t_1) * (t_0 * t_1)
end function
public static double code(double x, double y) {
double t_0 = (y + x) / (x - y);
double t_1 = 2.0 + t_0;
return (1.0 / t_1) * (t_0 * t_1);
}
def code(x, y): t_0 = (y + x) / (x - y) t_1 = 2.0 + t_0 return (1.0 / t_1) * (t_0 * t_1)
function code(x, y) t_0 = Float64(Float64(y + x) / Float64(x - y)) t_1 = Float64(2.0 + t_0) return Float64(Float64(1.0 / t_1) * Float64(t_0 * t_1)) end
function tmp = code(x, y) t_0 = (y + x) / (x - y); t_1 = 2.0 + t_0; tmp = (1.0 / t_1) * (t_0 * t_1); end
code[x_, y_] := Block[{t$95$0 = N[(N[(y + x), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(2.0 + t$95$0), $MachinePrecision]}, N[(N[(1.0 / t$95$1), $MachinePrecision] * N[(t$95$0 * t$95$1), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y + x}{x - y}\\
t_1 := 2 + t\_0\\
\frac{1}{t\_1} \cdot \left(t\_0 \cdot t\_1\right)
\end{array}
\end{array}
Initial program 99.9%
expm1-log1p-u98.8%
Applied egg-rr98.8%
expm1-define98.7%
flip--98.7%
div-inv98.7%
metadata-eval98.7%
sub-neg98.7%
pow298.7%
log1p-undefine98.8%
rem-exp-log98.8%
metadata-eval98.8%
+-commutative98.8%
log1p-undefine98.8%
Applied egg-rr99.9%
*-commutative99.9%
associate-+r+99.9%
metadata-eval99.9%
+-commutative99.9%
+-commutative99.9%
+-commutative99.9%
Simplified99.9%
+-commutative99.9%
+-commutative99.9%
+-commutative99.9%
pow299.9%
difference-of-sqr--199.9%
associate-+l+99.9%
+-commutative99.9%
metadata-eval99.9%
+-commutative99.9%
associate--l+99.9%
metadata-eval99.9%
+-rgt-identity99.9%
+-commutative99.9%
Applied egg-rr99.9%
*-commutative99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ 1.0 (* 2.0 (/ y x)))))
(if (<= x -1.02e+55)
t_0
(if (<= x -1.15e+41)
-1.0
(if (<= x -1.5e-18) 1.0 (if (<= x 1.8e-37) -1.0 t_0))))))
double code(double x, double y) {
double t_0 = 1.0 + (2.0 * (y / x));
double tmp;
if (x <= -1.02e+55) {
tmp = t_0;
} else if (x <= -1.15e+41) {
tmp = -1.0;
} else if (x <= -1.5e-18) {
tmp = 1.0;
} else if (x <= 1.8e-37) {
tmp = -1.0;
} 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 = 1.0d0 + (2.0d0 * (y / x))
if (x <= (-1.02d+55)) then
tmp = t_0
else if (x <= (-1.15d+41)) then
tmp = -1.0d0
else if (x <= (-1.5d-18)) then
tmp = 1.0d0
else if (x <= 1.8d-37) then
tmp = -1.0d0
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = 1.0 + (2.0 * (y / x));
double tmp;
if (x <= -1.02e+55) {
tmp = t_0;
} else if (x <= -1.15e+41) {
tmp = -1.0;
} else if (x <= -1.5e-18) {
tmp = 1.0;
} else if (x <= 1.8e-37) {
tmp = -1.0;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = 1.0 + (2.0 * (y / x)) tmp = 0 if x <= -1.02e+55: tmp = t_0 elif x <= -1.15e+41: tmp = -1.0 elif x <= -1.5e-18: tmp = 1.0 elif x <= 1.8e-37: tmp = -1.0 else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(1.0 + Float64(2.0 * Float64(y / x))) tmp = 0.0 if (x <= -1.02e+55) tmp = t_0; elseif (x <= -1.15e+41) tmp = -1.0; elseif (x <= -1.5e-18) tmp = 1.0; elseif (x <= 1.8e-37) tmp = -1.0; else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = 1.0 + (2.0 * (y / x)); tmp = 0.0; if (x <= -1.02e+55) tmp = t_0; elseif (x <= -1.15e+41) tmp = -1.0; elseif (x <= -1.5e-18) tmp = 1.0; elseif (x <= 1.8e-37) tmp = -1.0; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -1.02e+55], t$95$0, If[LessEqual[x, -1.15e+41], -1.0, If[LessEqual[x, -1.5e-18], 1.0, If[LessEqual[x, 1.8e-37], -1.0, t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 + 2 \cdot \frac{y}{x}\\
\mathbf{if}\;x \leq -1.02 \cdot 10^{+55}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq -1.15 \cdot 10^{+41}:\\
\;\;\;\;-1\\
\mathbf{elif}\;x \leq -1.5 \cdot 10^{-18}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 1.8 \cdot 10^{-37}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -1.02000000000000002e55 or 1.80000000000000004e-37 < x Initial program 99.9%
Taylor expanded in y around 0 79.4%
if -1.02000000000000002e55 < x < -1.1499999999999999e41 or -1.49999999999999991e-18 < x < 1.80000000000000004e-37Initial program 99.9%
Taylor expanded in x around 0 79.4%
if -1.1499999999999999e41 < x < -1.49999999999999991e-18Initial program 100.0%
Taylor expanded in x around inf 78.1%
Final simplification79.3%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ 1.0 (* 2.0 (/ y x)))))
(if (<= x -7.2e+55)
t_0
(if (<= x -1.2e+39)
-1.0
(if (<= x -1.65e-18)
1.0
(if (<= x 3.3e+71) (+ -1.0 (* -2.0 (/ x y))) t_0))))))
double code(double x, double y) {
double t_0 = 1.0 + (2.0 * (y / x));
double tmp;
if (x <= -7.2e+55) {
tmp = t_0;
} else if (x <= -1.2e+39) {
tmp = -1.0;
} else if (x <= -1.65e-18) {
tmp = 1.0;
} else if (x <= 3.3e+71) {
tmp = -1.0 + (-2.0 * (x / 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 = 1.0d0 + (2.0d0 * (y / x))
if (x <= (-7.2d+55)) then
tmp = t_0
else if (x <= (-1.2d+39)) then
tmp = -1.0d0
else if (x <= (-1.65d-18)) then
tmp = 1.0d0
else if (x <= 3.3d+71) then
tmp = (-1.0d0) + ((-2.0d0) * (x / y))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = 1.0 + (2.0 * (y / x));
double tmp;
if (x <= -7.2e+55) {
tmp = t_0;
} else if (x <= -1.2e+39) {
tmp = -1.0;
} else if (x <= -1.65e-18) {
tmp = 1.0;
} else if (x <= 3.3e+71) {
tmp = -1.0 + (-2.0 * (x / y));
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = 1.0 + (2.0 * (y / x)) tmp = 0 if x <= -7.2e+55: tmp = t_0 elif x <= -1.2e+39: tmp = -1.0 elif x <= -1.65e-18: tmp = 1.0 elif x <= 3.3e+71: tmp = -1.0 + (-2.0 * (x / y)) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(1.0 + Float64(2.0 * Float64(y / x))) tmp = 0.0 if (x <= -7.2e+55) tmp = t_0; elseif (x <= -1.2e+39) tmp = -1.0; elseif (x <= -1.65e-18) tmp = 1.0; elseif (x <= 3.3e+71) tmp = Float64(-1.0 + Float64(-2.0 * Float64(x / y))); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = 1.0 + (2.0 * (y / x)); tmp = 0.0; if (x <= -7.2e+55) tmp = t_0; elseif (x <= -1.2e+39) tmp = -1.0; elseif (x <= -1.65e-18) tmp = 1.0; elseif (x <= 3.3e+71) tmp = -1.0 + (-2.0 * (x / y)); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -7.2e+55], t$95$0, If[LessEqual[x, -1.2e+39], -1.0, If[LessEqual[x, -1.65e-18], 1.0, If[LessEqual[x, 3.3e+71], N[(-1.0 + N[(-2.0 * N[(x / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 + 2 \cdot \frac{y}{x}\\
\mathbf{if}\;x \leq -7.2 \cdot 10^{+55}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq -1.2 \cdot 10^{+39}:\\
\;\;\;\;-1\\
\mathbf{elif}\;x \leq -1.65 \cdot 10^{-18}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 3.3 \cdot 10^{+71}:\\
\;\;\;\;-1 + -2 \cdot \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -7.19999999999999975e55 or 3.2999999999999998e71 < x Initial program 99.9%
Taylor expanded in y around 0 85.6%
if -7.19999999999999975e55 < x < -1.2e39Initial program 100.0%
Taylor expanded in x around 0 100.0%
if -1.2e39 < x < -1.6500000000000001e-18Initial program 100.0%
Taylor expanded in x around inf 78.1%
if -1.6500000000000001e-18 < x < 3.2999999999999998e71Initial program 99.9%
Taylor expanded in x around 0 75.4%
Final simplification80.0%
(FPCore (x y)
:precision binary64
(if (<= x -5e+55)
1.0
(if (<= x -1e+39)
-1.0
(if (<= x -2e-18) 1.0 (if (<= x 7.5e+42) -1.0 1.0)))))
double code(double x, double y) {
double tmp;
if (x <= -5e+55) {
tmp = 1.0;
} else if (x <= -1e+39) {
tmp = -1.0;
} else if (x <= -2e-18) {
tmp = 1.0;
} else if (x <= 7.5e+42) {
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) :: tmp
if (x <= (-5d+55)) then
tmp = 1.0d0
else if (x <= (-1d+39)) then
tmp = -1.0d0
else if (x <= (-2d-18)) then
tmp = 1.0d0
else if (x <= 7.5d+42) then
tmp = -1.0d0
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -5e+55) {
tmp = 1.0;
} else if (x <= -1e+39) {
tmp = -1.0;
} else if (x <= -2e-18) {
tmp = 1.0;
} else if (x <= 7.5e+42) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -5e+55: tmp = 1.0 elif x <= -1e+39: tmp = -1.0 elif x <= -2e-18: tmp = 1.0 elif x <= 7.5e+42: tmp = -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -5e+55) tmp = 1.0; elseif (x <= -1e+39) tmp = -1.0; elseif (x <= -2e-18) tmp = 1.0; elseif (x <= 7.5e+42) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -5e+55) tmp = 1.0; elseif (x <= -1e+39) tmp = -1.0; elseif (x <= -2e-18) tmp = 1.0; elseif (x <= 7.5e+42) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -5e+55], 1.0, If[LessEqual[x, -1e+39], -1.0, If[LessEqual[x, -2e-18], 1.0, If[LessEqual[x, 7.5e+42], -1.0, 1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{+55}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq -1 \cdot 10^{+39}:\\
\;\;\;\;-1\\
\mathbf{elif}\;x \leq -2 \cdot 10^{-18}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 7.5 \cdot 10^{+42}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -5.00000000000000046e55 or -9.9999999999999994e38 < x < -2.0000000000000001e-18 or 7.50000000000000041e42 < x Initial program 99.9%
Taylor expanded in x around inf 81.6%
if -5.00000000000000046e55 < x < -9.9999999999999994e38 or -2.0000000000000001e-18 < x < 7.50000000000000041e42Initial program 99.9%
Taylor expanded in x around 0 76.5%
Final simplification78.8%
(FPCore (x y) :precision binary64 (/ (+ y x) (- x y)))
double code(double x, double y) {
return (y + x) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y + x) / (x - y)
end function
public static double code(double x, double y) {
return (y + x) / (x - y);
}
def code(x, y): return (y + x) / (x - y)
function code(x, y) return Float64(Float64(y + x) / Float64(x - y)) end
function tmp = code(x, y) tmp = (y + x) / (x - y); end
code[x_, y_] := N[(N[(y + x), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{y + x}{x - y}
\end{array}
Initial program 99.9%
Final simplification99.9%
(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 99.9%
Taylor expanded in x around 0 49.5%
Final simplification49.5%
(FPCore (x y) :precision binary64 (/ 1.0 (- (/ x (+ x y)) (/ y (+ x y)))))
double code(double x, double y) {
return 1.0 / ((x / (x + y)) - (y / (x + y)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 / ((x / (x + y)) - (y / (x + y)))
end function
public static double code(double x, double y) {
return 1.0 / ((x / (x + y)) - (y / (x + y)));
}
def code(x, y): return 1.0 / ((x / (x + y)) - (y / (x + y)))
function code(x, y) return Float64(1.0 / Float64(Float64(x / Float64(x + y)) - Float64(y / Float64(x + y)))) end
function tmp = code(x, y) tmp = 1.0 / ((x / (x + y)) - (y / (x + y))); end
code[x_, y_] := N[(1.0 / N[(N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision] - N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{x}{x + y} - \frac{y}{x + y}}
\end{array}
herbie shell --seed 2024096
(FPCore (x y)
:name "Linear.Projection:perspective from linear-1.19.1.3, A"
:precision binary64
:alt
(/ 1.0 (- (/ x (+ x y)) (/ y (+ x y))))
(/ (+ x y) (- x y)))