
(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 8 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 y (- 1.0 x) x))
double code(double x, double y) {
return fma(y, (1.0 - x), x);
}
function code(x, y) return fma(y, Float64(1.0 - x), x) end
code[x_, y_] := N[(y * N[(1.0 - x), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, 1 - x, x\right)
\end{array}
Initial program 100.0%
Taylor expanded in x around 0 100.0%
sub-neg100.0%
distribute-lft-in100.0%
*-rgt-identity100.0%
+-commutative100.0%
associate-+l+100.0%
+-commutative100.0%
distribute-rgt-neg-out100.0%
distribute-lft-neg-in100.0%
distribute-lft1-in100.0%
+-commutative100.0%
neg-mul-1100.0%
*-commutative100.0%
neg-mul-1100.0%
sub-neg100.0%
fma-define100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(let* ((t_0 (* y (- x))))
(if (<= x -3.8e+213)
x
(if (<= x -9e+156)
t_0
(if (<= x -3e+120)
x
(if (<= x -6e+65)
t_0
(if (<= x -2.8e-130) x (if (<= x 1.0) y t_0))))))))
double code(double x, double y) {
double t_0 = y * -x;
double tmp;
if (x <= -3.8e+213) {
tmp = x;
} else if (x <= -9e+156) {
tmp = t_0;
} else if (x <= -3e+120) {
tmp = x;
} else if (x <= -6e+65) {
tmp = t_0;
} else if (x <= -2.8e-130) {
tmp = x;
} else if (x <= 1.0) {
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 = y * -x
if (x <= (-3.8d+213)) then
tmp = x
else if (x <= (-9d+156)) then
tmp = t_0
else if (x <= (-3d+120)) then
tmp = x
else if (x <= (-6d+65)) then
tmp = t_0
else if (x <= (-2.8d-130)) then
tmp = x
else if (x <= 1.0d0) then
tmp = y
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = y * -x;
double tmp;
if (x <= -3.8e+213) {
tmp = x;
} else if (x <= -9e+156) {
tmp = t_0;
} else if (x <= -3e+120) {
tmp = x;
} else if (x <= -6e+65) {
tmp = t_0;
} else if (x <= -2.8e-130) {
tmp = x;
} else if (x <= 1.0) {
tmp = y;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = y * -x tmp = 0 if x <= -3.8e+213: tmp = x elif x <= -9e+156: tmp = t_0 elif x <= -3e+120: tmp = x elif x <= -6e+65: tmp = t_0 elif x <= -2.8e-130: tmp = x elif x <= 1.0: tmp = y else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(y * Float64(-x)) tmp = 0.0 if (x <= -3.8e+213) tmp = x; elseif (x <= -9e+156) tmp = t_0; elseif (x <= -3e+120) tmp = x; elseif (x <= -6e+65) tmp = t_0; elseif (x <= -2.8e-130) tmp = x; elseif (x <= 1.0) tmp = y; else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = y * -x; tmp = 0.0; if (x <= -3.8e+213) tmp = x; elseif (x <= -9e+156) tmp = t_0; elseif (x <= -3e+120) tmp = x; elseif (x <= -6e+65) tmp = t_0; elseif (x <= -2.8e-130) tmp = x; elseif (x <= 1.0) tmp = y; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(y * (-x)), $MachinePrecision]}, If[LessEqual[x, -3.8e+213], x, If[LessEqual[x, -9e+156], t$95$0, If[LessEqual[x, -3e+120], x, If[LessEqual[x, -6e+65], t$95$0, If[LessEqual[x, -2.8e-130], x, If[LessEqual[x, 1.0], y, t$95$0]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(-x\right)\\
\mathbf{if}\;x \leq -3.8 \cdot 10^{+213}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq -9 \cdot 10^{+156}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq -3 \cdot 10^{+120}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq -6 \cdot 10^{+65}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq -2.8 \cdot 10^{-130}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;y\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -3.7999999999999997e213 or -9.00000000000000061e156 < x < -3e120 or -6.0000000000000004e65 < x < -2.80000000000000016e-130Initial program 100.0%
Taylor expanded in y around 0 63.1%
if -3.7999999999999997e213 < x < -9.00000000000000061e156 or -3e120 < x < -6.0000000000000004e65 or 1 < x Initial program 100.0%
Taylor expanded in x around inf 100.0%
Taylor expanded in y around inf 63.6%
mul-1-neg63.6%
*-commutative63.6%
distribute-rgt-neg-in63.6%
Simplified63.6%
if -2.80000000000000016e-130 < x < 1Initial program 100.0%
Taylor expanded in x around 0 78.9%
Final simplification69.7%
(FPCore (x y) :precision binary64 (if (<= x -4.8e-139) (* x (- 1.0 y)) (if (<= x 1.0) y (* y (- x)))))
double code(double x, double y) {
double tmp;
if (x <= -4.8e-139) {
tmp = x * (1.0 - y);
} else if (x <= 1.0) {
tmp = y;
} 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 <= (-4.8d-139)) then
tmp = x * (1.0d0 - y)
else if (x <= 1.0d0) then
tmp = y
else
tmp = y * -x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -4.8e-139) {
tmp = x * (1.0 - y);
} else if (x <= 1.0) {
tmp = y;
} else {
tmp = y * -x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -4.8e-139: tmp = x * (1.0 - y) elif x <= 1.0: tmp = y else: tmp = y * -x return tmp
function code(x, y) tmp = 0.0 if (x <= -4.8e-139) tmp = Float64(x * Float64(1.0 - y)); elseif (x <= 1.0) tmp = y; else tmp = Float64(y * Float64(-x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -4.8e-139) tmp = x * (1.0 - y); elseif (x <= 1.0) tmp = y; else tmp = y * -x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -4.8e-139], N[(x * N[(1.0 - y), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.0], y, N[(y * (-x)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.8 \cdot 10^{-139}:\\
\;\;\;\;x \cdot \left(1 - y\right)\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-x\right)\\
\end{array}
\end{array}
if x < -4.80000000000000029e-139Initial program 100.0%
Taylor expanded in x around inf 82.1%
if -4.80000000000000029e-139 < x < 1Initial program 100.0%
Taylor expanded in x around 0 78.5%
if 1 < x Initial program 100.0%
Taylor expanded in x around inf 100.0%
Taylor expanded in y around inf 60.1%
mul-1-neg60.1%
*-commutative60.1%
distribute-rgt-neg-in60.1%
Simplified60.1%
Final simplification75.3%
(FPCore (x y) :precision binary64 (if (<= y 1.35e-127) (* x (- 1.0 y)) (* y (- 1.0 x))))
double code(double x, double y) {
double tmp;
if (y <= 1.35e-127) {
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 (y <= 1.35d-127) 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 (y <= 1.35e-127) {
tmp = x * (1.0 - y);
} else {
tmp = y * (1.0 - x);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.35e-127: tmp = x * (1.0 - y) else: tmp = y * (1.0 - x) return tmp
function code(x, y) tmp = 0.0 if (y <= 1.35e-127) 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 (y <= 1.35e-127) tmp = x * (1.0 - y); else tmp = y * (1.0 - x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.35e-127], N[(x * N[(1.0 - y), $MachinePrecision]), $MachinePrecision], N[(y * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.35 \cdot 10^{-127}:\\
\;\;\;\;x \cdot \left(1 - y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 - x\right)\\
\end{array}
\end{array}
if y < 1.35e-127Initial program 100.0%
Taylor expanded in x around inf 73.5%
if 1.35e-127 < y Initial program 100.0%
Taylor expanded in y around inf 87.2%
Final simplification78.3%
(FPCore (x y) :precision binary64 (if (<= y 1.75e-127) (- x (* y x)) (* y (- 1.0 x))))
double code(double x, double y) {
double tmp;
if (y <= 1.75e-127) {
tmp = x - (y * x);
} 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 (y <= 1.75d-127) then
tmp = x - (y * x)
else
tmp = y * (1.0d0 - x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 1.75e-127) {
tmp = x - (y * x);
} else {
tmp = y * (1.0 - x);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.75e-127: tmp = x - (y * x) else: tmp = y * (1.0 - x) return tmp
function code(x, y) tmp = 0.0 if (y <= 1.75e-127) tmp = Float64(x - Float64(y * x)); else tmp = Float64(y * Float64(1.0 - x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 1.75e-127) tmp = x - (y * x); else tmp = y * (1.0 - x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.75e-127], N[(x - N[(y * x), $MachinePrecision]), $MachinePrecision], N[(y * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.75 \cdot 10^{-127}:\\
\;\;\;\;x - y \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 - x\right)\\
\end{array}
\end{array}
if y < 1.74999999999999995e-127Initial program 100.0%
Taylor expanded in x around inf 73.5%
sub-neg73.5%
distribute-rgt-in73.5%
*-un-lft-identity73.5%
Applied egg-rr73.5%
distribute-lft-neg-out73.5%
unsub-neg73.5%
*-commutative73.5%
Applied egg-rr73.5%
if 1.74999999999999995e-127 < y Initial program 100.0%
Taylor expanded in y around inf 87.2%
Final simplification78.3%
(FPCore (x y) :precision binary64 (- (+ y x) (* y x)))
double code(double x, double y) {
return (y + x) - (y * x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y + x) - (y * x)
end function
public static double code(double x, double y) {
return (y + x) - (y * x);
}
def code(x, y): return (y + x) - (y * x)
function code(x, y) return Float64(Float64(y + x) - Float64(y * x)) end
function tmp = code(x, y) tmp = (y + x) - (y * x); end
code[x_, y_] := N[(N[(y + x), $MachinePrecision] - N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(y + x\right) - y \cdot x
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= y 1.4e-127) x y))
double code(double x, double y) {
double tmp;
if (y <= 1.4e-127) {
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 <= 1.4d-127) then
tmp = x
else
tmp = y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 1.4e-127) {
tmp = x;
} else {
tmp = y;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.4e-127: tmp = x else: tmp = y return tmp
function code(x, y) tmp = 0.0 if (y <= 1.4e-127) tmp = x; else tmp = y; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 1.4e-127) tmp = x; else tmp = y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.4e-127], x, y]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.4 \cdot 10^{-127}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y\\
\end{array}
\end{array}
if y < 1.4e-127Initial program 100.0%
Taylor expanded in y around 0 52.5%
if 1.4e-127 < y Initial program 100.0%
Taylor expanded in x around 0 57.0%
Final simplification54.1%
(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%
Taylor expanded in y around 0 38.1%
Final simplification38.1%
herbie shell --seed 2024059
(FPCore (x y)
:name "Data.Colour.RGBSpace.HSL:hsl from colour-2.3.3, A"
:precision binary64
(- (+ x y) (* x y)))