
(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 (- (/ x (+ x y)) (/ y (+ x y))))
double code(double x, double y) {
return (x / (x + y)) - (y / (x + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x / (x + y)) - (y / (x + y))
end function
public static double code(double x, double y) {
return (x / (x + y)) - (y / (x + y));
}
def code(x, y): return (x / (x + y)) - (y / (x + y))
function code(x, y) return Float64(Float64(x / Float64(x + y)) - Float64(y / Float64(x + y))) end
function tmp = code(x, y) tmp = (x / (x + y)) - (y / (x + y)); end
code[x_, y_] := N[(N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision] - N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{x + y} - \frac{y}{x + y}
\end{array}
Initial program 100.0%
div-sub100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (<= y -3.6e+18)
-1.0
(if (or (<= y 1.16e+15) (and (not (<= y 8.5e+55)) (<= y 2.2e+89)))
(- 1.0 (/ y x))
-1.0)))
double code(double x, double y) {
double tmp;
if (y <= -3.6e+18) {
tmp = -1.0;
} else if ((y <= 1.16e+15) || (!(y <= 8.5e+55) && (y <= 2.2e+89))) {
tmp = 1.0 - (y / x);
} 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 (y <= (-3.6d+18)) then
tmp = -1.0d0
else if ((y <= 1.16d+15) .or. (.not. (y <= 8.5d+55)) .and. (y <= 2.2d+89)) then
tmp = 1.0d0 - (y / x)
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -3.6e+18) {
tmp = -1.0;
} else if ((y <= 1.16e+15) || (!(y <= 8.5e+55) && (y <= 2.2e+89))) {
tmp = 1.0 - (y / x);
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -3.6e+18: tmp = -1.0 elif (y <= 1.16e+15) or (not (y <= 8.5e+55) and (y <= 2.2e+89)): tmp = 1.0 - (y / x) else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -3.6e+18) tmp = -1.0; elseif ((y <= 1.16e+15) || (!(y <= 8.5e+55) && (y <= 2.2e+89))) tmp = Float64(1.0 - Float64(y / x)); else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -3.6e+18) tmp = -1.0; elseif ((y <= 1.16e+15) || (~((y <= 8.5e+55)) && (y <= 2.2e+89))) tmp = 1.0 - (y / x); else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -3.6e+18], -1.0, If[Or[LessEqual[y, 1.16e+15], And[N[Not[LessEqual[y, 8.5e+55]], $MachinePrecision], LessEqual[y, 2.2e+89]]], N[(1.0 - N[(y / x), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.6 \cdot 10^{+18}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 1.16 \cdot 10^{+15} \lor \neg \left(y \leq 8.5 \cdot 10^{+55}\right) \land y \leq 2.2 \cdot 10^{+89}:\\
\;\;\;\;1 - \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -3.6e18 or 1.16e15 < y < 8.50000000000000002e55 or 2.2e89 < y Initial program 100.0%
Taylor expanded in x around 0 84.5%
if -3.6e18 < y < 1.16e15 or 8.50000000000000002e55 < y < 2.2e89Initial program 100.0%
div-sub100.0%
Applied egg-rr100.0%
frac-sub62.7%
associate-/r*63.2%
*-commutative63.2%
Applied egg-rr63.2%
Taylor expanded in x around inf 83.2%
Taylor expanded in x around inf 83.3%
mul-1-neg83.3%
unsub-neg83.3%
Simplified83.3%
Final simplification83.9%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ (/ x y) -1.0)) (t_1 (- 1.0 (/ y x))))
(if (<= y -2.9e+18)
t_0
(if (<= y 7.5e+16)
t_1
(if (<= y 8.8e+58) -1.0 (if (<= y 3.4e+89) t_1 t_0))))))
double code(double x, double y) {
double t_0 = (x / y) + -1.0;
double t_1 = 1.0 - (y / x);
double tmp;
if (y <= -2.9e+18) {
tmp = t_0;
} else if (y <= 7.5e+16) {
tmp = t_1;
} else if (y <= 8.8e+58) {
tmp = -1.0;
} else if (y <= 3.4e+89) {
tmp = t_1;
} 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) :: t_1
real(8) :: tmp
t_0 = (x / y) + (-1.0d0)
t_1 = 1.0d0 - (y / x)
if (y <= (-2.9d+18)) then
tmp = t_0
else if (y <= 7.5d+16) then
tmp = t_1
else if (y <= 8.8d+58) then
tmp = -1.0d0
else if (y <= 3.4d+89) then
tmp = t_1
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (x / y) + -1.0;
double t_1 = 1.0 - (y / x);
double tmp;
if (y <= -2.9e+18) {
tmp = t_0;
} else if (y <= 7.5e+16) {
tmp = t_1;
} else if (y <= 8.8e+58) {
tmp = -1.0;
} else if (y <= 3.4e+89) {
tmp = t_1;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = (x / y) + -1.0 t_1 = 1.0 - (y / x) tmp = 0 if y <= -2.9e+18: tmp = t_0 elif y <= 7.5e+16: tmp = t_1 elif y <= 8.8e+58: tmp = -1.0 elif y <= 3.4e+89: tmp = t_1 else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(x / y) + -1.0) t_1 = Float64(1.0 - Float64(y / x)) tmp = 0.0 if (y <= -2.9e+18) tmp = t_0; elseif (y <= 7.5e+16) tmp = t_1; elseif (y <= 8.8e+58) tmp = -1.0; elseif (y <= 3.4e+89) tmp = t_1; else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = (x / y) + -1.0; t_1 = 1.0 - (y / x); tmp = 0.0; if (y <= -2.9e+18) tmp = t_0; elseif (y <= 7.5e+16) tmp = t_1; elseif (y <= 8.8e+58) tmp = -1.0; elseif (y <= 3.4e+89) tmp = t_1; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 - N[(y / x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -2.9e+18], t$95$0, If[LessEqual[y, 7.5e+16], t$95$1, If[LessEqual[y, 8.8e+58], -1.0, If[LessEqual[y, 3.4e+89], t$95$1, t$95$0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{y} + -1\\
t_1 := 1 - \frac{y}{x}\\
\mathbf{if}\;y \leq -2.9 \cdot 10^{+18}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 7.5 \cdot 10^{+16}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq 8.8 \cdot 10^{+58}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 3.4 \cdot 10^{+89}:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -2.9e18 or 3.4000000000000002e89 < y Initial program 100.0%
div-sub100.0%
Applied egg-rr100.0%
frac-sub23.5%
associate-/r*25.5%
*-commutative25.5%
Applied egg-rr25.5%
Taylor expanded in x around 0 84.8%
neg-mul-184.8%
Simplified84.8%
Taylor expanded in y around inf 84.9%
if -2.9e18 < y < 7.5e16 or 8.8000000000000003e58 < y < 3.4000000000000002e89Initial program 100.0%
div-sub100.0%
Applied egg-rr100.0%
frac-sub62.7%
associate-/r*63.2%
*-commutative63.2%
Applied egg-rr63.2%
Taylor expanded in x around inf 83.2%
Taylor expanded in x around inf 83.3%
mul-1-neg83.3%
unsub-neg83.3%
Simplified83.3%
if 7.5e16 < y < 8.8000000000000003e58Initial program 100.0%
Taylor expanded in x around 0 85.9%
Final simplification84.1%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ (/ x y) -1.0)))
(if (<= y -1.1e+17)
t_0
(if (<= y 1.9e+32)
(/ x (+ x y))
(if (<= y 4.6e+55) -1.0 (if (<= y 2.1e+89) (- 1.0 (/ y x)) t_0))))))
double code(double x, double y) {
double t_0 = (x / y) + -1.0;
double tmp;
if (y <= -1.1e+17) {
tmp = t_0;
} else if (y <= 1.9e+32) {
tmp = x / (x + y);
} else if (y <= 4.6e+55) {
tmp = -1.0;
} else if (y <= 2.1e+89) {
tmp = 1.0 - (y / x);
} 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 = (x / y) + (-1.0d0)
if (y <= (-1.1d+17)) then
tmp = t_0
else if (y <= 1.9d+32) then
tmp = x / (x + y)
else if (y <= 4.6d+55) then
tmp = -1.0d0
else if (y <= 2.1d+89) then
tmp = 1.0d0 - (y / x)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (x / y) + -1.0;
double tmp;
if (y <= -1.1e+17) {
tmp = t_0;
} else if (y <= 1.9e+32) {
tmp = x / (x + y);
} else if (y <= 4.6e+55) {
tmp = -1.0;
} else if (y <= 2.1e+89) {
tmp = 1.0 - (y / x);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = (x / y) + -1.0 tmp = 0 if y <= -1.1e+17: tmp = t_0 elif y <= 1.9e+32: tmp = x / (x + y) elif y <= 4.6e+55: tmp = -1.0 elif y <= 2.1e+89: tmp = 1.0 - (y / x) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(x / y) + -1.0) tmp = 0.0 if (y <= -1.1e+17) tmp = t_0; elseif (y <= 1.9e+32) tmp = Float64(x / Float64(x + y)); elseif (y <= 4.6e+55) tmp = -1.0; elseif (y <= 2.1e+89) tmp = Float64(1.0 - Float64(y / x)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = (x / y) + -1.0; tmp = 0.0; if (y <= -1.1e+17) tmp = t_0; elseif (y <= 1.9e+32) tmp = x / (x + y); elseif (y <= 4.6e+55) tmp = -1.0; elseif (y <= 2.1e+89) tmp = 1.0 - (y / x); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[y, -1.1e+17], t$95$0, If[LessEqual[y, 1.9e+32], N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 4.6e+55], -1.0, If[LessEqual[y, 2.1e+89], N[(1.0 - N[(y / x), $MachinePrecision]), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{y} + -1\\
\mathbf{if}\;y \leq -1.1 \cdot 10^{+17}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 1.9 \cdot 10^{+32}:\\
\;\;\;\;\frac{x}{x + y}\\
\mathbf{elif}\;y \leq 4.6 \cdot 10^{+55}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 2.1 \cdot 10^{+89}:\\
\;\;\;\;1 - \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1.1e17 or 2.09999999999999986e89 < y Initial program 100.0%
div-sub100.0%
Applied egg-rr100.0%
frac-sub23.5%
associate-/r*25.5%
*-commutative25.5%
Applied egg-rr25.5%
Taylor expanded in x around 0 84.8%
neg-mul-184.8%
Simplified84.8%
Taylor expanded in y around inf 84.9%
if -1.1e17 < y < 1.9000000000000002e32Initial program 100.0%
div-sub100.0%
Applied egg-rr100.0%
frac-sub65.1%
associate-/r*65.6%
*-commutative65.6%
Applied egg-rr65.6%
Taylor expanded in x around inf 82.6%
if 1.9000000000000002e32 < y < 4.59999999999999975e55Initial program 100.0%
Taylor expanded in x around 0 100.0%
if 4.59999999999999975e55 < y < 2.09999999999999986e89Initial program 99.8%
div-sub100.0%
Applied egg-rr100.0%
frac-sub14.3%
associate-/r*17.0%
*-commutative17.0%
Applied egg-rr17.0%
Taylor expanded in x around inf 85.9%
Taylor expanded in x around inf 88.0%
mul-1-neg88.0%
unsub-neg88.0%
Simplified88.0%
Final simplification84.1%
(FPCore (x y)
:precision binary64
(if (<= y -6.8e+19)
-1.0
(if (<= y 5.8e+14)
1.0
(if (<= y 1e+55) -1.0 (if (<= y 2.5e+89) 1.0 -1.0)))))
double code(double x, double y) {
double tmp;
if (y <= -6.8e+19) {
tmp = -1.0;
} else if (y <= 5.8e+14) {
tmp = 1.0;
} else if (y <= 1e+55) {
tmp = -1.0;
} else if (y <= 2.5e+89) {
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 (y <= (-6.8d+19)) then
tmp = -1.0d0
else if (y <= 5.8d+14) then
tmp = 1.0d0
else if (y <= 1d+55) then
tmp = -1.0d0
else if (y <= 2.5d+89) 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 (y <= -6.8e+19) {
tmp = -1.0;
} else if (y <= 5.8e+14) {
tmp = 1.0;
} else if (y <= 1e+55) {
tmp = -1.0;
} else if (y <= 2.5e+89) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -6.8e+19: tmp = -1.0 elif y <= 5.8e+14: tmp = 1.0 elif y <= 1e+55: tmp = -1.0 elif y <= 2.5e+89: tmp = 1.0 else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -6.8e+19) tmp = -1.0; elseif (y <= 5.8e+14) tmp = 1.0; elseif (y <= 1e+55) tmp = -1.0; elseif (y <= 2.5e+89) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -6.8e+19) tmp = -1.0; elseif (y <= 5.8e+14) tmp = 1.0; elseif (y <= 1e+55) tmp = -1.0; elseif (y <= 2.5e+89) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -6.8e+19], -1.0, If[LessEqual[y, 5.8e+14], 1.0, If[LessEqual[y, 1e+55], -1.0, If[LessEqual[y, 2.5e+89], 1.0, -1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.8 \cdot 10^{+19}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 5.8 \cdot 10^{+14}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 10^{+55}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 2.5 \cdot 10^{+89}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -6.8e19 or 5.8e14 < y < 1.00000000000000001e55 or 2.49999999999999992e89 < y Initial program 100.0%
Taylor expanded in x around 0 84.5%
if -6.8e19 < y < 5.8e14 or 1.00000000000000001e55 < y < 2.49999999999999992e89Initial program 100.0%
Taylor expanded in x around inf 82.8%
Final simplification83.6%
(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}
Initial program 100.0%
Final simplification100.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 100.0%
Taylor expanded in x around 0 49.2%
Final simplification49.2%
(FPCore (x y) :precision binary64 (- (/ x (+ x y)) (/ y (+ x y))))
double code(double x, double y) {
return (x / (x + y)) - (y / (x + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x / (x + y)) - (y / (x + y))
end function
public static double code(double x, double y) {
return (x / (x + y)) - (y / (x + y));
}
def code(x, y): return (x / (x + y)) - (y / (x + y))
function code(x, y) return Float64(Float64(x / Float64(x + y)) - Float64(y / Float64(x + y))) end
function tmp = code(x, y) tmp = (x / (x + y)) - (y / (x + y)); end
code[x_, y_] := N[(N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision] - N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{x + y} - \frac{y}{x + y}
\end{array}
herbie shell --seed 2024044
(FPCore (x y)
:name "Data.Colour.RGB:hslsv from colour-2.3.3, D"
:precision binary64
:herbie-target
(- (/ x (+ x y)) (/ y (+ x y)))
(/ (- x y) (+ x y)))