
(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}
\\
\left(x + y\right) - x \cdot 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}
\\
\left(x + y\right) - x \cdot y
\end{array}
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (fma (- 1.0 x) y x))
assert(x < y);
double code(double x, double y) {
return fma((1.0 - x), y, x);
}
x, y = sort([x, y]) function code(x, y) return fma(Float64(1.0 - x), y, x) end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(N[(1.0 - x), $MachinePrecision] * y + x), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\mathsf{fma}\left(1 - x, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
cancel-sign-sub-invN/A
+-commutativeN/A
associate-+l+N/A
*-lft-identityN/A
distribute-rgt-inN/A
sub-negN/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y)
:precision binary64
(let* ((t_0 (- (+ x y) (* x y))))
(if (or (<= t_0 (- INFINITY)) (not (<= t_0 4e+297)))
(* (- y) x)
(fma 1.0 y x))))assert(x < y);
double code(double x, double y) {
double t_0 = (x + y) - (x * y);
double tmp;
if ((t_0 <= -((double) INFINITY)) || !(t_0 <= 4e+297)) {
tmp = -y * x;
} else {
tmp = fma(1.0, y, x);
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) t_0 = Float64(Float64(x + y) - Float64(x * y)) tmp = 0.0 if ((t_0 <= Float64(-Inf)) || !(t_0 <= 4e+297)) tmp = Float64(Float64(-y) * x); else tmp = fma(1.0, y, x); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_] := Block[{t$95$0 = N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, (-Infinity)], N[Not[LessEqual[t$95$0, 4e+297]], $MachinePrecision]], N[((-y) * x), $MachinePrecision], N[(1.0 * y + x), $MachinePrecision]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
t_0 := \left(x + y\right) - x \cdot y\\
\mathbf{if}\;t\_0 \leq -\infty \lor \neg \left(t\_0 \leq 4 \cdot 10^{+297}\right):\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -inf.0 or 4.0000000000000001e297 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around inf
Applied rewrites100.0%
if -inf.0 < (-.f64 (+.f64 x y) (*.f64 x y)) < 4.0000000000000001e297Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
cancel-sign-sub-invN/A
+-commutativeN/A
associate-+l+N/A
*-lft-identityN/A
distribute-rgt-inN/A
sub-negN/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites85.4%
Final simplification87.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= (- (+ x y) (* x y)) -1e-256) (fma (- y) x x) (- y (* y x))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (((x + y) - (x * y)) <= -1e-256) {
tmp = fma(-y, x, x);
} else {
tmp = y - (y * x);
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (Float64(Float64(x + y) - Float64(x * y)) <= -1e-256) tmp = fma(Float64(-y), x, x); else tmp = Float64(y - Float64(y * x)); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], -1e-256], N[((-y) * x + x), $MachinePrecision], N[(y - N[(y * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;\left(x + y\right) - x \cdot y \leq -1 \cdot 10^{-256}:\\
\;\;\;\;\mathsf{fma}\left(-y, x, x\right)\\
\mathbf{else}:\\
\;\;\;\;y - y \cdot x\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -9.99999999999999977e-257Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6460.3
Applied rewrites60.3%
Applied rewrites60.3%
if -9.99999999999999977e-257 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in y around inf
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6461.4
Applied rewrites61.4%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= (- (+ x y) (* x y)) -1e-256) (* (- 1.0 y) x) (- y (* y x))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (((x + y) - (x * y)) <= -1e-256) {
tmp = (1.0 - y) * x;
} else {
tmp = y - (y * x);
}
return tmp;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (((x + y) - (x * y)) <= (-1d-256)) then
tmp = (1.0d0 - y) * x
else
tmp = y - (y * x)
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (((x + y) - (x * y)) <= -1e-256) {
tmp = (1.0 - y) * x;
} else {
tmp = y - (y * x);
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if ((x + y) - (x * y)) <= -1e-256: tmp = (1.0 - y) * x else: tmp = y - (y * x) return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (Float64(Float64(x + y) - Float64(x * y)) <= -1e-256) tmp = Float64(Float64(1.0 - y) * x); else tmp = Float64(y - Float64(y * x)); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (((x + y) - (x * y)) <= -1e-256)
tmp = (1.0 - y) * x;
else
tmp = y - (y * x);
end
tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[N[(N[(x + y), $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], -1e-256], N[(N[(1.0 - y), $MachinePrecision] * x), $MachinePrecision], N[(y - N[(y * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;\left(x + y\right) - x \cdot y \leq -1 \cdot 10^{-256}:\\
\;\;\;\;\left(1 - y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;y - y \cdot x\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -9.99999999999999977e-257Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6460.3
Applied rewrites60.3%
if -9.99999999999999977e-257 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in y around inf
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6461.4
Applied rewrites61.4%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (or (<= x -480.0) (not (<= x 1.0))) (* (- 1.0 y) x) (fma 1.0 y x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if ((x <= -480.0) || !(x <= 1.0)) {
tmp = (1.0 - y) * x;
} else {
tmp = fma(1.0, y, x);
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if ((x <= -480.0) || !(x <= 1.0)) tmp = Float64(Float64(1.0 - y) * x); else tmp = fma(1.0, y, x); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[Or[LessEqual[x, -480.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(N[(1.0 - y), $MachinePrecision] * x), $MachinePrecision], N[(1.0 * y + x), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -480 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;\left(1 - y\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\end{array}
\end{array}
if x < -480 or 1 < x Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6498.4
Applied rewrites98.4%
if -480 < x < 1Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
cancel-sign-sub-invN/A
+-commutativeN/A
associate-+l+N/A
*-lft-identityN/A
distribute-rgt-inN/A
sub-negN/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites98.6%
Final simplification98.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (fma 1.0 y x))
assert(x < y);
double code(double x, double y) {
return fma(1.0, y, x);
}
x, y = sort([x, y]) function code(x, y) return fma(1.0, y, x) end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(1.0 * y + x), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\mathsf{fma}\left(1, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
distribute-lft-out--N/A
*-rgt-identityN/A
cancel-sign-sub-invN/A
+-commutativeN/A
associate-+l+N/A
*-lft-identityN/A
distribute-rgt-inN/A
sub-negN/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in x around 0
Applied rewrites73.6%
herbie shell --seed 2024317
(FPCore (x y)
:name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
:precision binary64
(- (+ x y) (* x y)))