
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(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 * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))
double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + log(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 * 0.5d0) + (y * ((1.0d0 - z) + log(z)))
end function
public static double code(double x, double y, double z) {
return (x * 0.5) + (y * ((1.0 - z) + Math.log(z)));
}
def code(x, y, z): return (x * 0.5) + (y * ((1.0 - z) + math.log(z)))
function code(x, y, z) return Float64(Float64(x * 0.5) + Float64(y * Float64(Float64(1.0 - z) + log(z)))) end
function tmp = code(x, y, z) tmp = (x * 0.5) + (y * ((1.0 - z) + log(z))); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] + N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 + y \cdot \left(\left(1 - z\right) + \log z\right)
\end{array}
(FPCore (x y z) :precision binary64 (fma 0.5 x (fma y (- (log z) z) y)))
double code(double x, double y, double z) {
return fma(0.5, x, fma(y, (log(z) - z), y));
}
function code(x, y, z) return fma(0.5, x, fma(y, Float64(log(z) - z), y)) end
code[x_, y_, z_] := N[(0.5 * x + N[(y * N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.5, x, \mathsf{fma}\left(y, \log z - z, y\right)\right)
\end{array}
Initial program 99.9%
lift-*.f64N/A
lift--.f64N/A
lift-log.f64N/A
lift-+.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied egg-rr99.9%
Taylor expanded in z around 0
+-commutativeN/A
associate-+l+N/A
mul-1-negN/A
distribute-rgt-neg-inN/A
distribute-lft-inN/A
sub-negN/A
lower-fma.f64N/A
associate--l+N/A
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
lower--.f64N/A
lower-log.f6499.9
Simplified99.9%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* y (+ (log z) (- 1.0 z)))) (t_1 (- (* y z)))) (if (<= t_0 -1e+189) t_1 (if (<= t_0 4e+90) (* 0.5 x) t_1))))
double code(double x, double y, double z) {
double t_0 = y * (log(z) + (1.0 - z));
double t_1 = -(y * z);
double tmp;
if (t_0 <= -1e+189) {
tmp = t_1;
} else if (t_0 <= 4e+90) {
tmp = 0.5 * x;
} else {
tmp = t_1;
}
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) :: t_1
real(8) :: tmp
t_0 = y * (log(z) + (1.0d0 - z))
t_1 = -(y * z)
if (t_0 <= (-1d+189)) then
tmp = t_1
else if (t_0 <= 4d+90) then
tmp = 0.5d0 * x
else
tmp = t_1
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = y * (Math.log(z) + (1.0 - z));
double t_1 = -(y * z);
double tmp;
if (t_0 <= -1e+189) {
tmp = t_1;
} else if (t_0 <= 4e+90) {
tmp = 0.5 * x;
} else {
tmp = t_1;
}
return tmp;
}
def code(x, y, z): t_0 = y * (math.log(z) + (1.0 - z)) t_1 = -(y * z) tmp = 0 if t_0 <= -1e+189: tmp = t_1 elif t_0 <= 4e+90: tmp = 0.5 * x else: tmp = t_1 return tmp
function code(x, y, z) t_0 = Float64(y * Float64(log(z) + Float64(1.0 - z))) t_1 = Float64(-Float64(y * z)) tmp = 0.0 if (t_0 <= -1e+189) tmp = t_1; elseif (t_0 <= 4e+90) tmp = Float64(0.5 * x); else tmp = t_1; end return tmp end
function tmp_2 = code(x, y, z) t_0 = y * (log(z) + (1.0 - z)); t_1 = -(y * z); tmp = 0.0; if (t_0 <= -1e+189) tmp = t_1; elseif (t_0 <= 4e+90) tmp = 0.5 * x; else tmp = t_1; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = (-N[(y * z), $MachinePrecision])}, If[LessEqual[t$95$0, -1e+189], t$95$1, If[LessEqual[t$95$0, 4e+90], N[(0.5 * x), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y \cdot \left(\log z + \left(1 - z\right)\right)\\
t_1 := -y \cdot z\\
\mathbf{if}\;t\_0 \leq -1 \cdot 10^{+189}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 4 \cdot 10^{+90}:\\
\;\;\;\;0.5 \cdot x\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < -1e189 or 3.99999999999999987e90 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) Initial program 99.8%
Taylor expanded in z around inf
mul-1-negN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
lower-neg.f6465.7
Simplified65.7%
if -1e189 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < 3.99999999999999987e90Initial program 99.9%
Taylor expanded in x around inf
lower-*.f6464.1
Simplified64.1%
Final simplification64.8%
(FPCore (x y z) :precision binary64 (let* ((t_0 (fma y (- (log z) z) y))) (if (<= y -1.42e+122) t_0 (if (<= y 2.2e+34) (fma (- z) y (* 0.5 x)) t_0))))
double code(double x, double y, double z) {
double t_0 = fma(y, (log(z) - z), y);
double tmp;
if (y <= -1.42e+122) {
tmp = t_0;
} else if (y <= 2.2e+34) {
tmp = fma(-z, y, (0.5 * x));
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(y, Float64(log(z) - z), y) tmp = 0.0 if (y <= -1.42e+122) tmp = t_0; elseif (y <= 2.2e+34) tmp = fma(Float64(-z), y, Float64(0.5 * x)); else tmp = t_0; end return tmp end
code[x_, y_, z_] := Block[{t$95$0 = N[(y * N[(N[Log[z], $MachinePrecision] - z), $MachinePrecision] + y), $MachinePrecision]}, If[LessEqual[y, -1.42e+122], t$95$0, If[LessEqual[y, 2.2e+34], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(y, \log z - z, y\right)\\
\mathbf{if}\;y \leq -1.42 \cdot 10^{+122}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 2.2 \cdot 10^{+34}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1.42000000000000005e122 or 2.2000000000000002e34 < y Initial program 99.8%
Taylor expanded in x around 0
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6487.9
Simplified87.9%
if -1.42000000000000005e122 < y < 2.2000000000000002e34Initial program 99.9%
lift-*.f64N/A
lift--.f64N/A
lift-log.f64N/A
lift-+.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied egg-rr99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6489.3
Simplified89.3%
Final simplification88.7%
(FPCore (x y z) :precision binary64 (if (<= z 5.8e-142) (* x (- 0.5 (/ (* y z) x))) (if (<= z 2.4e-28) (fma y (log z) y) (fma (- z) y (* 0.5 x)))))
double code(double x, double y, double z) {
double tmp;
if (z <= 5.8e-142) {
tmp = x * (0.5 - ((y * z) / x));
} else if (z <= 2.4e-28) {
tmp = fma(y, log(z), y);
} else {
tmp = fma(-z, y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 5.8e-142) tmp = Float64(x * Float64(0.5 - Float64(Float64(y * z) / x))); elseif (z <= 2.4e-28) tmp = fma(y, log(z), y); else tmp = fma(Float64(-z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 5.8e-142], N[(x * N[(0.5 - N[(N[(y * z), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 2.4e-28], N[(y * N[Log[z], $MachinePrecision] + y), $MachinePrecision], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 5.8 \cdot 10^{-142}:\\
\;\;\;\;x \cdot \left(0.5 - \frac{y \cdot z}{x}\right)\\
\mathbf{elif}\;z \leq 2.4 \cdot 10^{-28}:\\
\;\;\;\;\mathsf{fma}\left(y, \log z, y\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if z < 5.7999999999999998e-142Initial program 99.8%
lift--.f64N/A
lift-log.f64N/A
lift-+.f64N/A
*-commutativeN/A
lift-+.f64N/A
flip-+N/A
associate-*l/N/A
clear-numN/A
lower-/.f64N/A
lower-/.f64N/A
lift--.f64N/A
associate--l-N/A
lower--.f64N/A
lower-+.f64N/A
lower-*.f64N/A
Applied egg-rr99.7%
Taylor expanded in z around inf
lower-/.f64N/A
lower-*.f6457.8
Simplified57.8%
Taylor expanded in x around inf
metadata-evalN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
metadata-evalN/A
sub-negN/A
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
+-commutativeN/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6457.9
Simplified57.9%
if 5.7999999999999998e-142 < z < 2.4000000000000002e-28Initial program 99.7%
Taylor expanded in x around 0
sub-negN/A
associate-+r+N/A
mul-1-negN/A
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64N/A
lower-log.f6464.3
Simplified64.3%
Taylor expanded in z around 0
+-commutativeN/A
lower-fma.f64N/A
lower-log.f6464.3
Simplified64.3%
if 2.4000000000000002e-28 < z Initial program 100.0%
lift-*.f64N/A
lift--.f64N/A
lift-log.f64N/A
lift-+.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
Applied egg-rr100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6495.9
Simplified95.9%
Final simplification80.4%
(FPCore (x y z) :precision binary64 (if (<= z 0.275) (fma y (log z) (fma 0.5 x y)) (fma (- z) y (* 0.5 x))))
double code(double x, double y, double z) {
double tmp;
if (z <= 0.275) {
tmp = fma(y, log(z), fma(0.5, x, y));
} else {
tmp = fma(-z, y, (0.5 * x));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (z <= 0.275) tmp = fma(y, log(z), fma(0.5, x, y)); else tmp = fma(Float64(-z), y, Float64(0.5 * x)); end return tmp end
code[x_, y_, z_] := If[LessEqual[z, 0.275], N[(y * N[Log[z], $MachinePrecision] + N[(0.5 * x + y), $MachinePrecision]), $MachinePrecision], N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 0.275:\\
\;\;\;\;\mathsf{fma}\left(y, \log z, \mathsf{fma}\left(0.5, x, y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)\\
\end{array}
\end{array}
if z < 0.27500000000000002Initial program 99.8%
Taylor expanded in z around 0
distribute-lft-inN/A
*-rgt-identityN/A
associate-+r+N/A
+-commutativeN/A
lower-fma.f64N/A
lower-log.f64N/A
lower-fma.f6499.0
Simplified99.0%
if 0.27500000000000002 < z Initial program 100.0%
lift-*.f64N/A
lift--.f64N/A
lift-log.f64N/A
lift-+.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f64100.0
Applied egg-rr100.0%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6498.1
Simplified98.1%
Final simplification98.5%
(FPCore (x y z) :precision binary64 (fma (- z) y (* 0.5 x)))
double code(double x, double y, double z) {
return fma(-z, y, (0.5 * x));
}
function code(x, y, z) return fma(Float64(-z), y, Float64(0.5 * x)) end
code[x_, y_, z_] := N[((-z) * y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-z, y, 0.5 \cdot x\right)
\end{array}
Initial program 99.9%
lift-*.f64N/A
lift--.f64N/A
lift-log.f64N/A
lift-+.f64N/A
lift-*.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied egg-rr99.9%
Taylor expanded in z around inf
mul-1-negN/A
lower-neg.f6476.6
Simplified76.6%
Final simplification76.6%
(FPCore (x y z) :precision binary64 (* 0.5 x))
double code(double x, double y, double z) {
return 0.5 * x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 0.5d0 * x
end function
public static double code(double x, double y, double z) {
return 0.5 * x;
}
def code(x, y, z): return 0.5 * x
function code(x, y, z) return Float64(0.5 * x) end
function tmp = code(x, y, z) tmp = 0.5 * x; end
code[x_, y_, z_] := N[(0.5 * x), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot x
\end{array}
Initial program 99.9%
Taylor expanded in x around inf
lower-*.f6440.5
Simplified40.5%
(FPCore (x y z) :precision binary64 (- (+ y (* 0.5 x)) (* y (- z (log z)))))
double code(double x, double y, double z) {
return (y + (0.5 * x)) - (y * (z - log(z)));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (y + (0.5d0 * x)) - (y * (z - log(z)))
end function
public static double code(double x, double y, double z) {
return (y + (0.5 * x)) - (y * (z - Math.log(z)));
}
def code(x, y, z): return (y + (0.5 * x)) - (y * (z - math.log(z)))
function code(x, y, z) return Float64(Float64(y + Float64(0.5 * x)) - Float64(y * Float64(z - log(z)))) end
function tmp = code(x, y, z) tmp = (y + (0.5 * x)) - (y * (z - log(z))); end
code[x_, y_, z_] := N[(N[(y + N[(0.5 * x), $MachinePrecision]), $MachinePrecision] - N[(y * N[(z - N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(y + 0.5 \cdot x\right) - y \cdot \left(z - \log z\right)
\end{array}
herbie shell --seed 2024212
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:alt
(! :herbie-platform default (- (+ y (* 1/2 x)) (* y (- z (log z)))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))