
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
return (x * y) + ((1.0 - 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 = (x * y) + ((1.0d0 - x) * z)
end function
public static double code(double x, double y, double z) {
return (x * y) + ((1.0 - x) * z);
}
def code(x, y, z): return (x * y) + ((1.0 - x) * z)
function code(x, y, z) return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z)) end
function tmp = code(x, y, z) tmp = (x * y) + ((1.0 - x) * z); end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y + \left(1 - x\right) \cdot z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x y) (* (- 1.0 x) z)))
double code(double x, double y, double z) {
return (x * y) + ((1.0 - 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 = (x * y) + ((1.0d0 - x) * z)
end function
public static double code(double x, double y, double z) {
return (x * y) + ((1.0 - x) * z);
}
def code(x, y, z): return (x * y) + ((1.0 - x) * z)
function code(x, y, z) return Float64(Float64(x * y) + Float64(Float64(1.0 - x) * z)) end
function tmp = code(x, y, z) tmp = (x * y) + ((1.0 - x) * z); end
code[x_, y_, z_] := N[(N[(x * y), $MachinePrecision] + N[(N[(1.0 - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y + \left(1 - x\right) \cdot z
\end{array}
(FPCore (x y z) :precision binary64 (fma x (- y z) z))
double code(double x, double y, double z) {
return fma(x, (y - z), z);
}
function code(x, y, z) return fma(x, Float64(y - z), z) end
code[x_, y_, z_] := N[(x * N[(y - z), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, y - z, z\right)
\end{array}
Initial program 99.2%
sub-neg99.2%
+-commutative99.2%
distribute-lft1-in99.2%
associate-+r+99.2%
+-commutative99.2%
*-commutative99.2%
neg-mul-199.2%
associate-*r*99.2%
*-commutative99.2%
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
(let* ((t_0 (* x (- z))))
(if (<= x -1.4e+55)
t_0
(if (<= x -2.3e-29)
(* x y)
(if (<= x 6200000000.0)
z
(if (or (<= x 5.1e+206) (not (<= x 3.9e+251))) t_0 (* x y)))))))
double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (x <= -1.4e+55) {
tmp = t_0;
} else if (x <= -2.3e-29) {
tmp = x * y;
} else if (x <= 6200000000.0) {
tmp = z;
} else if ((x <= 5.1e+206) || !(x <= 3.9e+251)) {
tmp = t_0;
} else {
tmp = x * y;
}
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) :: t_0
real(8) :: tmp
t_0 = x * -z
if (x <= (-1.4d+55)) then
tmp = t_0
else if (x <= (-2.3d-29)) then
tmp = x * y
else if (x <= 6200000000.0d0) then
tmp = z
else if ((x <= 5.1d+206) .or. (.not. (x <= 3.9d+251))) then
tmp = t_0
else
tmp = x * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (x <= -1.4e+55) {
tmp = t_0;
} else if (x <= -2.3e-29) {
tmp = x * y;
} else if (x <= 6200000000.0) {
tmp = z;
} else if ((x <= 5.1e+206) || !(x <= 3.9e+251)) {
tmp = t_0;
} else {
tmp = x * y;
}
return tmp;
}
def code(x, y, z): t_0 = x * -z tmp = 0 if x <= -1.4e+55: tmp = t_0 elif x <= -2.3e-29: tmp = x * y elif x <= 6200000000.0: tmp = z elif (x <= 5.1e+206) or not (x <= 3.9e+251): tmp = t_0 else: tmp = x * y return tmp
function code(x, y, z) t_0 = Float64(x * Float64(-z)) tmp = 0.0 if (x <= -1.4e+55) tmp = t_0; elseif (x <= -2.3e-29) tmp = Float64(x * y); elseif (x <= 6200000000.0) tmp = z; elseif ((x <= 5.1e+206) || !(x <= 3.9e+251)) tmp = t_0; else tmp = Float64(x * y); end return tmp end
function tmp_2 = code(x, y, z) t_0 = x * -z; tmp = 0.0; if (x <= -1.4e+55) tmp = t_0; elseif (x <= -2.3e-29) tmp = x * y; elseif (x <= 6200000000.0) tmp = z; elseif ((x <= 5.1e+206) || ~((x <= 3.9e+251))) tmp = t_0; else tmp = x * y; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * (-z)), $MachinePrecision]}, If[LessEqual[x, -1.4e+55], t$95$0, If[LessEqual[x, -2.3e-29], N[(x * y), $MachinePrecision], If[LessEqual[x, 6200000000.0], z, If[Or[LessEqual[x, 5.1e+206], N[Not[LessEqual[x, 3.9e+251]], $MachinePrecision]], t$95$0, N[(x * y), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(-z\right)\\
\mathbf{if}\;x \leq -1.4 \cdot 10^{+55}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq -2.3 \cdot 10^{-29}:\\
\;\;\;\;x \cdot y\\
\mathbf{elif}\;x \leq 6200000000:\\
\;\;\;\;z\\
\mathbf{elif}\;x \leq 5.1 \cdot 10^{+206} \lor \neg \left(x \leq 3.9 \cdot 10^{+251}\right):\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if x < -1.4e55 or 6.2e9 < x < 5.1000000000000003e206 or 3.89999999999999976e251 < x Initial program 98.2%
sub-neg98.2%
+-commutative98.2%
distribute-lft1-in98.2%
associate-+r+98.2%
+-commutative98.2%
*-commutative98.2%
neg-mul-198.2%
associate-*r*98.2%
*-commutative98.2%
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 99.7%
Taylor expanded in y around 0 60.9%
mul-1-neg60.9%
distribute-rgt-neg-in60.9%
Simplified60.9%
if -1.4e55 < x < -2.29999999999999991e-29 or 5.1000000000000003e206 < x < 3.89999999999999976e251Initial program 100.0%
Taylor expanded in y around inf 65.2%
if -2.29999999999999991e-29 < x < 6.2e9Initial program 100.0%
Taylor expanded in x around 0 75.6%
Final simplification68.2%
(FPCore (x y z)
:precision binary64
(if (<= y -5e+55)
(* x y)
(if (or (<= y 2e+19) (and (not (<= y 9.5e+69)) (<= y 5e+130)))
(* z (- 1.0 x))
(* x y))))
double code(double x, double y, double z) {
double tmp;
if (y <= -5e+55) {
tmp = x * y;
} else if ((y <= 2e+19) || (!(y <= 9.5e+69) && (y <= 5e+130))) {
tmp = z * (1.0 - x);
} else {
tmp = x * y;
}
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 <= (-5d+55)) then
tmp = x * y
else if ((y <= 2d+19) .or. (.not. (y <= 9.5d+69)) .and. (y <= 5d+130)) then
tmp = z * (1.0d0 - x)
else
tmp = x * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -5e+55) {
tmp = x * y;
} else if ((y <= 2e+19) || (!(y <= 9.5e+69) && (y <= 5e+130))) {
tmp = z * (1.0 - x);
} else {
tmp = x * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -5e+55: tmp = x * y elif (y <= 2e+19) or (not (y <= 9.5e+69) and (y <= 5e+130)): tmp = z * (1.0 - x) else: tmp = x * y return tmp
function code(x, y, z) tmp = 0.0 if (y <= -5e+55) tmp = Float64(x * y); elseif ((y <= 2e+19) || (!(y <= 9.5e+69) && (y <= 5e+130))) tmp = Float64(z * Float64(1.0 - x)); else tmp = Float64(x * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -5e+55) tmp = x * y; elseif ((y <= 2e+19) || (~((y <= 9.5e+69)) && (y <= 5e+130))) tmp = z * (1.0 - x); else tmp = x * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -5e+55], N[(x * y), $MachinePrecision], If[Or[LessEqual[y, 2e+19], And[N[Not[LessEqual[y, 9.5e+69]], $MachinePrecision], LessEqual[y, 5e+130]]], N[(z * N[(1.0 - x), $MachinePrecision]), $MachinePrecision], N[(x * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -5 \cdot 10^{+55}:\\
\;\;\;\;x \cdot y\\
\mathbf{elif}\;y \leq 2 \cdot 10^{+19} \lor \neg \left(y \leq 9.5 \cdot 10^{+69}\right) \land y \leq 5 \cdot 10^{+130}:\\
\;\;\;\;z \cdot \left(1 - x\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if y < -5.00000000000000046e55 or 2e19 < y < 9.4999999999999995e69 or 4.9999999999999996e130 < y Initial program 97.7%
Taylor expanded in y around inf 73.5%
if -5.00000000000000046e55 < y < 2e19 or 9.4999999999999995e69 < y < 4.9999999999999996e130Initial program 100.0%
Taylor expanded in y around 0 84.5%
Final simplification80.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -2.5e-42) (not (<= x 9e-6))) (* x (- y z)) (* z (- 1.0 x))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -2.5e-42) || !(x <= 9e-6)) {
tmp = x * (y - z);
} else {
tmp = z * (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 <= (-2.5d-42)) .or. (.not. (x <= 9d-6))) then
tmp = x * (y - z)
else
tmp = z * (1.0d0 - x)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -2.5e-42) || !(x <= 9e-6)) {
tmp = x * (y - z);
} else {
tmp = z * (1.0 - x);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -2.5e-42) or not (x <= 9e-6): tmp = x * (y - z) else: tmp = z * (1.0 - x) return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -2.5e-42) || !(x <= 9e-6)) tmp = Float64(x * Float64(y - z)); else tmp = Float64(z * Float64(1.0 - x)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -2.5e-42) || ~((x <= 9e-6))) tmp = x * (y - z); else tmp = z * (1.0 - x); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -2.5e-42], N[Not[LessEqual[x, 9e-6]], $MachinePrecision]], N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision], N[(z * N[(1.0 - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.5 \cdot 10^{-42} \lor \neg \left(x \leq 9 \cdot 10^{-6}\right):\\
\;\;\;\;x \cdot \left(y - z\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(1 - x\right)\\
\end{array}
\end{array}
if x < -2.50000000000000001e-42 or 9.00000000000000023e-6 < x Initial program 98.6%
sub-neg98.6%
+-commutative98.6%
distribute-lft1-in98.6%
associate-+r+98.6%
+-commutative98.6%
*-commutative98.6%
neg-mul-198.6%
associate-*r*98.6%
*-commutative98.6%
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.7%
if -2.50000000000000001e-42 < x < 9.00000000000000023e-6Initial program 100.0%
Taylor expanded in y around 0 77.3%
Final simplification88.0%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.0))) (* x (- y z)) (+ z (* x y))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = x * (y - z);
} else {
tmp = z + (x * y);
}
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 * (y - z)
else
tmp = z + (x * y)
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 * (y - z);
} else {
tmp = z + (x * y);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -1.0) or not (x <= 1.0): tmp = x * (y - z) else: tmp = z + (x * y) return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.0)) tmp = Float64(x * Float64(y - z)); else tmp = Float64(z + Float64(x * y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -1.0) || ~((x <= 1.0))) tmp = x * (y - z); else tmp = z + (x * y); 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[(y - z), $MachinePrecision]), $MachinePrecision], N[(z + N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;x \cdot \left(y - z\right)\\
\mathbf{else}:\\
\;\;\;\;z + x \cdot y\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 98.5%
sub-neg98.5%
+-commutative98.5%
distribute-lft1-in98.5%
associate-+r+98.5%
+-commutative98.5%
*-commutative98.5%
neg-mul-198.5%
associate-*r*98.5%
*-commutative98.5%
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.6%
if -1 < x < 1Initial program 100.0%
sub-neg100.0%
+-commutative100.0%
distribute-lft1-in100.0%
associate-+r+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 y around inf 99.2%
Final simplification98.9%
(FPCore (x y z) :precision binary64 (if (<= x -3.2e-24) (* x y) (if (<= x 3.5e-18) z (* x y))))
double code(double x, double y, double z) {
double tmp;
if (x <= -3.2e-24) {
tmp = x * y;
} else if (x <= 3.5e-18) {
tmp = z;
} else {
tmp = x * y;
}
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 <= (-3.2d-24)) then
tmp = x * y
else if (x <= 3.5d-18) then
tmp = z
else
tmp = x * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -3.2e-24) {
tmp = x * y;
} else if (x <= 3.5e-18) {
tmp = z;
} else {
tmp = x * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -3.2e-24: tmp = x * y elif x <= 3.5e-18: tmp = z else: tmp = x * y return tmp
function code(x, y, z) tmp = 0.0 if (x <= -3.2e-24) tmp = Float64(x * y); elseif (x <= 3.5e-18) tmp = z; else tmp = Float64(x * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -3.2e-24) tmp = x * y; elseif (x <= 3.5e-18) tmp = z; else tmp = x * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -3.2e-24], N[(x * y), $MachinePrecision], If[LessEqual[x, 3.5e-18], z, N[(x * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-24}:\\
\;\;\;\;x \cdot y\\
\mathbf{elif}\;x \leq 3.5 \cdot 10^{-18}:\\
\;\;\;\;z\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if x < -3.20000000000000012e-24 or 3.4999999999999999e-18 < x Initial program 98.6%
Taylor expanded in y around inf 47.7%
if -3.20000000000000012e-24 < x < 3.4999999999999999e-18Initial program 100.0%
Taylor expanded in x around 0 77.4%
Final simplification61.1%
(FPCore (x y z) :precision binary64 (+ z (* x (- y z))))
double code(double x, double y, double z) {
return z + (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 = z + (x * (y - z))
end function
public static double code(double x, double y, double z) {
return z + (x * (y - z));
}
def code(x, y, z): return z + (x * (y - z))
function code(x, y, z) return Float64(z + Float64(x * Float64(y - z))) end
function tmp = code(x, y, z) tmp = z + (x * (y - z)); end
code[x_, y_, z_] := N[(z + N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
z + x \cdot \left(y - z\right)
\end{array}
Initial program 99.2%
sub-neg99.2%
+-commutative99.2%
distribute-lft1-in99.2%
associate-+r+99.2%
+-commutative99.2%
*-commutative99.2%
neg-mul-199.2%
associate-*r*99.2%
*-commutative99.2%
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 z)
double code(double x, double y, double z) {
return z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = z
end function
public static double code(double x, double y, double z) {
return z;
}
def code(x, y, z): return z
function code(x, y, z) return z end
function tmp = code(x, y, z) tmp = z; end
code[x_, y_, z_] := z
\begin{array}{l}
\\
z
\end{array}
Initial program 99.2%
Taylor expanded in x around 0 37.5%
Final simplification37.5%
herbie shell --seed 2023195
(FPCore (x y z)
:name "Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3"
:precision binary64
(+ (* x y) (* (- 1.0 x) z)))