
(FPCore (x y z) :precision binary64 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * 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) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z): return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)) end
function tmp = code(x, y, z) tmp = (((x * y) + (z * z)) + (z * z)) + (z * z); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))
double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * 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) + (z * z)) + (z * z)) + (z * z)
end function
public static double code(double x, double y, double z) {
return (((x * y) + (z * z)) + (z * z)) + (z * z);
}
def code(x, y, z): return (((x * y) + (z * z)) + (z * z)) + (z * z)
function code(x, y, z) return Float64(Float64(Float64(Float64(x * y) + Float64(z * z)) + Float64(z * z)) + Float64(z * z)) end
function tmp = code(x, y, z) tmp = (((x * y) + (z * z)) + (z * z)) + (z * z); end
code[x_, y_, z_] := N[(N[(N[(N[(x * y), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision] + N[(z * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(x \cdot y + z \cdot z\right) + z \cdot z\right) + z \cdot z
\end{array}
(FPCore (x y z) :precision binary64 (if (<= (* z z) 2e+285) (+ (* z z) (+ (* z z) (+ (* z z) (* x y)))) (* x (+ y (* 3.0 (* z (/ z x)))))))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 2e+285) {
tmp = (z * z) + ((z * z) + ((z * z) + (x * y)));
} else {
tmp = x * (y + (3.0 * (z * (z / 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 ((z * z) <= 2d+285) then
tmp = (z * z) + ((z * z) + ((z * z) + (x * y)))
else
tmp = x * (y + (3.0d0 * (z * (z / x))))
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 2e+285) {
tmp = (z * z) + ((z * z) + ((z * z) + (x * y)));
} else {
tmp = x * (y + (3.0 * (z * (z / x))));
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 2e+285: tmp = (z * z) + ((z * z) + ((z * z) + (x * y))) else: tmp = x * (y + (3.0 * (z * (z / x)))) return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 2e+285) tmp = Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(x * y)))); else tmp = Float64(x * Float64(y + Float64(3.0 * Float64(z * Float64(z / x))))); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 2e+285) tmp = (z * z) + ((z * z) + ((z * z) + (x * y))); else tmp = x * (y + (3.0 * (z * (z / x)))); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 2e+285], N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x * N[(y + N[(3.0 * N[(z * N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 2 \cdot 10^{+285}:\\
\;\;\;\;z \cdot z + \left(z \cdot z + \left(z \cdot z + x \cdot y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(y + 3 \cdot \left(z \cdot \frac{z}{x}\right)\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 2e285Initial program 99.8%
if 2e285 < (*.f64 z z) Initial program 91.0%
Taylor expanded in x around inf 97.0%
distribute-lft1-in97.0%
metadata-eval97.0%
Simplified97.0%
unpow297.0%
associate-/l*100.0%
Applied egg-rr100.0%
Final simplification99.8%
(FPCore (x y z) :precision binary64 (fma z z (fma x y (* 2.0 (* z z)))))
double code(double x, double y, double z) {
return fma(z, z, fma(x, y, (2.0 * (z * z))));
}
function code(x, y, z) return fma(z, z, fma(x, y, Float64(2.0 * Float64(z * z)))) end
code[x_, y_, z_] := N[(z * z + N[(x * y + N[(2.0 * N[(z * z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, z, \mathsf{fma}\left(x, y, 2 \cdot \left(z \cdot z\right)\right)\right)
\end{array}
Initial program 97.5%
+-commutative97.5%
fma-define97.6%
associate-+l+97.6%
fma-define99.2%
count-299.2%
Simplified99.2%
(FPCore (x y z) :precision binary64 (fma x y (* z (* z 3.0))))
double code(double x, double y, double z) {
return fma(x, y, (z * (z * 3.0)));
}
function code(x, y, z) return fma(x, y, Float64(z * Float64(z * 3.0))) end
code[x_, y_, z_] := N[(x * y + N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, y, z \cdot \left(z \cdot 3\right)\right)
\end{array}
Initial program 97.5%
associate-+l+97.5%
associate-+l+97.5%
fma-define99.1%
associate-+r+99.1%
distribute-lft-out99.1%
distribute-lft-out99.1%
remove-double-neg99.1%
unsub-neg99.1%
count-299.1%
neg-mul-199.1%
distribute-rgt-out--99.1%
metadata-eval99.1%
Simplified99.1%
(FPCore (x y z)
:precision binary64
(if (or (<= (* z z) 1.4e-134)
(and (not (<= (* z z) 1.6e-91)) (<= (* z z) 3.1e-41)))
(* x y)
(* (* z z) 3.0)))
double code(double x, double y, double z) {
double tmp;
if (((z * z) <= 1.4e-134) || (!((z * z) <= 1.6e-91) && ((z * z) <= 3.1e-41))) {
tmp = x * y;
} else {
tmp = (z * z) * 3.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 (((z * z) <= 1.4d-134) .or. (.not. ((z * z) <= 1.6d-91)) .and. ((z * z) <= 3.1d-41)) then
tmp = x * y
else
tmp = (z * z) * 3.0d0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (((z * z) <= 1.4e-134) || (!((z * z) <= 1.6e-91) && ((z * z) <= 3.1e-41))) {
tmp = x * y;
} else {
tmp = (z * z) * 3.0;
}
return tmp;
}
def code(x, y, z): tmp = 0 if ((z * z) <= 1.4e-134) or (not ((z * z) <= 1.6e-91) and ((z * z) <= 3.1e-41)): tmp = x * y else: tmp = (z * z) * 3.0 return tmp
function code(x, y, z) tmp = 0.0 if ((Float64(z * z) <= 1.4e-134) || (!(Float64(z * z) <= 1.6e-91) && (Float64(z * z) <= 3.1e-41))) tmp = Float64(x * y); else tmp = Float64(Float64(z * z) * 3.0); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (((z * z) <= 1.4e-134) || (~(((z * z) <= 1.6e-91)) && ((z * z) <= 3.1e-41))) tmp = x * y; else tmp = (z * z) * 3.0; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[N[(z * z), $MachinePrecision], 1.4e-134], And[N[Not[LessEqual[N[(z * z), $MachinePrecision], 1.6e-91]], $MachinePrecision], LessEqual[N[(z * z), $MachinePrecision], 3.1e-41]]], N[(x * y), $MachinePrecision], N[(N[(z * z), $MachinePrecision] * 3.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 1.4 \cdot 10^{-134} \lor \neg \left(z \cdot z \leq 1.6 \cdot 10^{-91}\right) \land z \cdot z \leq 3.1 \cdot 10^{-41}:\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(z \cdot z\right) \cdot 3\\
\end{array}
\end{array}
if (*.f64 z z) < 1.3999999999999999e-134 or 1.59999999999999998e-91 < (*.f64 z z) < 3.10000000000000001e-41Initial program 100.0%
Taylor expanded in x around inf 100.0%
distribute-lft1-in100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around inf 91.8%
if 1.3999999999999999e-134 < (*.f64 z z) < 1.59999999999999998e-91 or 3.10000000000000001e-41 < (*.f64 z z) Initial program 95.8%
Taylor expanded in x around 0 85.3%
distribute-lft1-in85.3%
metadata-eval85.3%
*-commutative85.3%
Simplified85.3%
unpow285.3%
Applied egg-rr85.3%
Final simplification88.0%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 4e+98) (* x (+ y (* 3.0 (* z (/ z x))))) (* (* z z) 3.0)))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 4e+98) {
tmp = x * (y + (3.0 * (z * (z / x))));
} else {
tmp = (z * z) * 3.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 ((z * z) <= 4d+98) then
tmp = x * (y + (3.0d0 * (z * (z / x))))
else
tmp = (z * z) * 3.0d0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 4e+98) {
tmp = x * (y + (3.0 * (z * (z / x))));
} else {
tmp = (z * z) * 3.0;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 4e+98: tmp = x * (y + (3.0 * (z * (z / x)))) else: tmp = (z * z) * 3.0 return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 4e+98) tmp = Float64(x * Float64(y + Float64(3.0 * Float64(z * Float64(z / x))))); else tmp = Float64(Float64(z * z) * 3.0); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 4e+98) tmp = x * (y + (3.0 * (z * (z / x)))); else tmp = (z * z) * 3.0; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 4e+98], N[(x * N[(y + N[(3.0 * N[(z * N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(z * z), $MachinePrecision] * 3.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 4 \cdot 10^{+98}:\\
\;\;\;\;x \cdot \left(y + 3 \cdot \left(z \cdot \frac{z}{x}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(z \cdot z\right) \cdot 3\\
\end{array}
\end{array}
if (*.f64 z z) < 3.99999999999999999e98Initial program 99.9%
Taylor expanded in x around inf 98.6%
distribute-lft1-in98.6%
metadata-eval98.6%
Simplified98.6%
unpow298.6%
associate-/l*98.6%
Applied egg-rr98.6%
if 3.99999999999999999e98 < (*.f64 z z) Initial program 94.4%
Taylor expanded in x around 0 91.7%
distribute-lft1-in91.7%
metadata-eval91.7%
*-commutative91.7%
Simplified91.7%
unpow291.7%
Applied egg-rr91.7%
(FPCore (x y z) :precision binary64 (if (<= y 2e-138) (* x (+ y (* 3.0 (* z (/ z x))))) (* y (+ x (* 3.0 (* z (/ z y)))))))
double code(double x, double y, double z) {
double tmp;
if (y <= 2e-138) {
tmp = x * (y + (3.0 * (z * (z / x))));
} else {
tmp = y * (x + (3.0 * (z * (z / 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 <= 2d-138) then
tmp = x * (y + (3.0d0 * (z * (z / x))))
else
tmp = y * (x + (3.0d0 * (z * (z / y))))
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= 2e-138) {
tmp = x * (y + (3.0 * (z * (z / x))));
} else {
tmp = y * (x + (3.0 * (z * (z / y))));
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= 2e-138: tmp = x * (y + (3.0 * (z * (z / x)))) else: tmp = y * (x + (3.0 * (z * (z / y)))) return tmp
function code(x, y, z) tmp = 0.0 if (y <= 2e-138) tmp = Float64(x * Float64(y + Float64(3.0 * Float64(z * Float64(z / x))))); else tmp = Float64(y * Float64(x + Float64(3.0 * Float64(z * Float64(z / y))))); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= 2e-138) tmp = x * (y + (3.0 * (z * (z / x)))); else tmp = y * (x + (3.0 * (z * (z / y)))); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, 2e-138], N[(x * N[(y + N[(3.0 * N[(z * N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * N[(x + N[(3.0 * N[(z * N[(z / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2 \cdot 10^{-138}:\\
\;\;\;\;x \cdot \left(y + 3 \cdot \left(z \cdot \frac{z}{x}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x + 3 \cdot \left(z \cdot \frac{z}{y}\right)\right)\\
\end{array}
\end{array}
if y < 2.00000000000000013e-138Initial program 96.6%
Taylor expanded in x around inf 91.8%
distribute-lft1-in91.8%
metadata-eval91.8%
Simplified91.8%
unpow291.8%
associate-/l*93.1%
Applied egg-rr93.1%
if 2.00000000000000013e-138 < y Initial program 98.9%
Taylor expanded in y around inf 99.8%
distribute-lft1-in99.8%
metadata-eval99.8%
Simplified99.8%
unpow299.8%
associate-/l*99.8%
Applied egg-rr99.8%
(FPCore (x y z) :precision binary64 (if (<= (* z z) 6.5e-39) (+ (* z z) (* x y)) (* (* z z) 3.0)))
double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 6.5e-39) {
tmp = (z * z) + (x * y);
} else {
tmp = (z * z) * 3.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 ((z * z) <= 6.5d-39) then
tmp = (z * z) + (x * y)
else
tmp = (z * z) * 3.0d0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z * z) <= 6.5e-39) {
tmp = (z * z) + (x * y);
} else {
tmp = (z * z) * 3.0;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z * z) <= 6.5e-39: tmp = (z * z) + (x * y) else: tmp = (z * z) * 3.0 return tmp
function code(x, y, z) tmp = 0.0 if (Float64(z * z) <= 6.5e-39) tmp = Float64(Float64(z * z) + Float64(x * y)); else tmp = Float64(Float64(z * z) * 3.0); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z * z) <= 6.5e-39) tmp = (z * z) + (x * y); else tmp = (z * z) * 3.0; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[N[(z * z), $MachinePrecision], 6.5e-39], N[(N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(N[(z * z), $MachinePrecision] * 3.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 6.5 \cdot 10^{-39}:\\
\;\;\;\;z \cdot z + x \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(z \cdot z\right) \cdot 3\\
\end{array}
\end{array}
if (*.f64 z z) < 6.50000000000000027e-39Initial program 99.9%
associate-+l+99.9%
+-commutative99.9%
count-299.9%
add-sqr-sqrt99.9%
fma-define99.9%
*-commutative99.9%
sqrt-prod99.9%
sqrt-prod48.6%
add-sqr-sqrt92.9%
*-commutative92.9%
sqrt-prod92.8%
sqrt-prod48.6%
add-sqr-sqrt99.8%
Applied egg-rr99.8%
Taylor expanded in y around inf 99.8%
+-commutative99.8%
associate-/l*99.8%
fma-define99.8%
unpow299.8%
rem-square-sqrt100.0%
Simplified100.0%
Taylor expanded in z around 0 87.7%
if 6.50000000000000027e-39 < (*.f64 z z) Initial program 95.4%
Taylor expanded in x around 0 86.3%
distribute-lft1-in86.3%
metadata-eval86.3%
*-commutative86.3%
Simplified86.3%
unpow286.3%
Applied egg-rr86.3%
Final simplification86.9%
(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.5%
Taylor expanded in x around inf 91.3%
distribute-lft1-in91.3%
metadata-eval91.3%
Simplified91.3%
Taylor expanded in x around inf 51.0%
(FPCore (x y z) :precision binary64 (+ (* (* 3.0 z) z) (* y x)))
double code(double x, double y, double z) {
return ((3.0 * z) * z) + (y * x);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((3.0d0 * z) * z) + (y * x)
end function
public static double code(double x, double y, double z) {
return ((3.0 * z) * z) + (y * x);
}
def code(x, y, z): return ((3.0 * z) * z) + (y * x)
function code(x, y, z) return Float64(Float64(Float64(3.0 * z) * z) + Float64(y * x)) end
function tmp = code(x, y, z) tmp = ((3.0 * z) * z) + (y * x); end
code[x_, y_, z_] := N[(N[(N[(3.0 * z), $MachinePrecision] * z), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(3 \cdot z\right) \cdot z + y \cdot x
\end{array}
herbie shell --seed 2024111
(FPCore (x y z)
:name "Linear.Quaternion:$c/ from linear-1.19.1.3, A"
:precision binary64
:alt
(+ (* (* 3.0 z) z) (* y x))
(+ (+ (+ (* x y) (* z z)) (* z z)) (* z z)))