
(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 10 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 (+ (* (+ (log z) (- 1.0 z)) y) (* 0.5 x)))
double code(double x, double y, double z) {
return ((log(z) + (1.0 - z)) * y) + (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 = ((log(z) + (1.0d0 - z)) * y) + (0.5d0 * x)
end function
public static double code(double x, double y, double z) {
return ((Math.log(z) + (1.0 - z)) * y) + (0.5 * x);
}
def code(x, y, z): return ((math.log(z) + (1.0 - z)) * y) + (0.5 * x)
function code(x, y, z) return Float64(Float64(Float64(log(z) + Float64(1.0 - z)) * y) + Float64(0.5 * x)) end
function tmp = code(x, y, z) tmp = ((log(z) + (1.0 - z)) * y) + (0.5 * x); end
code[x_, y_, z_] := N[(N[(N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision] + N[(0.5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\log z + \left(1 - z\right)\right) \cdot y + 0.5 \cdot x
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* (+ (log z) (- 1.0 z)) y)) (t_1 (* (- z) y))) (if (<= t_0 -1e+85) t_1 (if (<= t_0 5e+142) (* 0.5 x) t_1))))
double code(double x, double y, double z) {
double t_0 = (log(z) + (1.0 - z)) * y;
double t_1 = -z * y;
double tmp;
if (t_0 <= -1e+85) {
tmp = t_1;
} else if (t_0 <= 5e+142) {
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 = (log(z) + (1.0d0 - z)) * y
t_1 = -z * y
if (t_0 <= (-1d+85)) then
tmp = t_1
else if (t_0 <= 5d+142) 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 = (Math.log(z) + (1.0 - z)) * y;
double t_1 = -z * y;
double tmp;
if (t_0 <= -1e+85) {
tmp = t_1;
} else if (t_0 <= 5e+142) {
tmp = 0.5 * x;
} else {
tmp = t_1;
}
return tmp;
}
def code(x, y, z): t_0 = (math.log(z) + (1.0 - z)) * y t_1 = -z * y tmp = 0 if t_0 <= -1e+85: tmp = t_1 elif t_0 <= 5e+142: tmp = 0.5 * x else: tmp = t_1 return tmp
function code(x, y, z) t_0 = Float64(Float64(log(z) + Float64(1.0 - z)) * y) t_1 = Float64(Float64(-z) * y) tmp = 0.0 if (t_0 <= -1e+85) tmp = t_1; elseif (t_0 <= 5e+142) tmp = Float64(0.5 * x); else tmp = t_1; end return tmp end
function tmp_2 = code(x, y, z) t_0 = (log(z) + (1.0 - z)) * y; t_1 = -z * y; tmp = 0.0; if (t_0 <= -1e+85) tmp = t_1; elseif (t_0 <= 5e+142) tmp = 0.5 * x; else tmp = t_1; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(N[Log[z], $MachinePrecision] + N[(1.0 - z), $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision]}, Block[{t$95$1 = N[((-z) * y), $MachinePrecision]}, If[LessEqual[t$95$0, -1e+85], t$95$1, If[LessEqual[t$95$0, 5e+142], N[(0.5 * x), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\log z + \left(1 - z\right)\right) \cdot y\\
t_1 := \left(-z\right) \cdot y\\
\mathbf{if}\;t\_0 \leq -1 \cdot 10^{+85}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 5 \cdot 10^{+142}:\\
\;\;\;\;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))) < -1e85 or 5.0000000000000001e142 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) Initial program 99.9%
Taylor expanded in z around inf
mul-1-negN/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6458.5
Applied rewrites58.5%
if -1e85 < (*.f64 y (+.f64 (-.f64 #s(literal 1 binary64) z) (log.f64 z))) < 5.0000000000000001e142Initial program 99.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6461.6
Applied rewrites61.6%
Final simplification60.2%
(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 2024230
(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)))))