
(FPCore (x y z) :precision binary64 (/ (* x (+ y z)) z))
double code(double x, double y, double z) {
return (x * (y + 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
end function
public static double code(double x, double y, double z) {
return (x * (y + z)) / z;
}
def code(x, y, z): return (x * (y + z)) / z
function code(x, y, z) return Float64(Float64(x * Float64(y + z)) / z) end
function tmp = code(x, y, z) tmp = (x * (y + z)) / z; end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(y + z\right)}{z}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (/ (* x (+ y z)) z))
double code(double x, double y, double z) {
return (x * (y + 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
end function
public static double code(double x, double y, double z) {
return (x * (y + z)) / z;
}
def code(x, y, z): return (x * (y + z)) / z
function code(x, y, z) return Float64(Float64(x * Float64(y + z)) / z) end
function tmp = code(x, y, z) tmp = (x * (y + z)) / z; end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}
\\
\frac{x \cdot \left(y + z\right)}{z}
\end{array}
(FPCore (x y z) :precision binary64 (fma x (/ y z) x))
double code(double x, double y, double z) {
return fma(x, (y / z), x);
}
function code(x, y, z) return fma(x, Float64(y / z), x) end
code[x_, y_, z_] := N[(x * N[(y / z), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, \frac{y}{z}, x\right)
\end{array}
Initial program 84.3%
associate-*l/80.7%
distribute-lft-in76.9%
cancel-sign-sub76.9%
associate-*l/72.8%
associate-*r/77.4%
distribute-frac-neg77.4%
fma-neg77.4%
distribute-rgt-neg-out77.4%
neg-mul-177.4%
associate-/l*76.3%
associate-*l/77.1%
neg-mul-177.1%
remove-double-neg77.1%
associate-/r/97.7%
*-inverses97.7%
*-lft-identity97.7%
Simplified97.7%
Final simplification97.7%
(FPCore (x y z) :precision binary64 (if (<= z -4.5e+21) x (if (<= z 1.45e+102) (* x (/ y z)) x)))
double code(double x, double y, double z) {
double tmp;
if (z <= -4.5e+21) {
tmp = x;
} else if (z <= 1.45e+102) {
tmp = x * (y / 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 (z <= (-4.5d+21)) then
tmp = x
else if (z <= 1.45d+102) then
tmp = x * (y / z)
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= -4.5e+21) {
tmp = x;
} else if (z <= 1.45e+102) {
tmp = x * (y / z);
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= -4.5e+21: tmp = x elif z <= 1.45e+102: tmp = x * (y / z) else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if (z <= -4.5e+21) tmp = x; elseif (z <= 1.45e+102) tmp = Float64(x * Float64(y / z)); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= -4.5e+21) tmp = x; elseif (z <= 1.45e+102) tmp = x * (y / z); else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, -4.5e+21], x, If[LessEqual[z, 1.45e+102], N[(x * N[(y / z), $MachinePrecision]), $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -4.5 \cdot 10^{+21}:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 1.45 \cdot 10^{+102}:\\
\;\;\;\;x \cdot \frac{y}{z}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -4.5e21 or 1.4500000000000001e102 < z Initial program 72.3%
*-commutative72.3%
associate-*l/99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in y around 0 79.9%
if -4.5e21 < z < 1.4500000000000001e102Initial program 92.7%
*-commutative92.7%
associate-*l/96.1%
*-commutative96.1%
Simplified96.1%
Taylor expanded in y around inf 72.0%
associate-*r/72.9%
Simplified72.9%
Final simplification75.7%
(FPCore (x y z) :precision binary64 (* x (/ (+ y z) z)))
double code(double x, double y, double z) {
return x * ((y + 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)
end function
public static double code(double x, double y, double z) {
return x * ((y + z) / z);
}
def code(x, y, z): return x * ((y + z) / z)
function code(x, y, z) return Float64(x * Float64(Float64(y + z) / z)) end
function tmp = code(x, y, z) tmp = x * ((y + z) / z); end
code[x_, y_, z_] := N[(x * N[(N[(y + z), $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \frac{y + z}{z}
\end{array}
Initial program 84.3%
*-commutative84.3%
associate-*l/97.7%
*-commutative97.7%
Simplified97.7%
Final simplification97.7%
(FPCore (x y z) :precision binary64 (+ x (* x (/ y z))))
double code(double x, double y, double z) {
return x + (x * (y / 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 + (x * (y / z))
end function
public static double code(double x, double y, double z) {
return x + (x * (y / z));
}
def code(x, y, z): return x + (x * (y / z))
function code(x, y, z) return Float64(x + Float64(x * Float64(y / z))) end
function tmp = code(x, y, z) tmp = x + (x * (y / z)); end
code[x_, y_, z_] := N[(x + N[(x * N[(y / z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + x \cdot \frac{y}{z}
\end{array}
Initial program 84.3%
associate-*l/80.7%
distribute-lft-in76.9%
cancel-sign-sub76.9%
associate-*l/72.8%
associate-*r/77.4%
distribute-frac-neg77.4%
fma-neg77.4%
distribute-rgt-neg-out77.4%
neg-mul-177.4%
associate-/l*76.3%
associate-*l/77.1%
neg-mul-177.1%
remove-double-neg77.1%
associate-/r/97.7%
*-inverses97.7%
*-lft-identity97.7%
Simplified97.7%
fma-undefine97.7%
Applied egg-rr97.7%
Final simplification97.7%
(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 84.3%
*-commutative84.3%
associate-*l/97.7%
*-commutative97.7%
Simplified97.7%
Taylor expanded in y around 0 47.2%
Final simplification47.2%
(FPCore (x y z) :precision binary64 (/ x (/ z (+ y z))))
double code(double x, double y, double z) {
return x / (z / (y + 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 / (z / (y + z))
end function
public static double code(double x, double y, double z) {
return x / (z / (y + z));
}
def code(x, y, z): return x / (z / (y + z))
function code(x, y, z) return Float64(x / Float64(z / Float64(y + z))) end
function tmp = code(x, y, z) tmp = x / (z / (y + z)); end
code[x_, y_, z_] := N[(x / N[(z / N[(y + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{\frac{z}{y + z}}
\end{array}
herbie shell --seed 2024034
(FPCore (x y z)
:name "Numeric.SpecFunctions:choose from math-functions-0.1.5.2"
:precision binary64
:herbie-target
(/ x (/ z (+ y z)))
(/ (* x (+ y z)) z))