
(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 6 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 97.2%
Taylor expanded in x around 0
+-commutativeN/A
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
unsub-negN/A
lower--.f64100.0
Applied rewrites100.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* x (- z))))
(if (<= x -1.3e+159)
t_0
(if (<= x -3.2e-15)
(* x y)
(if (<= x 0.0052) (fma z x z) (if (<= x 3.8e+207) (* x y) t_0))))))
double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (x <= -1.3e+159) {
tmp = t_0;
} else if (x <= -3.2e-15) {
tmp = x * y;
} else if (x <= 0.0052) {
tmp = fma(z, x, z);
} else if (x <= 3.8e+207) {
tmp = x * y;
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(x * Float64(-z)) tmp = 0.0 if (x <= -1.3e+159) tmp = t_0; elseif (x <= -3.2e-15) tmp = Float64(x * y); elseif (x <= 0.0052) tmp = fma(z, x, z); elseif (x <= 3.8e+207) tmp = Float64(x * y); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * (-z)), $MachinePrecision]}, If[LessEqual[x, -1.3e+159], t$95$0, If[LessEqual[x, -3.2e-15], N[(x * y), $MachinePrecision], If[LessEqual[x, 0.0052], N[(z * x + z), $MachinePrecision], If[LessEqual[x, 3.8e+207], N[(x * y), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(-z\right)\\
\mathbf{if}\;x \leq -1.3 \cdot 10^{+159}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq -3.2 \cdot 10^{-15}:\\
\;\;\;\;x \cdot y\\
\mathbf{elif}\;x \leq 0.0052:\\
\;\;\;\;\mathsf{fma}\left(z, x, z\right)\\
\mathbf{elif}\;x \leq 3.8 \cdot 10^{+207}:\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -1.3e159 or 3.79999999999999986e207 < x Initial program 90.4%
Taylor expanded in x around inf
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
unsub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
mul-1-negN/A
distribute-rgt-neg-inN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
lower-neg.f6466.5
Applied rewrites66.5%
if -1.3e159 < x < -3.1999999999999999e-15 or 0.0051999999999999998 < x < 3.79999999999999986e207Initial program 97.5%
Taylor expanded in y around inf
lower-*.f6463.5
Applied rewrites63.5%
if -3.1999999999999999e-15 < x < 0.0051999999999999998Initial program 100.0%
Taylor expanded in y around 0
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6478.1
Applied rewrites78.1%
lift-*.f64N/A
sub-negN/A
+-commutativeN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
lower-neg.f6478.1
Applied rewrites78.1%
neg-sub0N/A
flip3--N/A
metadata-evalN/A
sub0-negN/A
sqr-powN/A
pow-prod-downN/A
sqr-negN/A
lift-neg.f64N/A
lift-neg.f64N/A
pow-prod-downN/A
sqr-powN/A
lift-neg.f64N/A
cube-negN/A
sub0-negN/A
metadata-evalN/A
distribute-neg-fracN/A
flip3--N/A
neg-sub0N/A
remove-double-neg77.0
Applied rewrites77.0%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* x (- y z)))) (if (<= x -1.2e-8) t_0 (if (<= x 0.23) (- z (* x z)) t_0))))
double code(double x, double y, double z) {
double t_0 = x * (y - z);
double tmp;
if (x <= -1.2e-8) {
tmp = t_0;
} else if (x <= 0.23) {
tmp = z - (x * z);
} else {
tmp = t_0;
}
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 * (y - z)
if (x <= (-1.2d-8)) then
tmp = t_0
else if (x <= 0.23d0) then
tmp = z - (x * z)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = x * (y - z);
double tmp;
if (x <= -1.2e-8) {
tmp = t_0;
} else if (x <= 0.23) {
tmp = z - (x * z);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = x * (y - z) tmp = 0 if x <= -1.2e-8: tmp = t_0 elif x <= 0.23: tmp = z - (x * z) else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(x * Float64(y - z)) tmp = 0.0 if (x <= -1.2e-8) tmp = t_0; elseif (x <= 0.23) tmp = Float64(z - Float64(x * z)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = x * (y - z); tmp = 0.0; if (x <= -1.2e-8) tmp = t_0; elseif (x <= 0.23) tmp = z - (x * z); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -1.2e-8], t$95$0, If[LessEqual[x, 0.23], N[(z - N[(x * z), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(y - z\right)\\
\mathbf{if}\;x \leq -1.2 \cdot 10^{-8}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq 0.23:\\
\;\;\;\;z - x \cdot z\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -1.19999999999999999e-8 or 0.23000000000000001 < x Initial program 94.7%
Taylor expanded in x around inf
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
unsub-negN/A
lower--.f6499.6
Applied rewrites99.6%
if -1.19999999999999999e-8 < x < 0.23000000000000001Initial program 100.0%
Taylor expanded in y around 0
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6478.1
Applied rewrites78.1%
Final simplification89.4%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* x (- y z)))) (if (<= x -3.1e-15) t_0 (if (<= x 0.0052) (fma z x z) t_0))))
double code(double x, double y, double z) {
double t_0 = x * (y - z);
double tmp;
if (x <= -3.1e-15) {
tmp = t_0;
} else if (x <= 0.0052) {
tmp = fma(z, x, z);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = Float64(x * Float64(y - z)) tmp = 0.0 if (x <= -3.1e-15) tmp = t_0; elseif (x <= 0.0052) tmp = fma(z, x, z); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * N[(y - z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -3.1e-15], t$95$0, If[LessEqual[x, 0.0052], N[(z * x + z), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(y - z\right)\\
\mathbf{if}\;x \leq -3.1 \cdot 10^{-15}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq 0.0052:\\
\;\;\;\;\mathsf{fma}\left(z, x, z\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -3.0999999999999999e-15 or 0.0051999999999999998 < x Initial program 94.7%
Taylor expanded in x around inf
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-*.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
unsub-negN/A
lower--.f6499.6
Applied rewrites99.6%
if -3.0999999999999999e-15 < x < 0.0051999999999999998Initial program 100.0%
Taylor expanded in y around 0
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6478.1
Applied rewrites78.1%
lift-*.f64N/A
sub-negN/A
+-commutativeN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
lower-neg.f6478.1
Applied rewrites78.1%
neg-sub0N/A
flip3--N/A
metadata-evalN/A
sub0-negN/A
sqr-powN/A
pow-prod-downN/A
sqr-negN/A
lift-neg.f64N/A
lift-neg.f64N/A
pow-prod-downN/A
sqr-powN/A
lift-neg.f64N/A
cube-negN/A
sub0-negN/A
metadata-evalN/A
distribute-neg-fracN/A
flip3--N/A
neg-sub0N/A
remove-double-neg77.0
Applied rewrites77.0%
(FPCore (x y z) :precision binary64 (if (<= x -3.2e-15) (* x y) (if (<= x 0.0052) (fma z x z) (* x y))))
double code(double x, double y, double z) {
double tmp;
if (x <= -3.2e-15) {
tmp = x * y;
} else if (x <= 0.0052) {
tmp = fma(z, x, z);
} else {
tmp = x * y;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (x <= -3.2e-15) tmp = Float64(x * y); elseif (x <= 0.0052) tmp = fma(z, x, z); else tmp = Float64(x * y); end return tmp end
code[x_, y_, z_] := If[LessEqual[x, -3.2e-15], N[(x * y), $MachinePrecision], If[LessEqual[x, 0.0052], N[(z * x + z), $MachinePrecision], N[(x * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.2 \cdot 10^{-15}:\\
\;\;\;\;x \cdot y\\
\mathbf{elif}\;x \leq 0.0052:\\
\;\;\;\;\mathsf{fma}\left(z, x, z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if x < -3.1999999999999999e-15 or 0.0051999999999999998 < x Initial program 94.7%
Taylor expanded in y around inf
lower-*.f6455.3
Applied rewrites55.3%
if -3.1999999999999999e-15 < x < 0.0051999999999999998Initial program 100.0%
Taylor expanded in y around 0
distribute-rgt-out--N/A
*-lft-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6478.1
Applied rewrites78.1%
lift-*.f64N/A
sub-negN/A
+-commutativeN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
lower-neg.f6478.1
Applied rewrites78.1%
neg-sub0N/A
flip3--N/A
metadata-evalN/A
sub0-negN/A
sqr-powN/A
pow-prod-downN/A
sqr-negN/A
lift-neg.f64N/A
lift-neg.f64N/A
pow-prod-downN/A
sqr-powN/A
lift-neg.f64N/A
cube-negN/A
sub0-negN/A
metadata-evalN/A
distribute-neg-fracN/A
flip3--N/A
neg-sub0N/A
remove-double-neg77.0
Applied rewrites77.0%
(FPCore (x y z) :precision binary64 (* x y))
double code(double x, double y, double z) {
return x * y;
}
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
end function
public static double code(double x, double y, double z) {
return x * y;
}
def code(x, y, z): return x * y
function code(x, y, z) return Float64(x * y) end
function tmp = code(x, y, z) tmp = x * y; end
code[x_, y_, z_] := N[(x * y), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y
\end{array}
Initial program 97.2%
Taylor expanded in y around inf
lower-*.f6440.2
Applied rewrites40.2%
herbie shell --seed 2024219
(FPCore (x y z)
:name "Diagrams.Backend.Rasterific:$crender from diagrams-rasterific-1.3.1.3"
:precision binary64
(+ (* x y) (* (- 1.0 x) z)))