
(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 10 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 (/ (+ y (+ x (* y -2.0))) (+ y x)))
double code(double x, double y) {
return (y + (x + (y * -2.0))) / (y + x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y + (x + (y * (-2.0d0)))) / (y + x)
end function
public static double code(double x, double y) {
return (y + (x + (y * -2.0))) / (y + x);
}
def code(x, y): return (y + (x + (y * -2.0))) / (y + x)
function code(x, y) return Float64(Float64(y + Float64(x + Float64(y * -2.0))) / Float64(y + x)) end
function tmp = code(x, y) tmp = (y + (x + (y * -2.0))) / (y + x); end
code[x_, y_] := N[(N[(y + N[(x + N[(y * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(y + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{y + \left(x + y \cdot -2\right)}{y + x}
\end{array}
Initial program 100.0%
*-un-lft-identity100.0%
*-un-lft-identity100.0%
prod-diff100.0%
*-commutative100.0%
*-un-lft-identity100.0%
fma-neg100.0%
*-un-lft-identity100.0%
*-commutative100.0%
*-un-lft-identity100.0%
Applied egg-rr100.0%
fma-undefine100.0%
*-rgt-identity100.0%
associate-+r+100.0%
+-commutative100.0%
sub-neg100.0%
associate-+l+100.0%
neg-mul-1100.0%
neg-mul-1100.0%
distribute-rgt-out100.0%
metadata-eval100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= y -2.8e+20) (/ y (- (- y) x)) (if (<= y 2.85e-11) (/ x (+ y x)) (+ (* 2.0 (/ x y)) -1.0))))
double code(double x, double y) {
double tmp;
if (y <= -2.8e+20) {
tmp = y / (-y - x);
} else if (y <= 2.85e-11) {
tmp = x / (y + x);
} else {
tmp = (2.0 * (x / y)) + -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 <= (-2.8d+20)) then
tmp = y / (-y - x)
else if (y <= 2.85d-11) then
tmp = x / (y + x)
else
tmp = (2.0d0 * (x / y)) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -2.8e+20) {
tmp = y / (-y - x);
} else if (y <= 2.85e-11) {
tmp = x / (y + x);
} else {
tmp = (2.0 * (x / y)) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2.8e+20: tmp = y / (-y - x) elif y <= 2.85e-11: tmp = x / (y + x) else: tmp = (2.0 * (x / y)) + -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -2.8e+20) tmp = Float64(y / Float64(Float64(-y) - x)); elseif (y <= 2.85e-11) tmp = Float64(x / Float64(y + x)); else tmp = Float64(Float64(2.0 * Float64(x / y)) + -1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -2.8e+20) tmp = y / (-y - x); elseif (y <= 2.85e-11) tmp = x / (y + x); else tmp = (2.0 * (x / y)) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2.8e+20], N[(y / N[((-y) - x), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 2.85e-11], N[(x / N[(y + x), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.8 \cdot 10^{+20}:\\
\;\;\;\;\frac{y}{\left(-y\right) - x}\\
\mathbf{elif}\;y \leq 2.85 \cdot 10^{-11}:\\
\;\;\;\;\frac{x}{y + x}\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \frac{x}{y} + -1\\
\end{array}
\end{array}
if y < -2.8e20Initial program 100.0%
Taylor expanded in x around 0 79.3%
neg-mul-179.3%
Simplified79.3%
if -2.8e20 < y < 2.8499999999999999e-11Initial program 100.0%
Taylor expanded in x around inf 80.2%
if 2.8499999999999999e-11 < y Initial program 99.9%
Taylor expanded in x around 0 77.5%
Final simplification79.3%
(FPCore (x y) :precision binary64 (if (or (<= y -6.6e+96) (not (<= y 1.35e-13))) (/ (- x y) y) (/ x (+ y x))))
double code(double x, double y) {
double tmp;
if ((y <= -6.6e+96) || !(y <= 1.35e-13)) {
tmp = (x - y) / y;
} else {
tmp = x / (y + x);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-6.6d+96)) .or. (.not. (y <= 1.35d-13))) then
tmp = (x - y) / y
else
tmp = x / (y + x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -6.6e+96) || !(y <= 1.35e-13)) {
tmp = (x - y) / y;
} else {
tmp = x / (y + x);
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -6.6e+96) or not (y <= 1.35e-13): tmp = (x - y) / y else: tmp = x / (y + x) return tmp
function code(x, y) tmp = 0.0 if ((y <= -6.6e+96) || !(y <= 1.35e-13)) tmp = Float64(Float64(x - y) / y); else tmp = Float64(x / Float64(y + x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -6.6e+96) || ~((y <= 1.35e-13))) tmp = (x - y) / y; else tmp = x / (y + x); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -6.6e+96], N[Not[LessEqual[y, 1.35e-13]], $MachinePrecision]], N[(N[(x - y), $MachinePrecision] / y), $MachinePrecision], N[(x / N[(y + x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.6 \cdot 10^{+96} \lor \neg \left(y \leq 1.35 \cdot 10^{-13}\right):\\
\;\;\;\;\frac{x - y}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y + x}\\
\end{array}
\end{array}
if y < -6.59999999999999969e96 or 1.35000000000000005e-13 < y Initial program 99.9%
Taylor expanded in x around 0 80.6%
if -6.59999999999999969e96 < y < 1.35000000000000005e-13Initial program 100.0%
Taylor expanded in x around inf 77.8%
Final simplification79.1%
(FPCore (x y) :precision binary64 (if (<= y -8.8e+19) (/ y (- (- y) x)) (if (<= y 1.35e-9) (/ x (+ y x)) (/ (- x y) y))))
double code(double x, double y) {
double tmp;
if (y <= -8.8e+19) {
tmp = y / (-y - x);
} else if (y <= 1.35e-9) {
tmp = x / (y + x);
} else {
tmp = (x - 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 <= (-8.8d+19)) then
tmp = y / (-y - x)
else if (y <= 1.35d-9) then
tmp = x / (y + x)
else
tmp = (x - y) / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -8.8e+19) {
tmp = y / (-y - x);
} else if (y <= 1.35e-9) {
tmp = x / (y + x);
} else {
tmp = (x - y) / y;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -8.8e+19: tmp = y / (-y - x) elif y <= 1.35e-9: tmp = x / (y + x) else: tmp = (x - y) / y return tmp
function code(x, y) tmp = 0.0 if (y <= -8.8e+19) tmp = Float64(y / Float64(Float64(-y) - x)); elseif (y <= 1.35e-9) tmp = Float64(x / Float64(y + x)); else tmp = Float64(Float64(x - y) / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -8.8e+19) tmp = y / (-y - x); elseif (y <= 1.35e-9) tmp = x / (y + x); else tmp = (x - y) / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -8.8e+19], N[(y / N[((-y) - x), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.35e-9], N[(x / N[(y + x), $MachinePrecision]), $MachinePrecision], N[(N[(x - y), $MachinePrecision] / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -8.8 \cdot 10^{+19}:\\
\;\;\;\;\frac{y}{\left(-y\right) - x}\\
\mathbf{elif}\;y \leq 1.35 \cdot 10^{-9}:\\
\;\;\;\;\frac{x}{y + x}\\
\mathbf{else}:\\
\;\;\;\;\frac{x - y}{y}\\
\end{array}
\end{array}
if y < -8.8e19Initial program 100.0%
Taylor expanded in x around 0 79.3%
neg-mul-179.3%
Simplified79.3%
if -8.8e19 < y < 1.3500000000000001e-9Initial program 100.0%
Taylor expanded in x around inf 80.2%
if 1.3500000000000001e-9 < y Initial program 99.9%
Taylor expanded in x around 0 77.1%
Final simplification79.2%
(FPCore (x y) :precision binary64 (if (<= y -1.1e+97) -1.0 (if (<= y 5800000.0) (/ x (+ y x)) -1.0)))
double code(double x, double y) {
double tmp;
if (y <= -1.1e+97) {
tmp = -1.0;
} else if (y <= 5800000.0) {
tmp = x / (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 <= (-1.1d+97)) then
tmp = -1.0d0
else if (y <= 5800000.0d0) then
tmp = x / (y + x)
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -1.1e+97) {
tmp = -1.0;
} else if (y <= 5800000.0) {
tmp = x / (y + x);
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -1.1e+97: tmp = -1.0 elif y <= 5800000.0: tmp = x / (y + x) else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -1.1e+97) tmp = -1.0; elseif (y <= 5800000.0) tmp = Float64(x / Float64(y + x)); else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -1.1e+97) tmp = -1.0; elseif (y <= 5800000.0) tmp = x / (y + x); else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -1.1e+97], -1.0, If[LessEqual[y, 5800000.0], N[(x / N[(y + x), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.1 \cdot 10^{+97}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 5800000:\\
\;\;\;\;\frac{x}{y + x}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -1.1e97 or 5.8e6 < y Initial program 99.9%
Taylor expanded in x around 0 80.6%
if -1.1e97 < y < 5.8e6Initial program 100.0%
Taylor expanded in x around inf 77.4%
Final simplification78.9%
(FPCore (x y) :precision binary64 (if (<= y -6e+41) -1.0 (if (<= y 550000000.0) (- 1.0 (/ y x)) -1.0)))
double code(double x, double y) {
double tmp;
if (y <= -6e+41) {
tmp = -1.0;
} else if (y <= 550000000.0) {
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 <= (-6d+41)) then
tmp = -1.0d0
else if (y <= 550000000.0d0) 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 <= -6e+41) {
tmp = -1.0;
} else if (y <= 550000000.0) {
tmp = 1.0 - (y / x);
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -6e+41: tmp = -1.0 elif y <= 550000000.0: tmp = 1.0 - (y / x) else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -6e+41) tmp = -1.0; elseif (y <= 550000000.0) 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 <= -6e+41) tmp = -1.0; elseif (y <= 550000000.0) tmp = 1.0 - (y / x); else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -6e+41], -1.0, If[LessEqual[y, 550000000.0], N[(1.0 - N[(y / x), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -6 \cdot 10^{+41}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 550000000:\\
\;\;\;\;1 - \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -5.9999999999999997e41 or 5.5e8 < y Initial program 99.9%
Taylor expanded in x around 0 78.7%
if -5.9999999999999997e41 < y < 5.5e8Initial program 100.0%
Taylor expanded in x around inf 78.9%
Taylor expanded in x around inf 78.6%
mul-1-neg78.6%
unsub-neg78.6%
Simplified78.6%
(FPCore (x y) :precision binary64 (if (<= y -6.6e+96) -1.0 (if (<= y 2.25e-9) 1.0 -1.0)))
double code(double x, double y) {
double tmp;
if (y <= -6.6e+96) {
tmp = -1.0;
} else if (y <= 2.25e-9) {
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.6d+96)) then
tmp = -1.0d0
else if (y <= 2.25d-9) 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.6e+96) {
tmp = -1.0;
} else if (y <= 2.25e-9) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -6.6e+96: tmp = -1.0 elif y <= 2.25e-9: tmp = 1.0 else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -6.6e+96) tmp = -1.0; elseif (y <= 2.25e-9) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -6.6e+96) tmp = -1.0; elseif (y <= 2.25e-9) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -6.6e+96], -1.0, If[LessEqual[y, 2.25e-9], 1.0, -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.6 \cdot 10^{+96}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 2.25 \cdot 10^{-9}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -6.59999999999999969e96 or 2.24999999999999988e-9 < y Initial program 99.9%
Taylor expanded in x around 0 80.1%
if -6.59999999999999969e96 < y < 2.24999999999999988e-9Initial program 100.0%
Taylor expanded in x around inf 77.0%
(FPCore (x y) :precision binary64 (/ 1.0 (/ (+ y x) (- x y))))
double code(double x, double y) {
return 1.0 / ((y + x) / (x - y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 / ((y + x) / (x - y))
end function
public static double code(double x, double y) {
return 1.0 / ((y + x) / (x - y));
}
def code(x, y): return 1.0 / ((y + x) / (x - y))
function code(x, y) return Float64(1.0 / Float64(Float64(y + x) / Float64(x - y))) end
function tmp = code(x, y) tmp = 1.0 / ((y + x) / (x - y)); end
code[x_, y_] := N[(1.0 / N[(N[(y + x), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{y + x}{x - y}}
\end{array}
Initial program 100.0%
*-un-lft-identity100.0%
*-un-lft-identity100.0%
prod-diff100.0%
*-commutative100.0%
*-un-lft-identity100.0%
fma-neg100.0%
*-un-lft-identity100.0%
*-commutative100.0%
*-un-lft-identity100.0%
Applied egg-rr100.0%
fma-undefine100.0%
*-rgt-identity100.0%
associate-+r+100.0%
+-commutative100.0%
sub-neg100.0%
associate-+l+100.0%
neg-mul-1100.0%
neg-mul-1100.0%
distribute-rgt-out100.0%
metadata-eval100.0%
Simplified100.0%
clear-num100.0%
inv-pow100.0%
+-commutative100.0%
+-commutative100.0%
fma-define100.0%
Applied egg-rr100.0%
unpow-1100.0%
fma-undefine100.0%
associate-+r+100.0%
*-commutative100.0%
distribute-rgt1-in100.0%
metadata-eval100.0%
+-commutative100.0%
mul-1-neg100.0%
unsub-neg100.0%
Simplified100.0%
(FPCore (x y) :precision binary64 (/ (- x y) (+ y x)))
double code(double x, double y) {
return (x - y) / (y + x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x - y) / (y + x)
end function
public static double code(double x, double y) {
return (x - y) / (y + x);
}
def code(x, y): return (x - y) / (y + x)
function code(x, y) return Float64(Float64(x - y) / Float64(y + x)) end
function tmp = code(x, y) tmp = (x - y) / (y + x); end
code[x_, y_] := N[(N[(x - y), $MachinePrecision] / N[(y + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - y}{y + x}
\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.9%
(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 2024146
(FPCore (x y)
:name "Data.Colour.RGB:hslsv from colour-2.3.3, D"
:precision binary64
:alt
(! :herbie-platform default (- (/ x (+ x y)) (/ y (+ x y))))
(/ (- x y) (+ x y)))