
(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 8 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 (if (<= y 5e+147) (* (/ (+ z y) z) x) (/ (* x (+ y z)) z)))
double code(double x, double y, double z) {
double tmp;
if (y <= 5e+147) {
tmp = ((z + y) / z) * x;
} else {
tmp = (x * (y + z)) / 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 <= 5d+147) then
tmp = ((z + y) / z) * x
else
tmp = (x * (y + z)) / z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= 5e+147) {
tmp = ((z + y) / z) * x;
} else {
tmp = (x * (y + z)) / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= 5e+147: tmp = ((z + y) / z) * x else: tmp = (x * (y + z)) / z return tmp
function code(x, y, z) tmp = 0.0 if (y <= 5e+147) tmp = Float64(Float64(Float64(z + y) / z) * x); else tmp = Float64(Float64(x * Float64(y + z)) / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= 5e+147) tmp = ((z + y) / z) * x; else tmp = (x * (y + z)) / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, 5e+147], N[(N[(N[(z + y), $MachinePrecision] / z), $MachinePrecision] * x), $MachinePrecision], N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 5 \cdot 10^{+147}:\\
\;\;\;\;\frac{z + y}{z} \cdot x\\
\mathbf{else}:\\
\;\;\;\;\frac{x \cdot \left(y + z\right)}{z}\\
\end{array}
\end{array}
if y < 5.0000000000000002e147Initial program 82.4%
Applied rewrites98.6%
if 5.0000000000000002e147 < y Initial program 94.5%
(FPCore (x y z)
:precision binary64
(if (<= z -2.2e+139)
(* 1.0 x)
(if (<= z 1.7e-293)
(* (/ y z) x)
(if (<= z 1.85e-14) (* (/ x z) y) (* 1.0 x)))))
double code(double x, double y, double z) {
double tmp;
if (z <= -2.2e+139) {
tmp = 1.0 * x;
} else if (z <= 1.7e-293) {
tmp = (y / z) * x;
} else if (z <= 1.85e-14) {
tmp = (x / z) * y;
} else {
tmp = 1.0 * 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 <= (-2.2d+139)) then
tmp = 1.0d0 * x
else if (z <= 1.7d-293) then
tmp = (y / z) * x
else if (z <= 1.85d-14) then
tmp = (x / z) * y
else
tmp = 1.0d0 * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= -2.2e+139) {
tmp = 1.0 * x;
} else if (z <= 1.7e-293) {
tmp = (y / z) * x;
} else if (z <= 1.85e-14) {
tmp = (x / z) * y;
} else {
tmp = 1.0 * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= -2.2e+139: tmp = 1.0 * x elif z <= 1.7e-293: tmp = (y / z) * x elif z <= 1.85e-14: tmp = (x / z) * y else: tmp = 1.0 * x return tmp
function code(x, y, z) tmp = 0.0 if (z <= -2.2e+139) tmp = Float64(1.0 * x); elseif (z <= 1.7e-293) tmp = Float64(Float64(y / z) * x); elseif (z <= 1.85e-14) tmp = Float64(Float64(x / z) * y); else tmp = Float64(1.0 * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= -2.2e+139) tmp = 1.0 * x; elseif (z <= 1.7e-293) tmp = (y / z) * x; elseif (z <= 1.85e-14) tmp = (x / z) * y; else tmp = 1.0 * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, -2.2e+139], N[(1.0 * x), $MachinePrecision], If[LessEqual[z, 1.7e-293], N[(N[(y / z), $MachinePrecision] * x), $MachinePrecision], If[LessEqual[z, 1.85e-14], N[(N[(x / z), $MachinePrecision] * y), $MachinePrecision], N[(1.0 * x), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2.2 \cdot 10^{+139}:\\
\;\;\;\;1 \cdot x\\
\mathbf{elif}\;z \leq 1.7 \cdot 10^{-293}:\\
\;\;\;\;\frac{y}{z} \cdot x\\
\mathbf{elif}\;z \leq 1.85 \cdot 10^{-14}:\\
\;\;\;\;\frac{x}{z} \cdot y\\
\mathbf{else}:\\
\;\;\;\;1 \cdot x\\
\end{array}
\end{array}
if z < -2.1999999999999999e139 or 1.85000000000000001e-14 < z Initial program 71.3%
Applied rewrites99.9%
Taylor expanded in y around 0
Applied rewrites77.0%
if -2.1999999999999999e139 < z < 1.7e-293Initial program 92.0%
Applied rewrites97.7%
Taylor expanded in y around 0
Applied rewrites21.7%
Taylor expanded in y around inf
lower-/.f6477.0
Applied rewrites77.0%
if 1.7e-293 < z < 1.85000000000000001e-14Initial program 95.3%
Applied rewrites87.4%
Taylor expanded in y around inf
associate-*l/N/A
lower-*.f64N/A
lower-/.f6482.2
Applied rewrites82.2%
(FPCore (x y z) :precision binary64 (if (or (<= z -2.15e+139) (not (<= z 1.85e-14))) (* 1.0 x) (/ (* y x) z)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -2.15e+139) || !(z <= 1.85e-14)) {
tmp = 1.0 * x;
} 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 ((z <= (-2.15d+139)) .or. (.not. (z <= 1.85d-14))) then
tmp = 1.0d0 * x
else
tmp = (y * x) / z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -2.15e+139) || !(z <= 1.85e-14)) {
tmp = 1.0 * x;
} else {
tmp = (y * x) / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -2.15e+139) or not (z <= 1.85e-14): tmp = 1.0 * x else: tmp = (y * x) / z return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -2.15e+139) || !(z <= 1.85e-14)) tmp = Float64(1.0 * x); else tmp = Float64(Float64(y * x) / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -2.15e+139) || ~((z <= 1.85e-14))) tmp = 1.0 * x; else tmp = (y * x) / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -2.15e+139], N[Not[LessEqual[z, 1.85e-14]], $MachinePrecision]], N[(1.0 * x), $MachinePrecision], N[(N[(y * x), $MachinePrecision] / z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2.15 \cdot 10^{+139} \lor \neg \left(z \leq 1.85 \cdot 10^{-14}\right):\\
\;\;\;\;1 \cdot x\\
\mathbf{else}:\\
\;\;\;\;\frac{y \cdot x}{z}\\
\end{array}
\end{array}
if z < -2.1499999999999999e139 or 1.85000000000000001e-14 < z Initial program 71.3%
Applied rewrites99.9%
Taylor expanded in y around 0
Applied rewrites77.0%
if -2.1499999999999999e139 < z < 1.85000000000000001e-14Initial program 93.4%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6477.2
Applied rewrites77.2%
Final simplification77.1%
(FPCore (x y z) :precision binary64 (if (or (<= z -2.15e+139) (not (<= z 1.85e-14))) (* 1.0 x) (* (/ x z) y)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -2.15e+139) || !(z <= 1.85e-14)) {
tmp = 1.0 * x;
} else {
tmp = (x / 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 ((z <= (-2.15d+139)) .or. (.not. (z <= 1.85d-14))) then
tmp = 1.0d0 * x
else
tmp = (x / z) * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -2.15e+139) || !(z <= 1.85e-14)) {
tmp = 1.0 * x;
} else {
tmp = (x / z) * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -2.15e+139) or not (z <= 1.85e-14): tmp = 1.0 * x else: tmp = (x / z) * y return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -2.15e+139) || !(z <= 1.85e-14)) tmp = Float64(1.0 * x); else tmp = Float64(Float64(x / z) * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -2.15e+139) || ~((z <= 1.85e-14))) tmp = 1.0 * x; else tmp = (x / z) * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -2.15e+139], N[Not[LessEqual[z, 1.85e-14]], $MachinePrecision]], N[(1.0 * x), $MachinePrecision], N[(N[(x / z), $MachinePrecision] * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2.15 \cdot 10^{+139} \lor \neg \left(z \leq 1.85 \cdot 10^{-14}\right):\\
\;\;\;\;1 \cdot x\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{z} \cdot y\\
\end{array}
\end{array}
if z < -2.1499999999999999e139 or 1.85000000000000001e-14 < z Initial program 71.3%
Applied rewrites99.9%
Taylor expanded in y around 0
Applied rewrites77.0%
if -2.1499999999999999e139 < z < 1.85000000000000001e-14Initial program 93.4%
Applied rewrites93.5%
Taylor expanded in y around inf
associate-*l/N/A
lower-*.f64N/A
lower-/.f6476.1
Applied rewrites76.1%
Final simplification76.4%
(FPCore (x y z) :precision binary64 (if (<= y 1e+128) (* (/ (+ z y) z) x) (fma (/ x z) y x)))
double code(double x, double y, double z) {
double tmp;
if (y <= 1e+128) {
tmp = ((z + y) / z) * x;
} else {
tmp = fma((x / z), y, x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 1e+128) tmp = Float64(Float64(Float64(z + y) / z) * x); else tmp = fma(Float64(x / z), y, x); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 1e+128], N[(N[(N[(z + y), $MachinePrecision] / z), $MachinePrecision] * x), $MachinePrecision], N[(N[(x / z), $MachinePrecision] * y + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 10^{+128}:\\
\;\;\;\;\frac{z + y}{z} \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{z}, y, x\right)\\
\end{array}
\end{array}
if y < 1.0000000000000001e128Initial program 82.9%
Applied rewrites98.6%
if 1.0000000000000001e128 < y Initial program 90.3%
Applied rewrites82.9%
lift-*.f64N/A
lift-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
div-addN/A
lift-/.f64N/A
*-inversesN/A
distribute-lft1-inN/A
lift-/.f64N/A
associate-*l/N/A
*-commutativeN/A
associate-*l/N/A
lift-/.f64N/A
lower-fma.f6493.4
Applied rewrites93.4%
(FPCore (x y z) :precision binary64 (if (<= y 1e+128) (fma (/ y z) x x) (fma (/ x z) y x)))
double code(double x, double y, double z) {
double tmp;
if (y <= 1e+128) {
tmp = fma((y / z), x, x);
} else {
tmp = fma((x / z), y, x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 1e+128) tmp = fma(Float64(y / z), x, x); else tmp = fma(Float64(x / z), y, x); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 1e+128], N[(N[(y / z), $MachinePrecision] * x + x), $MachinePrecision], N[(N[(x / z), $MachinePrecision] * y + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 10^{+128}:\\
\;\;\;\;\mathsf{fma}\left(\frac{y}{z}, x, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{z}, y, x\right)\\
\end{array}
\end{array}
if y < 1.0000000000000001e128Initial program 82.9%
Taylor expanded in x around 0
*-commutativeN/A
associate-*l/N/A
+-commutativeN/A
div-addN/A
*-inversesN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f64N/A
lower-/.f6498.6
Applied rewrites98.6%
if 1.0000000000000001e128 < y Initial program 90.3%
Applied rewrites82.9%
lift-*.f64N/A
lift-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
div-addN/A
lift-/.f64N/A
*-inversesN/A
distribute-lft1-inN/A
lift-/.f64N/A
associate-*l/N/A
*-commutativeN/A
associate-*l/N/A
lift-/.f64N/A
lower-fma.f6493.4
Applied rewrites93.4%
Final simplification97.8%
(FPCore (x y z) :precision binary64 (fma (/ y z) x x))
double code(double x, double y, double z) {
return fma((y / z), x, x);
}
function code(x, y, z) return fma(Float64(y / z), x, x) end
code[x_, y_, z_] := N[(N[(y / z), $MachinePrecision] * x + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{y}{z}, x, x\right)
\end{array}
Initial program 84.0%
Taylor expanded in x around 0
*-commutativeN/A
associate-*l/N/A
+-commutativeN/A
div-addN/A
*-inversesN/A
+-commutativeN/A
distribute-lft1-inN/A
lower-fma.f64N/A
lower-/.f6496.2
Applied rewrites96.2%
Final simplification96.2%
(FPCore (x y z) :precision binary64 (* 1.0 x))
double code(double x, double y, double z) {
return 1.0 * x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 1.0d0 * x
end function
public static double code(double x, double y, double z) {
return 1.0 * x;
}
def code(x, y, z): return 1.0 * x
function code(x, y, z) return Float64(1.0 * x) end
function tmp = code(x, y, z) tmp = 1.0 * x; end
code[x_, y_, z_] := N[(1.0 * x), $MachinePrecision]
\begin{array}{l}
\\
1 \cdot x
\end{array}
Initial program 84.0%
Applied rewrites96.2%
Taylor expanded in y around 0
Applied rewrites44.0%
(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 2024339
(FPCore (x y z)
:name "Numeric.SpecFunctions:choose from math-functions-0.1.5.2"
:precision binary64
:alt
(! :herbie-platform default (/ x (/ z (+ y z))))
(/ (* x (+ y z)) z))