
(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-def100.0%
+-commutative100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y z)
:precision binary64
(if (<= y -2.1e+212)
(* y z)
(if (<= y -7.2e+156)
(* y x)
(if (<= y -2.2e+113)
(* y z)
(if (<= y -1.0)
(* y x)
(if (<= y 1.45e-12)
x
(if (<= y 3.4e+129)
(* y z)
(if (<= y 5.8e+161) (* y x) (* y z)))))))))
double code(double x, double y, double z) {
double tmp;
if (y <= -2.1e+212) {
tmp = y * z;
} else if (y <= -7.2e+156) {
tmp = y * x;
} else if (y <= -2.2e+113) {
tmp = y * z;
} else if (y <= -1.0) {
tmp = y * x;
} else if (y <= 1.45e-12) {
tmp = x;
} else if (y <= 3.4e+129) {
tmp = y * z;
} else if (y <= 5.8e+161) {
tmp = y * x;
} 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 (y <= (-2.1d+212)) then
tmp = y * z
else if (y <= (-7.2d+156)) then
tmp = y * x
else if (y <= (-2.2d+113)) then
tmp = y * z
else if (y <= (-1.0d0)) then
tmp = y * x
else if (y <= 1.45d-12) then
tmp = x
else if (y <= 3.4d+129) then
tmp = y * z
else if (y <= 5.8d+161) then
tmp = y * x
else
tmp = y * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -2.1e+212) {
tmp = y * z;
} else if (y <= -7.2e+156) {
tmp = y * x;
} else if (y <= -2.2e+113) {
tmp = y * z;
} else if (y <= -1.0) {
tmp = y * x;
} else if (y <= 1.45e-12) {
tmp = x;
} else if (y <= 3.4e+129) {
tmp = y * z;
} else if (y <= 5.8e+161) {
tmp = y * x;
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -2.1e+212: tmp = y * z elif y <= -7.2e+156: tmp = y * x elif y <= -2.2e+113: tmp = y * z elif y <= -1.0: tmp = y * x elif y <= 1.45e-12: tmp = x elif y <= 3.4e+129: tmp = y * z elif y <= 5.8e+161: tmp = y * x else: tmp = y * z return tmp
function code(x, y, z) tmp = 0.0 if (y <= -2.1e+212) tmp = Float64(y * z); elseif (y <= -7.2e+156) tmp = Float64(y * x); elseif (y <= -2.2e+113) tmp = Float64(y * z); elseif (y <= -1.0) tmp = Float64(y * x); elseif (y <= 1.45e-12) tmp = x; elseif (y <= 3.4e+129) tmp = Float64(y * z); elseif (y <= 5.8e+161) tmp = Float64(y * x); else tmp = Float64(y * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -2.1e+212) tmp = y * z; elseif (y <= -7.2e+156) tmp = y * x; elseif (y <= -2.2e+113) tmp = y * z; elseif (y <= -1.0) tmp = y * x; elseif (y <= 1.45e-12) tmp = x; elseif (y <= 3.4e+129) tmp = y * z; elseif (y <= 5.8e+161) tmp = y * x; else tmp = y * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -2.1e+212], N[(y * z), $MachinePrecision], If[LessEqual[y, -7.2e+156], N[(y * x), $MachinePrecision], If[LessEqual[y, -2.2e+113], N[(y * z), $MachinePrecision], If[LessEqual[y, -1.0], N[(y * x), $MachinePrecision], If[LessEqual[y, 1.45e-12], x, If[LessEqual[y, 3.4e+129], N[(y * z), $MachinePrecision], If[LessEqual[y, 5.8e+161], N[(y * x), $MachinePrecision], N[(y * z), $MachinePrecision]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.1 \cdot 10^{+212}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;y \leq -7.2 \cdot 10^{+156}:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;y \leq -2.2 \cdot 10^{+113}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;y \leq -1:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;y \leq 1.45 \cdot 10^{-12}:\\
\;\;\;\;x\\
\mathbf{elif}\;y \leq 3.4 \cdot 10^{+129}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;y \leq 5.8 \cdot 10^{+161}:\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if y < -2.1e212 or -7.19999999999999958e156 < y < -2.2000000000000001e113 or 1.4500000000000001e-12 < y < 3.40000000000000018e129 or 5.80000000000000032e161 < y Initial program 100.0%
Taylor expanded in x around 0 64.5%
if -2.1e212 < y < -7.19999999999999958e156 or -2.2000000000000001e113 < y < -1 or 3.40000000000000018e129 < y < 5.80000000000000032e161Initial program 99.9%
Taylor expanded in x around inf 70.9%
Taylor expanded in y around inf 67.6%
if -1 < y < 1.4500000000000001e-12Initial program 100.0%
Taylor expanded in y around 0 76.7%
Final simplification71.1%
(FPCore (x y z) :precision binary64 (if (or (<= y -2.3e-33) (not (<= y 3.1e-21))) (* y (+ x z)) x))
double code(double x, double y, double z) {
double tmp;
if ((y <= -2.3e-33) || !(y <= 3.1e-21)) {
tmp = y * (x + z);
} 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 <= (-2.3d-33)) .or. (.not. (y <= 3.1d-21))) then
tmp = y * (x + z)
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -2.3e-33) || !(y <= 3.1e-21)) {
tmp = y * (x + z);
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -2.3e-33) or not (y <= 3.1e-21): tmp = y * (x + z) else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -2.3e-33) || !(y <= 3.1e-21)) tmp = Float64(y * Float64(x + z)); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -2.3e-33) || ~((y <= 3.1e-21))) tmp = y * (x + z); else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -2.3e-33], N[Not[LessEqual[y, 3.1e-21]], $MachinePrecision]], N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.3 \cdot 10^{-33} \lor \neg \left(y \leq 3.1 \cdot 10^{-21}\right):\\
\;\;\;\;y \cdot \left(x + z\right)\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if y < -2.29999999999999986e-33 or 3.0999999999999998e-21 < y Initial program 100.0%
Taylor expanded in y around inf 98.2%
if -2.29999999999999986e-33 < y < 3.0999999999999998e-21Initial program 100.0%
Taylor expanded in y around 0 77.8%
Final simplification88.1%
(FPCore (x y z) :precision binary64 (if (or (<= y -3.5e-32) (not (<= y 6.4e-23))) (* y (+ x z)) (* x (+ y 1.0))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -3.5e-32) || !(y <= 6.4e-23)) {
tmp = y * (x + z);
} else {
tmp = x * (y + 1.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) :: tmp
if ((y <= (-3.5d-32)) .or. (.not. (y <= 6.4d-23))) then
tmp = y * (x + z)
else
tmp = x * (y + 1.0d0)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -3.5e-32) || !(y <= 6.4e-23)) {
tmp = y * (x + z);
} else {
tmp = x * (y + 1.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -3.5e-32) or not (y <= 6.4e-23): tmp = y * (x + z) else: tmp = x * (y + 1.0) return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -3.5e-32) || !(y <= 6.4e-23)) tmp = Float64(y * Float64(x + z)); else tmp = Float64(x * Float64(y + 1.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -3.5e-32) || ~((y <= 6.4e-23))) tmp = y * (x + z); else tmp = x * (y + 1.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -3.5e-32], N[Not[LessEqual[y, 6.4e-23]], $MachinePrecision]], N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision], N[(x * N[(y + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.5 \cdot 10^{-32} \lor \neg \left(y \leq 6.4 \cdot 10^{-23}\right):\\
\;\;\;\;y \cdot \left(x + z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(y + 1\right)\\
\end{array}
\end{array}
if y < -3.4999999999999999e-32 or 6.39999999999999951e-23 < y Initial program 100.0%
Taylor expanded in y around inf 98.2%
if -3.4999999999999999e-32 < y < 6.39999999999999951e-23Initial program 100.0%
Taylor expanded in x around inf 77.8%
Final simplification88.1%
(FPCore (x y z) :precision binary64 (if (or (<= y -8.5) (not (<= y 3.8e-11))) (* y (+ x z)) (+ x (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -8.5) || !(y <= 3.8e-11)) {
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 <= (-8.5d0)) .or. (.not. (y <= 3.8d-11))) 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 <= -8.5) || !(y <= 3.8e-11)) {
tmp = y * (x + z);
} else {
tmp = x + (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -8.5) or not (y <= 3.8e-11): tmp = y * (x + z) else: tmp = x + (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -8.5) || !(y <= 3.8e-11)) 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 <= -8.5) || ~((y <= 3.8e-11))) tmp = y * (x + z); else tmp = x + (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -8.5], N[Not[LessEqual[y, 3.8e-11]], $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 -8.5 \lor \neg \left(y \leq 3.8 \cdot 10^{-11}\right):\\
\;\;\;\;y \cdot \left(x + z\right)\\
\mathbf{else}:\\
\;\;\;\;x + y \cdot z\\
\end{array}
\end{array}
if y < -8.5 or 3.7999999999999998e-11 < y Initial program 100.0%
Taylor expanded in y around inf 98.1%
if -8.5 < y < 3.7999999999999998e-11Initial program 100.0%
flip-+55.5%
associate-*r/54.8%
Applied egg-rr54.8%
*-commutative54.8%
associate-/l*53.0%
Simplified53.0%
Taylor expanded in z around inf 70.7%
unpow270.7%
Simplified70.7%
Taylor expanded in z around inf 100.0%
Final simplification99.1%
(FPCore (x y z) :precision binary64 (if (<= y -1.0) (* y x) (if (<= y 1.0) x (* y x))))
double code(double x, double y, double z) {
double tmp;
if (y <= -1.0) {
tmp = y * x;
} else if (y <= 1.0) {
tmp = x;
} 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 <= 1.0d0) then
tmp = x
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 <= 1.0) {
tmp = x;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -1.0: tmp = y * x elif y <= 1.0: tmp = x else: tmp = y * x return tmp
function code(x, y, z) tmp = 0.0 if (y <= -1.0) tmp = Float64(y * x); elseif (y <= 1.0) tmp = x; 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 <= 1.0) tmp = x; 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, 1.0], x, N[(y * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1:\\
\;\;\;\;y \cdot x\\
\mathbf{elif}\;y \leq 1:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if y < -1 or 1 < y Initial program 100.0%
Taylor expanded in x around inf 50.8%
Taylor expanded in y around inf 49.0%
if -1 < y < 1Initial program 100.0%
Taylor expanded in y around 0 75.5%
Final simplification62.5%
(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 39.9%
Final simplification39.9%
herbie shell --seed 2023214
(FPCore (x y z)
:name "Main:bigenough2 from A"
:precision binary64
(+ x (* y (+ z x))))