
(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 8 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 y (+ (- 1.0 z) (log z)) (* x 0.5)))
double code(double x, double y, double z) {
return fma(y, ((1.0 - z) + log(z)), (x * 0.5));
}
function code(x, y, z) return fma(y, Float64(Float64(1.0 - z) + log(z)), Float64(x * 0.5)) end
code[x_, y_, z_] := N[(y * N[(N[(1.0 - z), $MachinePrecision] + N[Log[z], $MachinePrecision]), $MachinePrecision] + N[(x * 0.5), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, \left(1 - z\right) + \log z, x \cdot 0.5\right)
\end{array}
Initial program 99.9%
+-commutative99.9%
fma-define99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (x y z)
:precision binary64
(if (or (<= z 7.5e-174)
(not (or (<= z 2.16e-103) (and (not (<= z 6.2e-63)) (<= z 2.7e-34)))))
(- (* x 0.5) (* y z))
(* y (+ 1.0 (log z)))))
double code(double x, double y, double z) {
double tmp;
if ((z <= 7.5e-174) || !((z <= 2.16e-103) || (!(z <= 6.2e-63) && (z <= 2.7e-34)))) {
tmp = (x * 0.5) - (y * z);
} else {
tmp = y * (1.0 + log(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 <= 7.5d-174) .or. (.not. (z <= 2.16d-103) .or. (.not. (z <= 6.2d-63)) .and. (z <= 2.7d-34))) then
tmp = (x * 0.5d0) - (y * z)
else
tmp = y * (1.0d0 + log(z))
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= 7.5e-174) || !((z <= 2.16e-103) || (!(z <= 6.2e-63) && (z <= 2.7e-34)))) {
tmp = (x * 0.5) - (y * z);
} else {
tmp = y * (1.0 + Math.log(z));
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= 7.5e-174) or not ((z <= 2.16e-103) or (not (z <= 6.2e-63) and (z <= 2.7e-34))): tmp = (x * 0.5) - (y * z) else: tmp = y * (1.0 + math.log(z)) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= 7.5e-174) || !((z <= 2.16e-103) || (!(z <= 6.2e-63) && (z <= 2.7e-34)))) tmp = Float64(Float64(x * 0.5) - Float64(y * z)); else tmp = Float64(y * Float64(1.0 + log(z))); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= 7.5e-174) || ~(((z <= 2.16e-103) || (~((z <= 6.2e-63)) && (z <= 2.7e-34))))) tmp = (x * 0.5) - (y * z); else tmp = y * (1.0 + log(z)); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, 7.5e-174], N[Not[Or[LessEqual[z, 2.16e-103], And[N[Not[LessEqual[z, 6.2e-63]], $MachinePrecision], LessEqual[z, 2.7e-34]]]], $MachinePrecision]], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision], N[(y * N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 7.5 \cdot 10^{-174} \lor \neg \left(z \leq 2.16 \cdot 10^{-103} \lor \neg \left(z \leq 6.2 \cdot 10^{-63}\right) \land z \leq 2.7 \cdot 10^{-34}\right):\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(1 + \log z\right)\\
\end{array}
\end{array}
if z < 7.5000000000000003e-174 or 2.1599999999999999e-103 < z < 6.19999999999999968e-63 or 2.70000000000000017e-34 < z Initial program 100.0%
Taylor expanded in z around inf 89.4%
mul-1-neg89.4%
distribute-rgt-neg-in89.4%
Simplified89.4%
if 7.5000000000000003e-174 < z < 2.1599999999999999e-103 or 6.19999999999999968e-63 < z < 2.70000000000000017e-34Initial program 99.6%
add-sqr-sqrt54.3%
pow254.3%
Applied egg-rr54.3%
Taylor expanded in x around 0 70.3%
Taylor expanded in z around 0 70.3%
Final simplification85.4%
(FPCore (x y z) :precision binary64 (if (or (<= y -1.76e+45) (not (<= y 1.15e+119))) (* y (- (+ 1.0 (log z)) z)) (- (* x 0.5) (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1.76e+45) || !(y <= 1.15e+119)) {
tmp = y * ((1.0 + log(z)) - z);
} else {
tmp = (x * 0.5) - (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 <= (-1.76d+45)) .or. (.not. (y <= 1.15d+119))) then
tmp = y * ((1.0d0 + log(z)) - z)
else
tmp = (x * 0.5d0) - (y * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -1.76e+45) || !(y <= 1.15e+119)) {
tmp = y * ((1.0 + Math.log(z)) - z);
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -1.76e+45) or not (y <= 1.15e+119): tmp = y * ((1.0 + math.log(z)) - z) else: tmp = (x * 0.5) - (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -1.76e+45) || !(y <= 1.15e+119)) tmp = Float64(y * Float64(Float64(1.0 + log(z)) - z)); else tmp = Float64(Float64(x * 0.5) - Float64(y * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -1.76e+45) || ~((y <= 1.15e+119))) tmp = y * ((1.0 + log(z)) - z); else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.76e+45], N[Not[LessEqual[y, 1.15e+119]], $MachinePrecision]], N[(y * N[(N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision] - z), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.76 \cdot 10^{+45} \lor \neg \left(y \leq 1.15 \cdot 10^{+119}\right):\\
\;\;\;\;y \cdot \left(\left(1 + \log z\right) - z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if y < -1.75999999999999997e45 or 1.15e119 < y Initial program 99.7%
add-sqr-sqrt55.3%
pow255.3%
Applied egg-rr55.3%
Taylor expanded in x around 0 92.1%
if -1.75999999999999997e45 < y < 1.15e119Initial program 100.0%
Taylor expanded in z around inf 89.9%
mul-1-neg89.9%
distribute-rgt-neg-in89.9%
Simplified89.9%
Final simplification90.8%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ 1.0 (log z))))
(if (<= z 1.5e-8)
(+ (* x 0.5) (* y t_0))
(if (<= z 500000.0) (* y (- t_0 z)) (- (* x 0.5) (* y z))))))
double code(double x, double y, double z) {
double t_0 = 1.0 + log(z);
double tmp;
if (z <= 1.5e-8) {
tmp = (x * 0.5) + (y * t_0);
} else if (z <= 500000.0) {
tmp = y * (t_0 - z);
} else {
tmp = (x * 0.5) - (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) :: t_0
real(8) :: tmp
t_0 = 1.0d0 + log(z)
if (z <= 1.5d-8) then
tmp = (x * 0.5d0) + (y * t_0)
else if (z <= 500000.0d0) then
tmp = y * (t_0 - z)
else
tmp = (x * 0.5d0) - (y * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = 1.0 + Math.log(z);
double tmp;
if (z <= 1.5e-8) {
tmp = (x * 0.5) + (y * t_0);
} else if (z <= 500000.0) {
tmp = y * (t_0 - z);
} else {
tmp = (x * 0.5) - (y * z);
}
return tmp;
}
def code(x, y, z): t_0 = 1.0 + math.log(z) tmp = 0 if z <= 1.5e-8: tmp = (x * 0.5) + (y * t_0) elif z <= 500000.0: tmp = y * (t_0 - z) else: tmp = (x * 0.5) - (y * z) return tmp
function code(x, y, z) t_0 = Float64(1.0 + log(z)) tmp = 0.0 if (z <= 1.5e-8) tmp = Float64(Float64(x * 0.5) + Float64(y * t_0)); elseif (z <= 500000.0) tmp = Float64(y * Float64(t_0 - z)); else tmp = Float64(Float64(x * 0.5) - Float64(y * z)); end return tmp end
function tmp_2 = code(x, y, z) t_0 = 1.0 + log(z); tmp = 0.0; if (z <= 1.5e-8) tmp = (x * 0.5) + (y * t_0); elseif (z <= 500000.0) tmp = y * (t_0 - z); else tmp = (x * 0.5) - (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(1.0 + N[Log[z], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, 1.5e-8], N[(N[(x * 0.5), $MachinePrecision] + N[(y * t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[z, 500000.0], N[(y * N[(t$95$0 - z), $MachinePrecision]), $MachinePrecision], N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 + \log z\\
\mathbf{if}\;z \leq 1.5 \cdot 10^{-8}:\\
\;\;\;\;x \cdot 0.5 + y \cdot t\_0\\
\mathbf{elif}\;z \leq 500000:\\
\;\;\;\;y \cdot \left(t\_0 - z\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 0.5 - y \cdot z\\
\end{array}
\end{array}
if z < 1.49999999999999987e-8Initial program 99.8%
Taylor expanded in z around 0 99.8%
if 1.49999999999999987e-8 < z < 5e5Initial program 99.6%
add-sqr-sqrt28.1%
pow228.1%
Applied egg-rr28.1%
Taylor expanded in x around 0 99.6%
if 5e5 < z Initial program 100.0%
Taylor expanded in z around inf 100.0%
mul-1-neg100.0%
distribute-rgt-neg-in100.0%
Simplified100.0%
Final simplification99.9%
(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}
Initial program 99.9%
Final simplification99.9%
(FPCore (x y z) :precision binary64 (if (<= z 1.35e+53) (* x 0.5) (* z (- y))))
double code(double x, double y, double z) {
double tmp;
if (z <= 1.35e+53) {
tmp = x * 0.5;
} else {
tmp = 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 <= 1.35d+53) then
tmp = x * 0.5d0
else
tmp = z * -y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= 1.35e+53) {
tmp = x * 0.5;
} else {
tmp = z * -y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= 1.35e+53: tmp = x * 0.5 else: tmp = z * -y return tmp
function code(x, y, z) tmp = 0.0 if (z <= 1.35e+53) tmp = Float64(x * 0.5); else tmp = Float64(z * Float64(-y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= 1.35e+53) tmp = x * 0.5; else tmp = z * -y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, 1.35e+53], N[(x * 0.5), $MachinePrecision], N[(z * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq 1.35 \cdot 10^{+53}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;z \cdot \left(-y\right)\\
\end{array}
\end{array}
if z < 1.3500000000000001e53Initial program 99.8%
Taylor expanded in x around inf 54.1%
if 1.3500000000000001e53 < z Initial program 100.0%
add-sqr-sqrt48.5%
pow248.5%
Applied egg-rr48.5%
Taylor expanded in x around 0 75.4%
Taylor expanded in z around inf 75.4%
neg-mul-175.4%
Simplified75.4%
Final simplification63.3%
(FPCore (x y z) :precision binary64 (- (* x 0.5) (* y z)))
double code(double x, double y, double z) {
return (x * 0.5) - (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 * 0.5d0) - (y * z)
end function
public static double code(double x, double y, double z) {
return (x * 0.5) - (y * z);
}
def code(x, y, z): return (x * 0.5) - (y * z)
function code(x, y, z) return Float64(Float64(x * 0.5) - Float64(y * z)) end
function tmp = code(x, y, z) tmp = (x * 0.5) - (y * z); end
code[x_, y_, z_] := N[(N[(x * 0.5), $MachinePrecision] - N[(y * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5 - y \cdot z
\end{array}
Initial program 99.9%
Taylor expanded in z around inf 77.5%
mul-1-neg77.5%
distribute-rgt-neg-in77.5%
Simplified77.5%
Final simplification77.5%
(FPCore (x y z) :precision binary64 (* x 0.5))
double code(double x, double y, double z) {
return x * 0.5;
}
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
end function
public static double code(double x, double y, double z) {
return x * 0.5;
}
def code(x, y, z): return x * 0.5
function code(x, y, z) return Float64(x * 0.5) end
function tmp = code(x, y, z) tmp = x * 0.5; end
code[x_, y_, z_] := N[(x * 0.5), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5
\end{array}
Initial program 99.9%
Taylor expanded in x around inf 41.8%
Final simplification41.8%
(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 2024044
(FPCore (x y z)
:name "System.Random.MWC.Distributions:gamma from mwc-random-0.13.3.2"
:precision binary64
:herbie-target
(- (+ y (* 0.5 x)) (* y (- z (log z))))
(+ (* x 0.5) (* y (+ (- 1.0 z) (log z)))))