
(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 (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 98.7%
+-commutative98.7%
fma-define98.8%
associate-+l+98.8%
fma-define99.6%
count-299.6%
Simplified99.6%
Final simplification99.6%
(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 98.7%
associate-+l+98.7%
associate-+l+98.7%
fma-define99.5%
associate-+r+99.5%
distribute-lft-out99.5%
distribute-lft-out99.5%
remove-double-neg99.5%
unsub-neg99.5%
count-299.5%
neg-mul-199.5%
distribute-rgt-out--99.5%
metadata-eval99.5%
Simplified99.5%
Final simplification99.5%
(FPCore (x y z) :precision binary64 (if (or (<= (* z z) 5e-129) (and (not (<= (* z z) 2e-87)) (<= (* z z) 2e+59))) (+ (* z z) (* x y)) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if (((z * z) <= 5e-129) || (!((z * z) <= 2e-87) && ((z * z) <= 2e+59))) {
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) <= 5d-129) .or. (.not. ((z * z) <= 2d-87)) .and. ((z * z) <= 2d+59)) 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) <= 5e-129) || (!((z * z) <= 2e-87) && ((z * z) <= 2e+59))) {
tmp = (z * z) + (x * y);
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if ((z * z) <= 5e-129) or (not ((z * z) <= 2e-87) and ((z * z) <= 2e+59)): 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) <= 5e-129) || (!(Float64(z * z) <= 2e-87) && (Float64(z * z) <= 2e+59))) tmp = Float64(Float64(z * z) + Float64(x * y)); else tmp = Float64(z * Float64(z * 3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (((z * z) <= 5e-129) || (~(((z * z) <= 2e-87)) && ((z * z) <= 2e+59))) tmp = (z * z) + (x * y); else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[N[(z * z), $MachinePrecision], 5e-129], And[N[Not[LessEqual[N[(z * z), $MachinePrecision], 2e-87]], $MachinePrecision], LessEqual[N[(z * z), $MachinePrecision], 2e+59]]], N[(N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \cdot z \leq 5 \cdot 10^{-129} \lor \neg \left(z \cdot z \leq 2 \cdot 10^{-87}\right) \land z \cdot z \leq 2 \cdot 10^{+59}:\\
\;\;\;\;z \cdot z + x \cdot y\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if (*.f64 z z) < 5.00000000000000027e-129 or 2.00000000000000004e-87 < (*.f64 z z) < 1.99999999999999994e59Initial program 100.0%
Taylor expanded in x around inf 91.5%
Taylor expanded in x around inf 91.3%
if 5.00000000000000027e-129 < (*.f64 z z) < 2.00000000000000004e-87 or 1.99999999999999994e59 < (*.f64 z z) Initial program 97.0%
associate-+l+97.0%
associate-+l+97.0%
fma-define98.9%
associate-+r+98.9%
distribute-lft-out98.9%
distribute-lft-out98.9%
remove-double-neg98.9%
unsub-neg98.9%
count-298.9%
neg-mul-198.9%
distribute-rgt-out--98.9%
metadata-eval98.9%
Simplified98.9%
add-sqr-sqrt98.7%
pow298.7%
associate-*r*98.8%
sqrt-prod98.7%
sqrt-prod44.7%
add-sqr-sqrt98.7%
Applied egg-rr98.7%
Taylor expanded in x around 0 87.9%
unpow287.9%
unpow287.9%
swap-sqr88.0%
unpow288.0%
Simplified88.0%
*-commutative88.0%
unpow-prod-down87.9%
pow287.9%
rem-square-sqrt88.2%
unpow288.2%
associate-*r*88.3%
Applied egg-rr88.3%
Final simplification90.0%
(FPCore (x y z) :precision binary64 (if (or (<= z 1.15e-64) (and (not (<= z 3.5e-39)) (<= z 4.2e+29))) (* x y) (* z (* z 3.0))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 1.15e-64) || (!(z <= 3.5e-39) && (z <= 4.2e+29))) {
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 <= 1.15d-64) .or. (.not. (z <= 3.5d-39)) .and. (z <= 4.2d+29)) 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 <= 1.15e-64) || (!(z <= 3.5e-39) && (z <= 4.2e+29))) {
tmp = x * y;
} else {
tmp = z * (z * 3.0);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 1.15e-64) or (not (z <= 3.5e-39) and (z <= 4.2e+29)): tmp = x * y else: tmp = z * (z * 3.0) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 1.15e-64) || (!(z <= 3.5e-39) && (z <= 4.2e+29))) tmp = Float64(x * y); else tmp = Float64(z * Float64(z * 3.0)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= 1.15e-64) || (~((z <= 3.5e-39)) && (z <= 4.2e+29))) tmp = x * y; else tmp = z * (z * 3.0); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 1.15e-64], And[N[Not[LessEqual[z, 3.5e-39]], $MachinePrecision], LessEqual[z, 4.2e+29]]], N[(x * y), $MachinePrecision], N[(z * N[(z * 3.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.15 \cdot 10^{-64} \lor \neg \left(z \leq 3.5 \cdot 10^{-39}\right) \land z \leq 4.2 \cdot 10^{+29}:\\
\;\;\;\;x \cdot y\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(z \cdot 3\right)\\
\end{array}
\end{array}
if z < 1.1500000000000001e-64 or 3.5e-39 < z < 4.2000000000000003e29Initial program 98.5%
associate-+l+98.5%
associate-+l+98.5%
fma-define99.4%
associate-+r+99.4%
distribute-lft-out99.4%
distribute-lft-out99.4%
remove-double-neg99.4%
unsub-neg99.4%
count-299.4%
neg-mul-199.4%
distribute-rgt-out--99.4%
metadata-eval99.4%
Simplified99.4%
add-sqr-sqrt99.3%
pow299.3%
associate-*r*99.4%
sqrt-prod99.3%
sqrt-prod33.5%
add-sqr-sqrt99.3%
Applied egg-rr99.3%
Taylor expanded in x around inf 69.7%
if 1.1500000000000001e-64 < z < 3.5e-39 or 4.2000000000000003e29 < z Initial program 99.8%
associate-+l+99.8%
associate-+l+99.8%
fma-define99.8%
associate-+r+99.8%
distribute-lft-out99.8%
distribute-lft-out99.9%
remove-double-neg99.9%
unsub-neg99.9%
count-299.9%
neg-mul-199.9%
distribute-rgt-out--99.9%
metadata-eval99.9%
Simplified99.9%
add-sqr-sqrt99.7%
pow299.7%
associate-*r*99.7%
sqrt-prod99.7%
sqrt-prod99.4%
add-sqr-sqrt99.7%
Applied egg-rr99.7%
Taylor expanded in x around 0 88.3%
unpow288.3%
unpow288.3%
swap-sqr88.4%
unpow288.4%
Simplified88.4%
*-commutative88.4%
unpow-prod-down88.3%
pow288.3%
rem-square-sqrt88.5%
unpow288.5%
associate-*r*88.6%
Applied egg-rr88.6%
Final simplification73.4%
(FPCore (x y z) :precision binary64 (+ (* z z) (+ (* z z) (+ (* z z) (* x y)))))
double code(double x, double y, double z) {
return (z * z) + ((z * z) + ((z * z) + (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 = (z * z) + ((z * z) + ((z * z) + (x * y)))
end function
public static double code(double x, double y, double z) {
return (z * z) + ((z * z) + ((z * z) + (x * y)));
}
def code(x, y, z): return (z * z) + ((z * z) + ((z * z) + (x * y)))
function code(x, y, z) return Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(Float64(z * z) + Float64(x * y)))) end
function tmp = code(x, y, z) tmp = (z * z) + ((z * z) + ((z * z) + (x * y))); end
code[x_, y_, z_] := N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(N[(z * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
z \cdot z + \left(z \cdot z + \left(z \cdot z + x \cdot y\right)\right)
\end{array}
Initial program 98.7%
Final simplification98.7%
(FPCore (x y z) :precision binary64 (* x (+ y (* 3.0 (* z (/ z x))))))
double code(double x, double y, double z) {
return x * (y + (3.0 * (z * (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 + (3.0d0 * (z * (z / x))))
end function
public static double code(double x, double y, double z) {
return x * (y + (3.0 * (z * (z / x))));
}
def code(x, y, z): return x * (y + (3.0 * (z * (z / x))))
function code(x, y, z) return Float64(x * Float64(y + Float64(3.0 * Float64(z * Float64(z / x))))) end
function tmp = code(x, y, z) tmp = x * (y + (3.0 * (z * (z / x)))); end
code[x_, y_, z_] := N[(x * N[(y + N[(3.0 * N[(z * N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(y + 3 \cdot \left(z \cdot \frac{z}{x}\right)\right)
\end{array}
Initial program 98.7%
Taylor expanded in x around inf 96.6%
Simplified96.6%
unpow296.6%
associate-/l*97.0%
Applied egg-rr97.0%
Final simplification97.0%
(FPCore (x y z) :precision binary64 (* x (+ y (* 3.0 (/ z (/ x z))))))
double code(double x, double y, double z) {
return x * (y + (3.0 * (z / (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 + (3.0d0 * (z / (x / z))))
end function
public static double code(double x, double y, double z) {
return x * (y + (3.0 * (z / (x / z))));
}
def code(x, y, z): return x * (y + (3.0 * (z / (x / z))))
function code(x, y, z) return Float64(x * Float64(y + Float64(3.0 * Float64(z / Float64(x / z))))) end
function tmp = code(x, y, z) tmp = x * (y + (3.0 * (z / (x / z)))); end
code[x_, y_, z_] := N[(x * N[(y + N[(3.0 * N[(z / N[(x / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(y + 3 \cdot \frac{z}{\frac{x}{z}}\right)
\end{array}
Initial program 98.7%
Taylor expanded in x around inf 96.6%
Simplified96.6%
unpow296.6%
associate-/l*97.0%
Applied egg-rr97.0%
clear-num97.0%
un-div-inv97.0%
Applied egg-rr97.0%
Final simplification97.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 98.7%
associate-+l+98.7%
associate-+l+98.7%
fma-define99.5%
associate-+r+99.5%
distribute-lft-out99.5%
distribute-lft-out99.5%
remove-double-neg99.5%
unsub-neg99.5%
count-299.5%
neg-mul-199.5%
distribute-rgt-out--99.5%
metadata-eval99.5%
Simplified99.5%
add-sqr-sqrt99.4%
pow299.4%
associate-*r*99.4%
sqrt-prod99.4%
sqrt-prod46.4%
add-sqr-sqrt99.4%
Applied egg-rr99.4%
Taylor expanded in x around inf 60.1%
Final simplification60.1%
(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 2024053
(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)))