
(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 x (- 1.0 y) y))
double code(double x, double y) {
return fma(x, (1.0 - y), y);
}
function code(x, y) return fma(x, Float64(1.0 - y), y) end
code[x_, y_] := N[(x * N[(1.0 - y), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, 1 - y, y\right)
\end{array}
Initial program 100.0%
+-commutative100.0%
*-commutative100.0%
associate--l+100.0%
+-commutative100.0%
*-lft-identity100.0%
metadata-eval100.0%
distribute-rgt-out--100.0%
fma-define100.0%
metadata-eval100.0%
Simplified100.0%
(FPCore (x y)
:precision binary64
(let* ((t_0 (* x (- y))))
(if (<= y -1.0)
t_0
(if (<= y 8e-29)
x
(if (or (<= y 1.7e+142) (and (not (<= y 3.4e+184)) (<= y 5.5e+230)))
y
t_0)))))
double code(double x, double y) {
double t_0 = x * -y;
double tmp;
if (y <= -1.0) {
tmp = t_0;
} else if (y <= 8e-29) {
tmp = x;
} else if ((y <= 1.7e+142) || (!(y <= 3.4e+184) && (y <= 5.5e+230))) {
tmp = y;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = x * -y
if (y <= (-1.0d0)) then
tmp = t_0
else if (y <= 8d-29) then
tmp = x
else if ((y <= 1.7d+142) .or. (.not. (y <= 3.4d+184)) .and. (y <= 5.5d+230)) then
tmp = y
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = x * -y;
double tmp;
if (y <= -1.0) {
tmp = t_0;
} else if (y <= 8e-29) {
tmp = x;
} else if ((y <= 1.7e+142) || (!(y <= 3.4e+184) && (y <= 5.5e+230))) {
tmp = y;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = x * -y tmp = 0 if y <= -1.0: tmp = t_0 elif y <= 8e-29: tmp = x elif (y <= 1.7e+142) or (not (y <= 3.4e+184) and (y <= 5.5e+230)): tmp = y else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(x * Float64(-y)) tmp = 0.0 if (y <= -1.0) tmp = t_0; elseif (y <= 8e-29) tmp = x; elseif ((y <= 1.7e+142) || (!(y <= 3.4e+184) && (y <= 5.5e+230))) tmp = y; else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = x * -y; tmp = 0.0; if (y <= -1.0) tmp = t_0; elseif (y <= 8e-29) tmp = x; elseif ((y <= 1.7e+142) || (~((y <= 3.4e+184)) && (y <= 5.5e+230))) tmp = y; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(x * (-y)), $MachinePrecision]}, If[LessEqual[y, -1.0], t$95$0, If[LessEqual[y, 8e-29], x, If[Or[LessEqual[y, 1.7e+142], And[N[Not[LessEqual[y, 3.4e+184]], $MachinePrecision], LessEqual[y, 5.5e+230]]], y, t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(-y\right)\\
\mathbf{if}\;y \leq -1:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 8 \cdot 10^{-29}:\\
\;\;\;\;x\\
\mathbf{elif}\;y \leq 1.7 \cdot 10^{+142} \lor \neg \left(y \leq 3.4 \cdot 10^{+184}\right) \land y \leq 5.5 \cdot 10^{+230}:\\
\;\;\;\;y\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1 or 1.6999999999999999e142 < y < 3.4000000000000002e184 or 5.49999999999999979e230 < y Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around inf 58.7%
Taylor expanded in y around inf 58.2%
associate-*r*58.2%
neg-mul-158.2%
*-commutative58.2%
Simplified58.2%
if -1 < y < 7.99999999999999955e-29Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in y around 0 71.7%
if 7.99999999999999955e-29 < y < 1.6999999999999999e142 or 3.4000000000000002e184 < y < 5.49999999999999979e230Initial program 99.9%
associate--l+99.9%
+-commutative99.9%
remove-double-neg99.9%
unsub-neg99.9%
cancel-sign-sub-inv99.9%
associate--l+99.9%
neg-mul-199.9%
cancel-sign-sub-inv99.9%
distribute-lft-neg-out99.9%
distribute-rgt-neg-out99.9%
*-commutative99.9%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 53.4%
Final simplification63.1%
(FPCore (x y) :precision binary64 (if (or (<= x -1.7e-155) (not (<= x 0.058))) (* x (- 1.0 y)) y))
double code(double x, double y) {
double tmp;
if ((x <= -1.7e-155) || !(x <= 0.058)) {
tmp = x * (1.0 - y);
} else {
tmp = y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-1.7d-155)) .or. (.not. (x <= 0.058d0))) then
tmp = x * (1.0d0 - y)
else
tmp = y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -1.7e-155) || !(x <= 0.058)) {
tmp = x * (1.0 - y);
} else {
tmp = y;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.7e-155) or not (x <= 0.058): tmp = x * (1.0 - y) else: tmp = y return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.7e-155) || !(x <= 0.058)) tmp = Float64(x * Float64(1.0 - y)); else tmp = y; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.7e-155) || ~((x <= 0.058))) tmp = x * (1.0 - y); else tmp = y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.7e-155], N[Not[LessEqual[x, 0.058]], $MachinePrecision]], N[(x * N[(1.0 - y), $MachinePrecision]), $MachinePrecision], y]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.7 \cdot 10^{-155} \lor \neg \left(x \leq 0.058\right):\\
\;\;\;\;x \cdot \left(1 - y\right)\\
\mathbf{else}:\\
\;\;\;\;y\\
\end{array}
\end{array}
if x < -1.7e-155 or 0.0580000000000000029 < x Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around inf 89.9%
if -1.7e-155 < x < 0.0580000000000000029Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 92.1%
Final simplification90.6%
(FPCore (x y) :precision binary64 (if (<= x -8e-156) (* x (- 1.0 y)) (* y (- 1.0 x))))
double code(double x, double y) {
double tmp;
if (x <= -8e-156) {
tmp = x * (1.0 - y);
} else {
tmp = y * (1.0 - x);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-8d-156)) then
tmp = x * (1.0d0 - y)
else
tmp = y * (1.0d0 - x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -8e-156) {
tmp = x * (1.0 - y);
} else {
tmp = y * (1.0 - x);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -8e-156: tmp = x * (1.0 - y) else: tmp = y * (1.0 - x) return tmp
function code(x, y) tmp = 0.0 if (x <= -8e-156) tmp = Float64(x * Float64(1.0 - y)); else tmp = Float64(y * Float64(1.0 - x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -8e-156) tmp = x * (1.0 - y); else tmp = y * (1.0 - x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -8e-156], N[(x * N[(1.0 - y), $MachinePrecision]), $MachinePrecision], N[(y * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8 \cdot 10^{-156}:\\
\;\;\;\;x \cdot \left(1 - y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 - x\right)\\
\end{array}
\end{array}
if x < -8.00000000000000032e-156Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around inf 84.4%
if -8.00000000000000032e-156 < x Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in y around inf 75.0%
neg-mul-175.0%
sub-neg75.0%
Simplified75.0%
(FPCore (x y) :precision binary64 (+ y (* x (- 1.0 y))))
double code(double x, double y) {
return y + (x * (1.0 - y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y + (x * (1.0d0 - y))
end function
public static double code(double x, double y) {
return y + (x * (1.0 - y));
}
def code(x, y): return y + (x * (1.0 - y))
function code(x, y) return Float64(y + Float64(x * Float64(1.0 - y))) end
function tmp = code(x, y) tmp = y + (x * (1.0 - y)); end
code[x_, y_] := N[(y + N[(x * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y + x \cdot \left(1 - y\right)
\end{array}
Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
(FPCore (x y) :precision binary64 (if (<= y 6e-29) x y))
double code(double x, double y) {
double tmp;
if (y <= 6e-29) {
tmp = x;
} else {
tmp = y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 6d-29) then
tmp = x
else
tmp = y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 6e-29) {
tmp = x;
} else {
tmp = y;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 6e-29: tmp = x else: tmp = y return tmp
function code(x, y) tmp = 0.0 if (y <= 6e-29) tmp = x; else tmp = y; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 6e-29) tmp = x; else tmp = y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 6e-29], x, y]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6 \cdot 10^{-29}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y\\
\end{array}
\end{array}
if y < 6.0000000000000005e-29Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in y around 0 44.2%
if 6.0000000000000005e-29 < y Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 47.8%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 100.0%
associate--l+100.0%
+-commutative100.0%
remove-double-neg100.0%
unsub-neg100.0%
cancel-sign-sub-inv100.0%
associate--l+100.0%
neg-mul-1100.0%
cancel-sign-sub-inv100.0%
distribute-lft-neg-out100.0%
distribute-rgt-neg-out100.0%
*-commutative100.0%
distribute-rgt-out100.0%
+-commutative100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in y around 0 33.5%
herbie shell --seed 2024107
(FPCore (x y)
:name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
:precision binary64
(- (+ x y) (* x y)))