
(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 5 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 (+ 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%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (or (<= x -3e+257)
(and (not (<= x -6.6e+190))
(or (<= x -1.05e+135) (not (<= x 16000.0)))))
(* y (- x))
(+ y x)))
double code(double x, double y) {
double tmp;
if ((x <= -3e+257) || (!(x <= -6.6e+190) && ((x <= -1.05e+135) || !(x <= 16000.0)))) {
tmp = y * -x;
} else {
tmp = 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 <= (-3d+257)) .or. (.not. (x <= (-6.6d+190))) .and. (x <= (-1.05d+135)) .or. (.not. (x <= 16000.0d0))) then
tmp = y * -x
else
tmp = y + x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -3e+257) || (!(x <= -6.6e+190) && ((x <= -1.05e+135) || !(x <= 16000.0)))) {
tmp = y * -x;
} else {
tmp = y + x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -3e+257) or (not (x <= -6.6e+190) and ((x <= -1.05e+135) or not (x <= 16000.0))): tmp = y * -x else: tmp = y + x return tmp
function code(x, y) tmp = 0.0 if ((x <= -3e+257) || (!(x <= -6.6e+190) && ((x <= -1.05e+135) || !(x <= 16000.0)))) tmp = Float64(y * Float64(-x)); else tmp = Float64(y + x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -3e+257) || (~((x <= -6.6e+190)) && ((x <= -1.05e+135) || ~((x <= 16000.0))))) tmp = y * -x; else tmp = y + x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -3e+257], And[N[Not[LessEqual[x, -6.6e+190]], $MachinePrecision], Or[LessEqual[x, -1.05e+135], N[Not[LessEqual[x, 16000.0]], $MachinePrecision]]]], N[(y * (-x)), $MachinePrecision], N[(y + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3 \cdot 10^{+257} \lor \neg \left(x \leq -6.6 \cdot 10^{+190}\right) \land \left(x \leq -1.05 \cdot 10^{+135} \lor \neg \left(x \leq 16000\right)\right):\\
\;\;\;\;y \cdot \left(-x\right)\\
\mathbf{else}:\\
\;\;\;\;y + x\\
\end{array}
\end{array}
if x < -3e257 or -6.6e190 < x < -1.05000000000000005e135 or 16000 < 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 60.0%
mul-1-neg60.0%
distribute-rgt-neg-out60.0%
Simplified60.0%
Taylor expanded in x around inf 59.3%
associate-*r*59.3%
neg-mul-159.3%
Simplified59.3%
if -3e257 < x < -6.6e190 or -1.05000000000000005e135 < x < 16000Initial 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 91.9%
Final simplification80.8%
(FPCore (x y) :precision binary64 (if (<= y -1.4e+14) (* y (- x)) (if (<= y 0.17) (+ y x) (- y (* y x)))))
double code(double x, double y) {
double tmp;
if (y <= -1.4e+14) {
tmp = y * -x;
} else if (y <= 0.17) {
tmp = y + x;
} 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 (y <= (-1.4d+14)) then
tmp = y * -x
else if (y <= 0.17d0) then
tmp = y + x
else
tmp = y - (y * x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -1.4e+14) {
tmp = y * -x;
} else if (y <= 0.17) {
tmp = y + x;
} else {
tmp = y - (y * x);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -1.4e+14: tmp = y * -x elif y <= 0.17: tmp = y + x else: tmp = y - (y * x) return tmp
function code(x, y) tmp = 0.0 if (y <= -1.4e+14) tmp = Float64(y * Float64(-x)); elseif (y <= 0.17) tmp = Float64(y + x); else tmp = Float64(y - Float64(y * x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -1.4e+14) tmp = y * -x; elseif (y <= 0.17) tmp = y + x; else tmp = y - (y * x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -1.4e+14], N[(y * (-x)), $MachinePrecision], If[LessEqual[y, 0.17], N[(y + x), $MachinePrecision], N[(y - N[(y * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.4 \cdot 10^{+14}:\\
\;\;\;\;y \cdot \left(-x\right)\\
\mathbf{elif}\;y \leq 0.17:\\
\;\;\;\;y + x\\
\mathbf{else}:\\
\;\;\;\;y - y \cdot x\\
\end{array}
\end{array}
if y < -1.4e14Initial 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 99.9%
mul-1-neg99.9%
distribute-rgt-neg-out99.9%
Simplified99.9%
Taylor expanded in x around inf 58.6%
associate-*r*58.6%
neg-mul-158.6%
Simplified58.6%
if -1.4e14 < y < 0.170000000000000012Initial 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 98.6%
if 0.170000000000000012 < 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 y around inf 99.0%
mul-1-neg99.0%
distribute-rgt-neg-out99.0%
Simplified99.0%
distribute-rgt-neg-out99.0%
unsub-neg99.0%
*-commutative99.0%
Applied egg-rr99.0%
Final simplification88.1%
(FPCore (x y) :precision binary64 (+ y x))
double code(double x, double y) {
return y + x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y + x
end function
public static double code(double x, double y) {
return y + x;
}
def code(x, y): return y + x
function code(x, y) return Float64(y + x) end
function tmp = code(x, y) tmp = y + x; end
code[x_, y_] := N[(y + x), $MachinePrecision]
\begin{array}{l}
\\
y + 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 74.9%
Final simplification74.9%
(FPCore (x y) :precision binary64 y)
double code(double x, double y) {
return y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y
end function
public static double code(double x, double y) {
return y;
}
def code(x, y): return y
function code(x, y) return y end
function tmp = code(x, y) tmp = y; end
code[x_, y_] := y
\begin{array}{l}
\\
y
\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 inf 64.7%
mul-1-neg64.7%
distribute-rgt-neg-out64.7%
Simplified64.7%
Taylor expanded in x around 0 39.9%
Final simplification39.9%
herbie shell --seed 2024043
(FPCore (x y)
:name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
:precision binary64
(- (+ x y) (* x y)))