
(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 6 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 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%
(FPCore (x y) :precision binary64 (if (or (<= x -3.6e-30) (not (<= x 5.5e+72))) (/ x (+ x y)) (/ y (- (- y) x))))
double code(double x, double y) {
double tmp;
if ((x <= -3.6e-30) || !(x <= 5.5e+72)) {
tmp = x / (x + y);
} else {
tmp = y / (-y - x);
}
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-30)) .or. (.not. (x <= 5.5d+72))) then
tmp = x / (x + y)
else
tmp = y / (-y - x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -3.6e-30) || !(x <= 5.5e+72)) {
tmp = x / (x + y);
} else {
tmp = y / (-y - x);
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -3.6e-30) or not (x <= 5.5e+72): tmp = x / (x + y) else: tmp = y / (-y - x) return tmp
function code(x, y) tmp = 0.0 if ((x <= -3.6e-30) || !(x <= 5.5e+72)) tmp = Float64(x / Float64(x + y)); else tmp = Float64(y / Float64(Float64(-y) - x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -3.6e-30) || ~((x <= 5.5e+72))) tmp = x / (x + y); else tmp = y / (-y - x); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -3.6e-30], N[Not[LessEqual[x, 5.5e+72]], $MachinePrecision]], N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(y / N[((-y) - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.6 \cdot 10^{-30} \lor \neg \left(x \leq 5.5 \cdot 10^{+72}\right):\\
\;\;\;\;\frac{x}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{y}{\left(-y\right) - x}\\
\end{array}
\end{array}
if x < -3.6000000000000003e-30 or 5.5e72 < x Initial program 100.0%
Taylor expanded in x around inf 83.6%
if -3.6000000000000003e-30 < x < 5.5e72Initial program 100.0%
Taylor expanded in x around 0 77.7%
neg-mul-177.7%
Simplified77.7%
Final simplification80.2%
(FPCore (x y) :precision binary64 (if (or (<= x -4.1e-30) (not (<= x 3.3e+92))) (/ x (+ x y)) (+ (/ x y) -1.0)))
double code(double x, double y) {
double tmp;
if ((x <= -4.1e-30) || !(x <= 3.3e+92)) {
tmp = x / (x + y);
} else {
tmp = (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 ((x <= (-4.1d-30)) .or. (.not. (x <= 3.3d+92))) then
tmp = x / (x + y)
else
tmp = (x / y) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -4.1e-30) || !(x <= 3.3e+92)) {
tmp = x / (x + y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -4.1e-30) or not (x <= 3.3e+92): tmp = x / (x + y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -4.1e-30) || !(x <= 3.3e+92)) tmp = Float64(x / Float64(x + y)); else tmp = Float64(Float64(x / y) + -1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -4.1e-30) || ~((x <= 3.3e+92))) tmp = x / (x + y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -4.1e-30], N[Not[LessEqual[x, 3.3e+92]], $MachinePrecision]], N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.1 \cdot 10^{-30} \lor \neg \left(x \leq 3.3 \cdot 10^{+92}\right):\\
\;\;\;\;\frac{x}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -4.1000000000000003e-30 or 3.29999999999999974e92 < x Initial program 100.0%
Taylor expanded in x around inf 84.1%
if -4.1000000000000003e-30 < x < 3.29999999999999974e92Initial program 100.0%
Taylor expanded in x around 0 77.3%
neg-mul-177.3%
Simplified77.3%
Taylor expanded in y around inf 77.3%
Final simplification80.1%
(FPCore (x y) :precision binary64 (if (<= x -2800000000.0) 1.0 (if (<= x 3.3e+92) (+ (/ x y) -1.0) 1.0)))
double code(double x, double y) {
double tmp;
if (x <= -2800000000.0) {
tmp = 1.0;
} else if (x <= 3.3e+92) {
tmp = (x / y) + -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 <= (-2800000000.0d0)) then
tmp = 1.0d0
else if (x <= 3.3d+92) then
tmp = (x / y) + (-1.0d0)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2800000000.0) {
tmp = 1.0;
} else if (x <= 3.3e+92) {
tmp = (x / y) + -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2800000000.0: tmp = 1.0 elif x <= 3.3e+92: tmp = (x / y) + -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -2800000000.0) tmp = 1.0; elseif (x <= 3.3e+92) tmp = Float64(Float64(x / y) + -1.0); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2800000000.0) tmp = 1.0; elseif (x <= 3.3e+92) tmp = (x / y) + -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2800000000.0], 1.0, If[LessEqual[x, 3.3e+92], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2800000000:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 3.3 \cdot 10^{+92}:\\
\;\;\;\;\frac{x}{y} + -1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -2.8e9 or 3.29999999999999974e92 < x Initial program 100.0%
Taylor expanded in x around inf 85.5%
if -2.8e9 < x < 3.29999999999999974e92Initial program 100.0%
Taylor expanded in x around 0 76.3%
neg-mul-176.3%
Simplified76.3%
Taylor expanded in y around inf 76.3%
Final simplification79.9%
(FPCore (x y) :precision binary64 (if (<= x -3.2e-37) 1.0 (if (<= x 3.3e+92) -1.0 1.0)))
double code(double x, double y) {
double tmp;
if (x <= -3.2e-37) {
tmp = 1.0;
} else if (x <= 3.3e+92) {
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 <= (-3.2d-37)) then
tmp = 1.0d0
else if (x <= 3.3d+92) 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 <= -3.2e-37) {
tmp = 1.0;
} else if (x <= 3.3e+92) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -3.2e-37: tmp = 1.0 elif x <= 3.3e+92: tmp = -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -3.2e-37) tmp = 1.0; elseif (x <= 3.3e+92) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -3.2e-37) tmp = 1.0; elseif (x <= 3.3e+92) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -3.2e-37], 1.0, If[LessEqual[x, 3.3e+92], -1.0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-37}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 3.3 \cdot 10^{+92}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -3.1999999999999999e-37 or 3.29999999999999974e92 < x Initial program 100.0%
Taylor expanded in x around inf 83.0%
if -3.1999999999999999e-37 < x < 3.29999999999999974e92Initial program 100.0%
Taylor expanded in x around 0 77.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 52.1%
(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 2024143
(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)))