
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
return x + (y * (z + x));
}
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 * (z + x))
end function
public static double code(double x, double y, double z) {
return x + (y * (z + x));
}
def code(x, y, z): return x + (y * (z + x))
function code(x, y, z) return Float64(x + Float64(y * Float64(z + x))) end
function tmp = code(x, y, z) tmp = x + (y * (z + x)); end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + y \cdot \left(z + x\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
return x + (y * (z + x));
}
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 * (z + x))
end function
public static double code(double x, double y, double z) {
return x + (y * (z + x));
}
def code(x, y, z): return x + (y * (z + x))
function code(x, y, z) return Float64(x + Float64(y * Float64(z + x))) end
function tmp = code(x, y, z) tmp = x + (y * (z + x)); end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + y \cdot \left(z + x\right)
\end{array}
(FPCore (x y z) :precision binary64 (fma y (+ x z) x))
double code(double x, double y, double z) {
return fma(y, (x + z), x);
}
function code(x, y, z) return fma(y, Float64(x + z), x) end
code[x_, y_, z_] := N[(y * N[(x + z), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x + z, x\right)
\end{array}
Initial program 100.0%
+-commutative100.0%
fma-define100.0%
+-commutative100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y z)
:precision binary64
(if (<= y -1.0)
(* y x)
(if (<= y -2.2e-93)
x
(if (<= y -4.8e-103)
(* y z)
(if (<= y 1.5e-18)
x
(if (or (<= y 8.5e+17)
(and (not (<= y 1.18e+39))
(or (<= y 3.5e+123) (not (<= y 2.25e+260)))))
(* y z)
(* y x)))))))
double code(double x, double y, double z) {
double tmp;
if (y <= -1.0) {
tmp = y * x;
} else if (y <= -2.2e-93) {
tmp = x;
} else if (y <= -4.8e-103) {
tmp = y * z;
} else if (y <= 1.5e-18) {
tmp = x;
} else if ((y <= 8.5e+17) || (!(y <= 1.18e+39) && ((y <= 3.5e+123) || !(y <= 2.25e+260)))) {
tmp = y * z;
} else {
tmp = y * 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 (y <= (-1.0d0)) then
tmp = y * x
else if (y <= (-2.2d-93)) then
tmp = x
else if (y <= (-4.8d-103)) then
tmp = y * z
else if (y <= 1.5d-18) then
tmp = x
else if ((y <= 8.5d+17) .or. (.not. (y <= 1.18d+39)) .and. (y <= 3.5d+123) .or. (.not. (y <= 2.25d+260))) then
tmp = y * z
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -1.0) {
tmp = y * x;
} else if (y <= -2.2e-93) {
tmp = x;
} else if (y <= -4.8e-103) {
tmp = y * z;
} else if (y <= 1.5e-18) {
tmp = x;
} else if ((y <= 8.5e+17) || (!(y <= 1.18e+39) && ((y <= 3.5e+123) || !(y <= 2.25e+260)))) {
tmp = y * z;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -1.0: tmp = y * x elif y <= -2.2e-93: tmp = x elif y <= -4.8e-103: tmp = y * z elif y <= 1.5e-18: tmp = x elif (y <= 8.5e+17) or (not (y <= 1.18e+39) and ((y <= 3.5e+123) or not (y <= 2.25e+260))): tmp = y * z else: tmp = y * x return tmp
function code(x, y, z) tmp = 0.0 if (y <= -1.0) tmp = Float64(y * x); elseif (y <= -2.2e-93) tmp = x; elseif (y <= -4.8e-103) tmp = Float64(y * z); elseif (y <= 1.5e-18) tmp = x; elseif ((y <= 8.5e+17) || (!(y <= 1.18e+39) && ((y <= 3.5e+123) || !(y <= 2.25e+260)))) tmp = Float64(y * z); else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -1.0) tmp = y * x; elseif (y <= -2.2e-93) tmp = x; elseif (y <= -4.8e-103) tmp = y * z; elseif (y <= 1.5e-18) tmp = x; elseif ((y <= 8.5e+17) || (~((y <= 1.18e+39)) && ((y <= 3.5e+123) || ~((y <= 2.25e+260))))) tmp = y * z; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -1.0], N[(y * x), $MachinePrecision], If[LessEqual[y, -2.2e-93], x, If[LessEqual[y, -4.8e-103], N[(y * z), $MachinePrecision], If[LessEqual[y, 1.5e-18], x, If[Or[LessEqual[y, 8.5e+17], And[N[Not[LessEqual[y, 1.18e+39]], $MachinePrecision], Or[LessEqual[y, 3.5e+123], N[Not[LessEqual[y, 2.25e+260]], $MachinePrecision]]]], N[(y * z), $MachinePrecision], N[(y * x), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;y \leq -2.2 \cdot 10^{-93}:\\
\;\;\;\;x\\
\mathbf{elif}\;y \leq -4.8 \cdot 10^{-103}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;y \leq 1.5 \cdot 10^{-18}:\\
\;\;\;\;x\\
\mathbf{elif}\;y \leq 8.5 \cdot 10^{+17} \lor \neg \left(y \leq 1.18 \cdot 10^{+39}\right) \land \left(y \leq 3.5 \cdot 10^{+123} \lor \neg \left(y \leq 2.25 \cdot 10^{+260}\right)\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if y < -1 or 8.5e17 < y < 1.17999999999999996e39 or 3.5e123 < y < 2.25000000000000011e260Initial program 100.0%
Taylor expanded in x around inf 59.5%
+-commutative59.5%
Simplified59.5%
Taylor expanded in y around inf 58.4%
if -1 < y < -2.19999999999999996e-93 or -4.8000000000000004e-103 < y < 1.49999999999999991e-18Initial program 100.0%
Taylor expanded in y around 0 73.6%
if -2.19999999999999996e-93 < y < -4.8000000000000004e-103 or 1.49999999999999991e-18 < y < 8.5e17 or 1.17999999999999996e39 < y < 3.5e123 or 2.25000000000000011e260 < y Initial program 100.0%
Taylor expanded in z around inf 78.4%
Taylor expanded in x around 0 76.3%
Final simplification68.3%
(FPCore (x y z)
:precision binary64
(if (or (<= x -7.6e-31)
(not (or (<= x 7e-94) (and (not (<= x 1.7e-73)) (<= x 1.4e-21)))))
(* x (+ y 1.0))
(* y z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -7.6e-31) || !((x <= 7e-94) || (!(x <= 1.7e-73) && (x <= 1.4e-21)))) {
tmp = x * (y + 1.0);
} else {
tmp = y * 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 <= (-7.6d-31)) .or. (.not. (x <= 7d-94) .or. (.not. (x <= 1.7d-73)) .and. (x <= 1.4d-21))) then
tmp = x * (y + 1.0d0)
else
tmp = y * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -7.6e-31) || !((x <= 7e-94) || (!(x <= 1.7e-73) && (x <= 1.4e-21)))) {
tmp = x * (y + 1.0);
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -7.6e-31) or not ((x <= 7e-94) or (not (x <= 1.7e-73) and (x <= 1.4e-21))): tmp = x * (y + 1.0) else: tmp = y * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -7.6e-31) || !((x <= 7e-94) || (!(x <= 1.7e-73) && (x <= 1.4e-21)))) tmp = Float64(x * Float64(y + 1.0)); else tmp = Float64(y * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -7.6e-31) || ~(((x <= 7e-94) || (~((x <= 1.7e-73)) && (x <= 1.4e-21))))) tmp = x * (y + 1.0); else tmp = y * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -7.6e-31], N[Not[Or[LessEqual[x, 7e-94], And[N[Not[LessEqual[x, 1.7e-73]], $MachinePrecision], LessEqual[x, 1.4e-21]]]], $MachinePrecision]], N[(x * N[(y + 1.0), $MachinePrecision]), $MachinePrecision], N[(y * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.6 \cdot 10^{-31} \lor \neg \left(x \leq 7 \cdot 10^{-94} \lor \neg \left(x \leq 1.7 \cdot 10^{-73}\right) \land x \leq 1.4 \cdot 10^{-21}\right):\\
\;\;\;\;x \cdot \left(y + 1\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if x < -7.5999999999999999e-31 or 6.99999999999999996e-94 < x < 1.7000000000000001e-73 or 1.40000000000000002e-21 < x Initial program 100.0%
Taylor expanded in x around inf 86.8%
+-commutative86.8%
Simplified86.8%
if -7.5999999999999999e-31 < x < 6.99999999999999996e-94 or 1.7000000000000001e-73 < x < 1.40000000000000002e-21Initial program 100.0%
Taylor expanded in z around inf 90.7%
Taylor expanded in x around 0 75.9%
Final simplification82.1%
(FPCore (x y z) :precision binary64 (if (or (<= x -620000000.0) (not (<= x 2.6e+94))) (* x (+ y 1.0)) (* y (+ x z))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -620000000.0) || !(x <= 2.6e+94)) {
tmp = x * (y + 1.0);
} 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 <= (-620000000.0d0)) .or. (.not. (x <= 2.6d+94))) then
tmp = x * (y + 1.0d0)
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 <= -620000000.0) || !(x <= 2.6e+94)) {
tmp = x * (y + 1.0);
} else {
tmp = y * (x + z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -620000000.0) or not (x <= 2.6e+94): tmp = x * (y + 1.0) else: tmp = y * (x + z) return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -620000000.0) || !(x <= 2.6e+94)) tmp = Float64(x * Float64(y + 1.0)); else tmp = Float64(y * Float64(x + z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -620000000.0) || ~((x <= 2.6e+94))) tmp = x * (y + 1.0); else tmp = y * (x + z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -620000000.0], N[Not[LessEqual[x, 2.6e+94]], $MachinePrecision]], N[(x * N[(y + 1.0), $MachinePrecision]), $MachinePrecision], N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -620000000 \lor \neg \left(x \leq 2.6 \cdot 10^{+94}\right):\\
\;\;\;\;x \cdot \left(y + 1\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x + z\right)\\
\end{array}
\end{array}
if x < -6.2e8 or 2.5999999999999999e94 < x Initial program 100.0%
Taylor expanded in x around inf 89.9%
+-commutative89.9%
Simplified89.9%
if -6.2e8 < x < 2.5999999999999999e94Initial program 100.0%
+-commutative100.0%
fma-define100.0%
+-commutative100.0%
Simplified100.0%
fma-undefine100.0%
+-commutative100.0%
+-commutative100.0%
distribute-lft-in100.0%
associate-+r+100.0%
Applied egg-rr100.0%
Taylor expanded in y around inf 82.4%
Final simplification85.6%
(FPCore (x y z) :precision binary64 (if (or (<= y -32.0) (not (<= y 0.045))) (* y (+ x z)) (+ x (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -32.0) || !(y <= 0.045)) {
tmp = y * (x + z);
} else {
tmp = x + (y * 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 <= (-32.0d0)) .or. (.not. (y <= 0.045d0))) then
tmp = y * (x + z)
else
tmp = x + (y * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -32.0) || !(y <= 0.045)) {
tmp = y * (x + z);
} else {
tmp = x + (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -32.0) or not (y <= 0.045): tmp = y * (x + z) else: tmp = x + (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -32.0) || !(y <= 0.045)) tmp = Float64(y * Float64(x + z)); else tmp = Float64(x + Float64(y * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -32.0) || ~((y <= 0.045))) tmp = y * (x + z); else tmp = x + (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -32.0], N[Not[LessEqual[y, 0.045]], $MachinePrecision]], N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision], N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -32 \lor \neg \left(y \leq 0.045\right):\\
\;\;\;\;y \cdot \left(x + z\right)\\
\mathbf{else}:\\
\;\;\;\;x + y \cdot z\\
\end{array}
\end{array}
if y < -32 or 0.044999999999999998 < y Initial program 100.0%
+-commutative100.0%
fma-define100.0%
+-commutative100.0%
Simplified100.0%
fma-undefine100.0%
+-commutative100.0%
+-commutative100.0%
distribute-lft-in96.9%
associate-+r+96.9%
Applied egg-rr96.9%
Taylor expanded in y around inf 99.1%
if -32 < y < 0.044999999999999998Initial program 100.0%
Taylor expanded in z around inf 98.2%
Final simplification98.7%
(FPCore (x y z) :precision binary64 (if (or (<= y -1.0) (not (<= y 1950000000.0))) (* y x) x))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1.0) || !(y <= 1950000000.0)) {
tmp = y * x;
} else {
tmp = 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 ((y <= (-1.0d0)) .or. (.not. (y <= 1950000000.0d0))) then
tmp = y * x
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -1.0) || !(y <= 1950000000.0)) {
tmp = y * x;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -1.0) or not (y <= 1950000000.0): tmp = y * x else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -1.0) || !(y <= 1950000000.0)) tmp = Float64(y * x); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -1.0) || ~((y <= 1950000000.0))) tmp = y * x; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.0], N[Not[LessEqual[y, 1950000000.0]], $MachinePrecision]], N[(y * x), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1 \lor \neg \left(y \leq 1950000000\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if y < -1 or 1.95e9 < y Initial program 100.0%
Taylor expanded in x around inf 53.3%
+-commutative53.3%
Simplified53.3%
Taylor expanded in y around inf 52.4%
if -1 < y < 1.95e9Initial program 100.0%
Taylor expanded in y around 0 66.6%
Final simplification59.4%
(FPCore (x y z) :precision binary64 (+ x (* y (+ x z))))
double code(double x, double y, double z) {
return 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 = x + (y * (x + z))
end function
public static double code(double x, double y, double z) {
return x + (y * (x + z));
}
def code(x, y, z): return x + (y * (x + z))
function code(x, y, z) return Float64(x + Float64(y * Float64(x + z))) end
function tmp = code(x, y, z) tmp = x + (y * (x + z)); end
code[x_, y_, z_] := N[(x + N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + y \cdot \left(x + z\right)
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y z) :precision binary64 x)
double code(double x, double y, double z) {
return x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x
end function
public static double code(double x, double y, double z) {
return x;
}
def code(x, y, z): return x
function code(x, y, z) return x end
function tmp = code(x, y, z) tmp = x; end
code[x_, y_, z_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 100.0%
Taylor expanded in y around 0 34.1%
Final simplification34.1%
herbie shell --seed 2024044
(FPCore (x y z)
:name "Main:bigenough2 from A"
:precision binary64
(+ x (* y (+ z x))))