
(FPCore (x y z) :precision binary64 (+ (* (- 1.0 x) y) (* x z)))
double code(double x, double y, double z) {
return ((1.0 - x) * y) + (x * z);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((1.0d0 - x) * y) + (x * z)
end function
public static double code(double x, double y, double z) {
return ((1.0 - x) * y) + (x * z);
}
def code(x, y, z): return ((1.0 - x) * y) + (x * z)
function code(x, y, z) return Float64(Float64(Float64(1.0 - x) * y) + Float64(x * z)) end
function tmp = code(x, y, z) tmp = ((1.0 - x) * y) + (x * z); end
code[x_, y_, z_] := N[(N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision] + N[(x * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 - x\right) \cdot y + x \cdot z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* (- 1.0 x) y) (* x z)))
double code(double x, double y, double z) {
return ((1.0 - x) * y) + (x * z);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((1.0d0 - x) * y) + (x * z)
end function
public static double code(double x, double y, double z) {
return ((1.0 - x) * y) + (x * z);
}
def code(x, y, z): return ((1.0 - x) * y) + (x * z)
function code(x, y, z) return Float64(Float64(Float64(1.0 - x) * y) + Float64(x * z)) end
function tmp = code(x, y, z) tmp = ((1.0 - x) * y) + (x * z); end
code[x_, y_, z_] := N[(N[(N[(1.0 - x), $MachinePrecision] * y), $MachinePrecision] + N[(x * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(1 - x\right) \cdot y + x \cdot z
\end{array}
(FPCore (x y z) :precision binary64 (fma x (- z y) y))
double code(double x, double y, double z) {
return fma(x, (z - y), y);
}
function code(x, y, z) return fma(x, Float64(z - y), y) end
code[x_, y_, z_] := N[(x * N[(z - y), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, z - y, y\right)
\end{array}
Initial program 98.4%
sub-neg98.4%
+-commutative98.4%
distribute-rgt1-in98.4%
associate-+l+98.4%
+-commutative98.4%
*-commutative98.4%
neg-mul-198.4%
associate-*r*98.4%
*-commutative98.4%
distribute-rgt-out100.0%
fma-def100.0%
+-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
unsub-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y z)
:precision binary64
(if (or (<= y -4.2e-14)
(and (not (<= y -2.25e-91))
(or (<= y -2.1e-152) (not (<= y 2.75e-73)))))
(* y (- 1.0 x))
(* x z)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -4.2e-14) || (!(y <= -2.25e-91) && ((y <= -2.1e-152) || !(y <= 2.75e-73)))) {
tmp = y * (1.0 - x);
} else {
tmp = x * z;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((y <= (-4.2d-14)) .or. (.not. (y <= (-2.25d-91))) .and. (y <= (-2.1d-152)) .or. (.not. (y <= 2.75d-73))) then
tmp = y * (1.0d0 - x)
else
tmp = x * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -4.2e-14) || (!(y <= -2.25e-91) && ((y <= -2.1e-152) || !(y <= 2.75e-73)))) {
tmp = y * (1.0 - x);
} else {
tmp = x * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -4.2e-14) or (not (y <= -2.25e-91) and ((y <= -2.1e-152) or not (y <= 2.75e-73))): tmp = y * (1.0 - x) else: tmp = x * z return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -4.2e-14) || (!(y <= -2.25e-91) && ((y <= -2.1e-152) || !(y <= 2.75e-73)))) tmp = Float64(y * Float64(1.0 - x)); else tmp = Float64(x * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -4.2e-14) || (~((y <= -2.25e-91)) && ((y <= -2.1e-152) || ~((y <= 2.75e-73))))) tmp = y * (1.0 - x); else tmp = x * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -4.2e-14], And[N[Not[LessEqual[y, -2.25e-91]], $MachinePrecision], Or[LessEqual[y, -2.1e-152], N[Not[LessEqual[y, 2.75e-73]], $MachinePrecision]]]], N[(y * N[(1.0 - x), $MachinePrecision]), $MachinePrecision], N[(x * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.2 \cdot 10^{-14} \lor \neg \left(y \leq -2.25 \cdot 10^{-91}\right) \land \left(y \leq -2.1 \cdot 10^{-152} \lor \neg \left(y \leq 2.75 \cdot 10^{-73}\right)\right):\\
\;\;\;\;y \cdot \left(1 - x\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot z\\
\end{array}
\end{array}
if y < -4.1999999999999998e-14 or -2.24999999999999988e-91 < y < -2.09999999999999999e-152 or 2.75000000000000003e-73 < y Initial program 97.5%
Taylor expanded in y around inf 81.9%
if -4.1999999999999998e-14 < y < -2.24999999999999988e-91 or -2.09999999999999999e-152 < y < 2.75000000000000003e-73Initial program 100.0%
Taylor expanded in y around 0 75.1%
Final simplification79.4%
(FPCore (x y z)
:precision binary64
(if (<= x -1.45e-48)
(* x z)
(if (<= x 8.6e-23)
y
(if (<= x 1.5e+197) (* x z) (if (<= x 2.5e+255) (* y (- x)) (* x z))))))
double code(double x, double y, double z) {
double tmp;
if (x <= -1.45e-48) {
tmp = x * z;
} else if (x <= 8.6e-23) {
tmp = y;
} else if (x <= 1.5e+197) {
tmp = x * z;
} else if (x <= 2.5e+255) {
tmp = y * -x;
} else {
tmp = x * z;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if (x <= (-1.45d-48)) then
tmp = x * z
else if (x <= 8.6d-23) then
tmp = y
else if (x <= 1.5d+197) then
tmp = x * z
else if (x <= 2.5d+255) then
tmp = y * -x
else
tmp = x * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -1.45e-48) {
tmp = x * z;
} else if (x <= 8.6e-23) {
tmp = y;
} else if (x <= 1.5e+197) {
tmp = x * z;
} else if (x <= 2.5e+255) {
tmp = y * -x;
} else {
tmp = x * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -1.45e-48: tmp = x * z elif x <= 8.6e-23: tmp = y elif x <= 1.5e+197: tmp = x * z elif x <= 2.5e+255: tmp = y * -x else: tmp = x * z return tmp
function code(x, y, z) tmp = 0.0 if (x <= -1.45e-48) tmp = Float64(x * z); elseif (x <= 8.6e-23) tmp = y; elseif (x <= 1.5e+197) tmp = Float64(x * z); elseif (x <= 2.5e+255) tmp = Float64(y * Float64(-x)); else tmp = Float64(x * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -1.45e-48) tmp = x * z; elseif (x <= 8.6e-23) tmp = y; elseif (x <= 1.5e+197) tmp = x * z; elseif (x <= 2.5e+255) tmp = y * -x; else tmp = x * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -1.45e-48], N[(x * z), $MachinePrecision], If[LessEqual[x, 8.6e-23], y, If[LessEqual[x, 1.5e+197], N[(x * z), $MachinePrecision], If[LessEqual[x, 2.5e+255], N[(y * (-x)), $MachinePrecision], N[(x * z), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.45 \cdot 10^{-48}:\\
\;\;\;\;x \cdot z\\
\mathbf{elif}\;x \leq 8.6 \cdot 10^{-23}:\\
\;\;\;\;y\\
\mathbf{elif}\;x \leq 1.5 \cdot 10^{+197}:\\
\;\;\;\;x \cdot z\\
\mathbf{elif}\;x \leq 2.5 \cdot 10^{+255}:\\
\;\;\;\;y \cdot \left(-x\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot z\\
\end{array}
\end{array}
if x < -1.4500000000000001e-48 or 8.60000000000000004e-23 < x < 1.5000000000000001e197 or 2.5000000000000001e255 < x Initial program 96.8%
Taylor expanded in y around 0 62.6%
if -1.4500000000000001e-48 < x < 8.60000000000000004e-23Initial program 100.0%
Taylor expanded in x around 0 77.9%
if 1.5000000000000001e197 < x < 2.5000000000000001e255Initial program 100.0%
sub-neg100.0%
+-commutative100.0%
distribute-rgt1-in100.0%
associate-+l+100.0%
+-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
associate-*r*100.0%
*-commutative100.0%
distribute-rgt-out100.0%
fma-def100.0%
+-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in x around inf 100.0%
Taylor expanded in z around 0 73.9%
mul-1-neg73.9%
distribute-rgt-neg-in73.9%
Simplified73.9%
Final simplification70.1%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.66e-49) (not (<= x 1.5))) (* x (- z y)) (* y (- 1.0 x))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.66e-49) || !(x <= 1.5)) {
tmp = x * (z - y);
} else {
tmp = y * (1.0 - x);
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((x <= (-1.66d-49)) .or. (.not. (x <= 1.5d0))) then
tmp = x * (z - y)
else
tmp = y * (1.0d0 - x)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -1.66e-49) || !(x <= 1.5)) {
tmp = x * (z - y);
} else {
tmp = y * (1.0 - x);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -1.66e-49) or not (x <= 1.5): tmp = x * (z - y) else: tmp = y * (1.0 - x) return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -1.66e-49) || !(x <= 1.5)) tmp = Float64(x * Float64(z - y)); else tmp = Float64(y * Float64(1.0 - x)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -1.66e-49) || ~((x <= 1.5))) tmp = x * (z - y); else tmp = y * (1.0 - x); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.66e-49], N[Not[LessEqual[x, 1.5]], $MachinePrecision]], N[(x * N[(z - y), $MachinePrecision]), $MachinePrecision], N[(y * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.66 \cdot 10^{-49} \lor \neg \left(x \leq 1.5\right):\\
\;\;\;\;x \cdot \left(z - y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 - x\right)\\
\end{array}
\end{array}
if x < -1.6599999999999999e-49 or 1.5 < x Initial program 97.1%
sub-neg97.1%
+-commutative97.1%
distribute-rgt1-in97.1%
associate-+l+97.1%
+-commutative97.1%
*-commutative97.1%
neg-mul-197.1%
associate-*r*97.1%
*-commutative97.1%
distribute-rgt-out100.0%
fma-def100.0%
+-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in x around inf 96.3%
if -1.6599999999999999e-49 < x < 1.5Initial program 100.0%
Taylor expanded in y around inf 77.5%
Final simplification87.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.0))) (* x (- z y)) (+ y (* x z))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = x * (z - y);
} else {
tmp = y + (x * z);
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if ((x <= (-1.0d0)) .or. (.not. (x <= 1.0d0))) then
tmp = x * (z - y)
else
tmp = y + (x * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = x * (z - y);
} else {
tmp = y + (x * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -1.0) or not (x <= 1.0): tmp = x * (z - y) else: tmp = y + (x * z) return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.0)) tmp = Float64(x * Float64(z - y)); else tmp = Float64(y + Float64(x * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -1.0) || ~((x <= 1.0))) tmp = x * (z - y); else tmp = y + (x * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(x * N[(z - y), $MachinePrecision]), $MachinePrecision], N[(y + N[(x * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;x \cdot \left(z - y\right)\\
\mathbf{else}:\\
\;\;\;\;y + x \cdot z\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 96.9%
sub-neg96.9%
+-commutative96.9%
distribute-rgt1-in96.9%
associate-+l+96.9%
+-commutative96.9%
*-commutative96.9%
neg-mul-196.9%
associate-*r*96.9%
*-commutative96.9%
distribute-rgt-out100.0%
fma-def100.0%
+-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in x around inf 98.8%
if -1 < x < 1Initial program 100.0%
sub-neg100.0%
+-commutative100.0%
distribute-rgt1-in100.0%
associate-+l+100.0%
+-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
associate-*r*100.0%
*-commutative100.0%
distribute-rgt-out100.0%
fma-def100.0%
+-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
Taylor expanded in z around inf 99.1%
Final simplification99.0%
(FPCore (x y z) :precision binary64 (if (<= x -2.65e-49) (* x z) (if (<= x 2.7e-22) y (* x z))))
double code(double x, double y, double z) {
double tmp;
if (x <= -2.65e-49) {
tmp = x * z;
} else if (x <= 2.7e-22) {
tmp = y;
} else {
tmp = x * z;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if (x <= (-2.65d-49)) then
tmp = x * z
else if (x <= 2.7d-22) then
tmp = y
else
tmp = x * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -2.65e-49) {
tmp = x * z;
} else if (x <= 2.7e-22) {
tmp = y;
} else {
tmp = x * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -2.65e-49: tmp = x * z elif x <= 2.7e-22: tmp = y else: tmp = x * z return tmp
function code(x, y, z) tmp = 0.0 if (x <= -2.65e-49) tmp = Float64(x * z); elseif (x <= 2.7e-22) tmp = y; else tmp = Float64(x * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -2.65e-49) tmp = x * z; elseif (x <= 2.7e-22) tmp = y; else tmp = x * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -2.65e-49], N[(x * z), $MachinePrecision], If[LessEqual[x, 2.7e-22], y, N[(x * z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.65 \cdot 10^{-49}:\\
\;\;\;\;x \cdot z\\
\mathbf{elif}\;x \leq 2.7 \cdot 10^{-22}:\\
\;\;\;\;y\\
\mathbf{else}:\\
\;\;\;\;x \cdot z\\
\end{array}
\end{array}
if x < -2.6500000000000001e-49 or 2.7000000000000002e-22 < x Initial program 97.2%
Taylor expanded in y around 0 59.6%
if -2.6500000000000001e-49 < x < 2.7000000000000002e-22Initial program 100.0%
Taylor expanded in x around 0 77.9%
Final simplification67.8%
(FPCore (x y z) :precision binary64 (+ y (* x (- z y))))
double code(double x, double y, double z) {
return y + (x * (z - y));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y + (x * (z - y))
end function
public static double code(double x, double y, double z) {
return y + (x * (z - y));
}
def code(x, y, z): return y + (x * (z - y))
function code(x, y, z) return Float64(y + Float64(x * Float64(z - y))) end
function tmp = code(x, y, z) tmp = y + (x * (z - y)); end
code[x_, y_, z_] := N[(y + N[(x * N[(z - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y + x \cdot \left(z - y\right)
\end{array}
Initial program 98.4%
sub-neg98.4%
+-commutative98.4%
distribute-rgt1-in98.4%
associate-+l+98.4%
+-commutative98.4%
*-commutative98.4%
neg-mul-198.4%
associate-*r*98.4%
*-commutative98.4%
distribute-rgt-out100.0%
fma-def100.0%
+-commutative100.0%
*-commutative100.0%
neg-mul-1100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in x around 0 100.0%
Final simplification100.0%
(FPCore (x y z) :precision binary64 y)
double code(double x, double y, double z) {
return y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y
end function
public static double code(double x, double y, double z) {
return y;
}
def code(x, y, z): return y
function code(x, y, z) return y end
function tmp = code(x, y, z) tmp = y; end
code[x_, y_, z_] := y
\begin{array}{l}
\\
y
\end{array}
Initial program 98.4%
Taylor expanded in x around 0 38.1%
Final simplification38.1%
(FPCore (x y z) :precision binary64 (- y (* x (- y z))))
double code(double x, double y, double z) {
return y - (x * (y - z));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y - (x * (y - z))
end function
public static double code(double x, double y, double z) {
return y - (x * (y - z));
}
def code(x, y, z): return y - (x * (y - z))
function code(x, y, z) return Float64(y - Float64(x * Float64(y - z))) end
function tmp = code(x, y, z) tmp = y - (x * (y - z)); end
code[x_, y_, z_] := N[(y - N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y - x \cdot \left(y - z\right)
\end{array}
herbie shell --seed 2023200
(FPCore (x y z)
:name "Diagrams.Color.HSV:lerp from diagrams-contrib-1.3.0.5"
:precision binary64
:herbie-target
(- y (* x (- y z)))
(+ (* (- 1.0 x) y) (* x z)))