
(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 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}
\\
\left(x + y\right) - x \cdot y
\end{array}
(FPCore (x y) :precision binary64 (fma (- 1.0 x) y x))
double code(double x, double y) {
return fma((1.0 - x), y, x);
}
function code(x, y) return fma(Float64(1.0 - x), y, x) end
code[x_, y_] := N[(N[(1.0 - x), $MachinePrecision] * y + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1 - x, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/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%
(FPCore (x y) :precision binary64 (let* ((t_0 (- (+ y x) (* y x))) (t_1 (* (- y) x))) (if (<= t_0 (- INFINITY)) t_1 (if (<= t_0 6e+300) (fma 1.0 y x) t_1))))
double code(double x, double y) {
double t_0 = (y + x) - (y * x);
double t_1 = -y * x;
double tmp;
if (t_0 <= -((double) INFINITY)) {
tmp = t_1;
} else if (t_0 <= 6e+300) {
tmp = fma(1.0, y, x);
} else {
tmp = t_1;
}
return tmp;
}
function code(x, y) t_0 = Float64(Float64(y + x) - Float64(y * x)) t_1 = Float64(Float64(-y) * x) tmp = 0.0 if (t_0 <= Float64(-Inf)) tmp = t_1; elseif (t_0 <= 6e+300) tmp = fma(1.0, y, x); else tmp = t_1; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[(y + x), $MachinePrecision] - N[(y * x), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[((-y) * x), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], t$95$1, If[LessEqual[t$95$0, 6e+300], N[(1.0 * y + x), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(y + x\right) - y \cdot x\\
t_1 := \left(-y\right) \cdot x\\
\mathbf{if}\;t\_0 \leq -\infty:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 6 \cdot 10^{+300}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -inf.0 or 6.00000000000000032e300 < (-.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 rewrites97.3%
if -inf.0 < (-.f64 (+.f64 x y) (*.f64 x y)) < 6.00000000000000032e300Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/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 rewrites87.9%
Final simplification89.2%
(FPCore (x y) :precision binary64 (if (<= (- (+ y x) (* y x)) -5e-265) (* 1.0 x) (* 1.0 y)))
double code(double x, double y) {
double tmp;
if (((y + x) - (y * x)) <= -5e-265) {
tmp = 1.0 * x;
} else {
tmp = 1.0 * y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (((y + x) - (y * x)) <= (-5d-265)) then
tmp = 1.0d0 * x
else
tmp = 1.0d0 * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (((y + x) - (y * x)) <= -5e-265) {
tmp = 1.0 * x;
} else {
tmp = 1.0 * y;
}
return tmp;
}
def code(x, y): tmp = 0 if ((y + x) - (y * x)) <= -5e-265: tmp = 1.0 * x else: tmp = 1.0 * y return tmp
function code(x, y) tmp = 0.0 if (Float64(Float64(y + x) - Float64(y * x)) <= -5e-265) tmp = Float64(1.0 * x); else tmp = Float64(1.0 * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (((y + x) - (y * x)) <= -5e-265) tmp = 1.0 * x; else tmp = 1.0 * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[(y + x), $MachinePrecision] - N[(y * x), $MachinePrecision]), $MachinePrecision], -5e-265], N[(1.0 * x), $MachinePrecision], N[(1.0 * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left(y + x\right) - y \cdot x \leq -5 \cdot 10^{-265}:\\
\;\;\;\;1 \cdot x\\
\mathbf{else}:\\
\;\;\;\;1 \cdot y\\
\end{array}
\end{array}
if (-.f64 (+.f64 x y) (*.f64 x y)) < -5.0000000000000001e-265Initial 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--.f6454.2
Applied rewrites54.2%
Taylor expanded in y around 0
Applied rewrites33.7%
if -5.0000000000000001e-265 < (-.f64 (+.f64 x y) (*.f64 x y)) Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6459.3
Applied rewrites59.3%
Taylor expanded in x around 0
Applied rewrites35.3%
Final simplification34.5%
(FPCore (x y) :precision binary64 (if (<= y -2.15e-16) (* (- 1.0 y) x) (if (<= y 6.8e-16) (fma 1.0 y x) (* (- 1.0 x) y))))
double code(double x, double y) {
double tmp;
if (y <= -2.15e-16) {
tmp = (1.0 - y) * x;
} else if (y <= 6.8e-16) {
tmp = fma(1.0, y, x);
} else {
tmp = (1.0 - x) * y;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -2.15e-16) tmp = Float64(Float64(1.0 - y) * x); elseif (y <= 6.8e-16) tmp = fma(1.0, y, x); else tmp = Float64(Float64(1.0 - x) * y); end return tmp end
code[x_, y_] := If[LessEqual[y, -2.15e-16], N[(N[(1.0 - y), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[y, 6.8e-16], N[(1.0 * y + x), $MachinePrecision], N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.15 \cdot 10^{-16}:\\
\;\;\;\;\left(1 - y\right) \cdot x\\
\mathbf{elif}\;y \leq 6.8 \cdot 10^{-16}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) \cdot y\\
\end{array}
\end{array}
if y < -2.1499999999999999e-16Initial 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--.f6449.6
Applied rewrites49.6%
if -2.1499999999999999e-16 < y < 6.8e-16Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/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 rewrites100.0%
if 6.8e-16 < y Initial program 100.0%
Taylor expanded in y 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%
(FPCore (x y) :precision binary64 (if (<= y -240000.0) (* (- y) x) (if (<= y 6.8e-16) (fma 1.0 y x) (* (- 1.0 x) y))))
double code(double x, double y) {
double tmp;
if (y <= -240000.0) {
tmp = -y * x;
} else if (y <= 6.8e-16) {
tmp = fma(1.0, y, x);
} else {
tmp = (1.0 - x) * y;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (y <= -240000.0) tmp = Float64(Float64(-y) * x); elseif (y <= 6.8e-16) tmp = fma(1.0, y, x); else tmp = Float64(Float64(1.0 - x) * y); end return tmp end
code[x_, y_] := If[LessEqual[y, -240000.0], N[((-y) * x), $MachinePrecision], If[LessEqual[y, 6.8e-16], N[(1.0 * y + x), $MachinePrecision], N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -240000:\\
\;\;\;\;\left(-y\right) \cdot x\\
\mathbf{elif}\;y \leq 6.8 \cdot 10^{-16}:\\
\;\;\;\;\mathsf{fma}\left(1, y, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) \cdot y\\
\end{array}
\end{array}
if y < -2.4e5Initial 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--.f6448.8
Applied rewrites48.8%
Taylor expanded in y around inf
Applied rewrites47.4%
if -2.4e5 < y < 6.8e-16Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/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.7%
if 6.8e-16 < y Initial program 100.0%
Taylor expanded in y 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%
(FPCore (x y) :precision binary64 (fma 1.0 y x))
double code(double x, double y) {
return fma(1.0, y, x);
}
function code(x, y) return fma(1.0, y, x) end
code[x_, y_] := N[(1.0 * y + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1, y, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in y around 0
+-commutativeN/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 rewrites76.5%
(FPCore (x y) :precision binary64 (* 1.0 y))
double code(double x, double y) {
return 1.0 * y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 * y
end function
public static double code(double x, double y) {
return 1.0 * y;
}
def code(x, y): return 1.0 * y
function code(x, y) return Float64(1.0 * y) end
function tmp = code(x, y) tmp = 1.0 * y; end
code[x_, y_] := N[(1.0 * y), $MachinePrecision]
\begin{array}{l}
\\
1 \cdot y
\end{array}
Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f6463.5
Applied rewrites63.5%
Taylor expanded in x around 0
Applied rewrites40.4%
herbie shell --seed 2024276
(FPCore (x y)
:name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
:precision binary64
(- (+ x y) (* x y)))