
(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 7 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 -2e+19) (fma y (/ x z) x) (/ x (/ z (+ y z)))))
double code(double x, double y, double z) {
double tmp;
if (y <= -2e+19) {
tmp = fma(y, (x / z), x);
} else {
tmp = x / (z / (y + z));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -2e+19) tmp = fma(y, Float64(x / z), x); else tmp = Float64(x / Float64(z / Float64(y + z))); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -2e+19], N[(y * N[(x / z), $MachinePrecision] + x), $MachinePrecision], N[(x / N[(z / N[(y + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2 \cdot 10^{+19}:\\
\;\;\;\;\mathsf{fma}\left(y, \frac{x}{z}, x\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{\frac{z}{y + z}}\\
\end{array}
\end{array}
if y < -2e19Initial program 91.4%
Taylor expanded in x around 0
associate-/l*N/A
+-commutativeN/A
*-lft-identityN/A
metadata-evalN/A
cancel-sign-sub-invN/A
div-subN/A
*-inversesN/A
associate-*r/N/A
distribute-rgt-out--N/A
*-lft-identityN/A
mul-1-negN/A
cancel-sign-subN/A
distribute-rgt1-inN/A
distribute-lft1-inN/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6489.9
Applied rewrites89.9%
lift-/.f64N/A
*-commutativeN/A
lift-/.f64N/A
div-invN/A
lift-/.f64N/A
associate-*l*N/A
lower-fma.f64N/A
*-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-/.f6499.8
Applied rewrites99.8%
if -2e19 < y Initial program 84.7%
lift-+.f64N/A
associate-/l*N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
lower-/.f6498.9
Applied rewrites98.9%
(FPCore (x y z) :precision binary64 (if (<= y -2.1e-8) (* y (/ x z)) (if (<= y 1.06e+26) x (/ (* y x) z))))
double code(double x, double y, double z) {
double tmp;
if (y <= -2.1e-8) {
tmp = y * (x / z);
} else if (y <= 1.06e+26) {
tmp = 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 (y <= (-2.1d-8)) then
tmp = y * (x / z)
else if (y <= 1.06d+26) then
tmp = 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 (y <= -2.1e-8) {
tmp = y * (x / z);
} else if (y <= 1.06e+26) {
tmp = x;
} else {
tmp = (y * x) / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -2.1e-8: tmp = y * (x / z) elif y <= 1.06e+26: tmp = x else: tmp = (y * x) / z return tmp
function code(x, y, z) tmp = 0.0 if (y <= -2.1e-8) tmp = Float64(y * Float64(x / z)); elseif (y <= 1.06e+26) tmp = x; else tmp = Float64(Float64(y * x) / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -2.1e-8) tmp = y * (x / z); elseif (y <= 1.06e+26) tmp = x; else tmp = (y * x) / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -2.1e-8], N[(y * N[(x / z), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.06e+26], x, N[(N[(y * x), $MachinePrecision] / z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.1 \cdot 10^{-8}:\\
\;\;\;\;y \cdot \frac{x}{z}\\
\mathbf{elif}\;y \leq 1.06 \cdot 10^{+26}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;\frac{y \cdot x}{z}\\
\end{array}
\end{array}
if y < -2.09999999999999994e-8Initial program 92.6%
Taylor expanded in y around inf
lower-/.f64N/A
lower-*.f6476.7
Applied rewrites76.7%
*-commutativeN/A
associate-*l/N/A
div-invN/A
lift-/.f64N/A
associate-*l*N/A
lower-*.f64N/A
*-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-/.f6478.0
Applied rewrites78.0%
if -2.09999999999999994e-8 < y < 1.05999999999999997e26Initial program 80.9%
lift-+.f64N/A
associate-/l*N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
Applied rewrites78.7%
/-rgt-identity78.7
Applied rewrites78.7%
if 1.05999999999999997e26 < y Initial program 90.6%
Taylor expanded in y around inf
lower-/.f64N/A
lower-*.f6477.5
Applied rewrites77.5%
Final simplification78.3%
(FPCore (x y z) :precision binary64 (if (<= y -2.1e-8) (* y (/ x z)) (if (<= y 1.06e+26) x (* x (/ y z)))))
double code(double x, double y, double z) {
double tmp;
if (y <= -2.1e-8) {
tmp = y * (x / z);
} else if (y <= 1.06e+26) {
tmp = x;
} 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 <= (-2.1d-8)) then
tmp = y * (x / z)
else if (y <= 1.06d+26) then
tmp = x
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 <= -2.1e-8) {
tmp = y * (x / z);
} else if (y <= 1.06e+26) {
tmp = x;
} else {
tmp = x * (y / z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -2.1e-8: tmp = y * (x / z) elif y <= 1.06e+26: tmp = x else: tmp = x * (y / z) return tmp
function code(x, y, z) tmp = 0.0 if (y <= -2.1e-8) tmp = Float64(y * Float64(x / z)); elseif (y <= 1.06e+26) tmp = x; else tmp = Float64(x * Float64(y / z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -2.1e-8) tmp = y * (x / z); elseif (y <= 1.06e+26) tmp = x; else tmp = x * (y / z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -2.1e-8], N[(y * N[(x / z), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.06e+26], x, N[(x * N[(y / z), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.1 \cdot 10^{-8}:\\
\;\;\;\;y \cdot \frac{x}{z}\\
\mathbf{elif}\;y \leq 1.06 \cdot 10^{+26}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;x \cdot \frac{y}{z}\\
\end{array}
\end{array}
if y < -2.09999999999999994e-8Initial program 92.6%
Taylor expanded in y around inf
lower-/.f64N/A
lower-*.f6476.7
Applied rewrites76.7%
*-commutativeN/A
associate-*l/N/A
div-invN/A
lift-/.f64N/A
associate-*l*N/A
lower-*.f64N/A
*-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-/.f6478.0
Applied rewrites78.0%
if -2.09999999999999994e-8 < y < 1.05999999999999997e26Initial program 80.9%
lift-+.f64N/A
associate-/l*N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
Applied rewrites78.7%
/-rgt-identity78.7
Applied rewrites78.7%
if 1.05999999999999997e26 < y Initial program 90.6%
Taylor expanded in y around inf
lower-/.f64N/A
lower-*.f6477.5
Applied rewrites77.5%
associate-*r/N/A
lift-/.f64N/A
*-commutativeN/A
lower-*.f6475.9
Applied rewrites75.9%
Final simplification77.9%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* y (/ x z)))) (if (<= y -2.1e-8) t_0 (if (<= y 1.06e+26) x t_0))))
double code(double x, double y, double z) {
double t_0 = y * (x / z);
double tmp;
if (y <= -2.1e-8) {
tmp = t_0;
} else if (y <= 1.06e+26) {
tmp = x;
} else {
tmp = t_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) :: t_0
real(8) :: tmp
t_0 = y * (x / z)
if (y <= (-2.1d-8)) then
tmp = t_0
else if (y <= 1.06d+26) then
tmp = x
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = y * (x / z);
double tmp;
if (y <= -2.1e-8) {
tmp = t_0;
} else if (y <= 1.06e+26) {
tmp = x;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = y * (x / z) tmp = 0 if y <= -2.1e-8: tmp = t_0 elif y <= 1.06e+26: tmp = x else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(y * Float64(x / z)) tmp = 0.0 if (y <= -2.1e-8) tmp = t_0; elseif (y <= 1.06e+26) tmp = x; else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = y * (x / z); tmp = 0.0; if (y <= -2.1e-8) tmp = t_0; elseif (y <= 1.06e+26) tmp = x; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(x / z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -2.1e-8], t$95$0, If[LessEqual[y, 1.06e+26], x, t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \frac{x}{z}\\
\mathbf{if}\;y \leq -2.1 \cdot 10^{-8}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 1.06 \cdot 10^{+26}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -2.09999999999999994e-8 or 1.05999999999999997e26 < y Initial program 91.6%
Taylor expanded in y around inf
lower-/.f64N/A
lower-*.f6477.1
Applied rewrites77.1%
*-commutativeN/A
associate-*l/N/A
div-invN/A
lift-/.f64N/A
associate-*l*N/A
lower-*.f64N/A
*-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-/.f6476.5
Applied rewrites76.5%
if -2.09999999999999994e-8 < y < 1.05999999999999997e26Initial program 80.9%
lift-+.f64N/A
associate-/l*N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
Taylor expanded in z around inf
Applied rewrites78.7%
/-rgt-identity78.7
Applied rewrites78.7%
(FPCore (x y z) :precision binary64 (if (<= y -500000000.0) (fma y (/ x z) x) (fma x (/ y z) x)))
double code(double x, double y, double z) {
double tmp;
if (y <= -500000000.0) {
tmp = fma(y, (x / z), x);
} else {
tmp = fma(x, (y / z), x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -500000000.0) tmp = fma(y, Float64(x / z), x); else tmp = fma(x, Float64(y / z), x); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -500000000.0], N[(y * N[(x / z), $MachinePrecision] + x), $MachinePrecision], N[(x * N[(y / z), $MachinePrecision] + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -500000000:\\
\;\;\;\;\mathsf{fma}\left(y, \frac{x}{z}, x\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{y}{z}, x\right)\\
\end{array}
\end{array}
if y < -5e8Initial program 92.1%
Taylor expanded in x around 0
associate-/l*N/A
+-commutativeN/A
*-lft-identityN/A
metadata-evalN/A
cancel-sign-sub-invN/A
div-subN/A
*-inversesN/A
associate-*r/N/A
distribute-rgt-out--N/A
*-lft-identityN/A
mul-1-negN/A
cancel-sign-subN/A
distribute-rgt1-inN/A
distribute-lft1-inN/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6490.7
Applied rewrites90.7%
lift-/.f64N/A
*-commutativeN/A
lift-/.f64N/A
div-invN/A
lift-/.f64N/A
associate-*l*N/A
lower-fma.f64N/A
*-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-/.f6499.8
Applied rewrites99.8%
if -5e8 < y Initial program 84.3%
Taylor expanded in x around 0
associate-/l*N/A
+-commutativeN/A
*-lft-identityN/A
metadata-evalN/A
cancel-sign-sub-invN/A
div-subN/A
*-inversesN/A
associate-*r/N/A
distribute-rgt-out--N/A
*-lft-identityN/A
mul-1-negN/A
cancel-sign-subN/A
distribute-rgt1-inN/A
distribute-lft1-inN/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6498.4
Applied rewrites98.4%
(FPCore (x y z) :precision binary64 (if (<= y -3.2e+197) (* y (/ x z)) (fma x (/ y z) x)))
double code(double x, double y, double z) {
double tmp;
if (y <= -3.2e+197) {
tmp = y * (x / z);
} else {
tmp = fma(x, (y / z), x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -3.2e+197) tmp = Float64(y * Float64(x / z)); else tmp = fma(x, Float64(y / z), x); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -3.2e+197], N[(y * N[(x / z), $MachinePrecision]), $MachinePrecision], N[(x * N[(y / z), $MachinePrecision] + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3.2 \cdot 10^{+197}:\\
\;\;\;\;y \cdot \frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, \frac{y}{z}, x\right)\\
\end{array}
\end{array}
if y < -3.1999999999999998e197Initial program 91.1%
Taylor expanded in y around inf
lower-/.f64N/A
lower-*.f6491.1
Applied rewrites91.1%
*-commutativeN/A
associate-*l/N/A
div-invN/A
lift-/.f64N/A
associate-*l*N/A
lower-*.f64N/A
*-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-/.f6499.3
Applied rewrites99.3%
if -3.1999999999999998e197 < y Initial program 85.8%
Taylor expanded in x around 0
associate-/l*N/A
+-commutativeN/A
*-lft-identityN/A
metadata-evalN/A
cancel-sign-sub-invN/A
div-subN/A
*-inversesN/A
associate-*r/N/A
distribute-rgt-out--N/A
*-lft-identityN/A
mul-1-negN/A
cancel-sign-subN/A
distribute-rgt1-inN/A
distribute-lft1-inN/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6498.2
Applied rewrites98.2%
(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 86.2%
lift-+.f64N/A
associate-/l*N/A
clear-numN/A
un-div-invN/A
lower-/.f64N/A
lower-/.f6497.2
Applied rewrites97.2%
Taylor expanded in z around inf
Applied rewrites50.9%
/-rgt-identity50.9
Applied rewrites50.9%
(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 2024214
(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))