
(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) (+ 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 99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (<= (/ (- x y) (+ y x)) -0.5) (fma (/ 2.0 y) x -1.0) (fma (/ -2.0 x) y 1.0)))
double code(double x, double y) {
double tmp;
if (((x - y) / (y + x)) <= -0.5) {
tmp = fma((2.0 / y), x, -1.0);
} else {
tmp = fma((-2.0 / x), y, 1.0);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(Float64(x - y) / Float64(y + x)) <= -0.5) tmp = fma(Float64(2.0 / y), x, -1.0); else tmp = fma(Float64(-2.0 / x), y, 1.0); end return tmp end
code[x_, y_] := If[LessEqual[N[(N[(x - y), $MachinePrecision] / N[(y + x), $MachinePrecision]), $MachinePrecision], -0.5], N[(N[(2.0 / y), $MachinePrecision] * x + -1.0), $MachinePrecision], N[(N[(-2.0 / x), $MachinePrecision] * y + 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x - y}{y + x} \leq -0.5:\\
\;\;\;\;\mathsf{fma}\left(\frac{2}{y}, x, -1\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{-2}{x}, y, 1\right)\\
\end{array}
\end{array}
if (/.f64 (-.f64 x y) (+.f64 x y)) < -0.5Initial program 100.0%
Taylor expanded in x around 0
sub-negN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
metadata-evalN/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6499.0
Applied rewrites99.0%
if -0.5 < (/.f64 (-.f64 x y) (+.f64 x y)) Initial program 100.0%
Taylor expanded in x around inf
sub-negN/A
mul-1-negN/A
+-commutativeN/A
+-commutativeN/A
associate-+r+N/A
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
associate-*r/N/A
*-commutativeN/A
associate-/l*N/A
distribute-rgt-outN/A
remove-double-negN/A
distribute-neg-outN/A
mul-1-negN/A
sub-negN/A
mul-1-negN/A
Applied rewrites99.0%
Final simplification99.0%
(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 2024230
(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)))