
(FPCore (u1 u2) :precision binary64 (+ (* (* (/ 1.0 6.0) (pow (* -2.0 (log u1)) 0.5)) (cos (* (* 2.0 PI) u2))) 0.5))
double code(double u1, double u2) {
return (((1.0 / 6.0) * pow((-2.0 * log(u1)), 0.5)) * cos(((2.0 * ((double) M_PI)) * u2))) + 0.5;
}
public static double code(double u1, double u2) {
return (((1.0 / 6.0) * Math.pow((-2.0 * Math.log(u1)), 0.5)) * Math.cos(((2.0 * Math.PI) * u2))) + 0.5;
}
def code(u1, u2): return (((1.0 / 6.0) * math.pow((-2.0 * math.log(u1)), 0.5)) * math.cos(((2.0 * math.pi) * u2))) + 0.5
function code(u1, u2) return Float64(Float64(Float64(Float64(1.0 / 6.0) * (Float64(-2.0 * log(u1)) ^ 0.5)) * cos(Float64(Float64(2.0 * pi) * u2))) + 0.5) end
function tmp = code(u1, u2) tmp = (((1.0 / 6.0) * ((-2.0 * log(u1)) ^ 0.5)) * cos(((2.0 * pi) * u2))) + 0.5; end
code[u1_, u2_] := N[(N[(N[(N[(1.0 / 6.0), $MachinePrecision] * N[Power[N[(-2.0 * N[Log[u1], $MachinePrecision]), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (u1 u2) :precision binary64 (+ (* (* (/ 1.0 6.0) (pow (* -2.0 (log u1)) 0.5)) (cos (* (* 2.0 PI) u2))) 0.5))
double code(double u1, double u2) {
return (((1.0 / 6.0) * pow((-2.0 * log(u1)), 0.5)) * cos(((2.0 * ((double) M_PI)) * u2))) + 0.5;
}
public static double code(double u1, double u2) {
return (((1.0 / 6.0) * Math.pow((-2.0 * Math.log(u1)), 0.5)) * Math.cos(((2.0 * Math.PI) * u2))) + 0.5;
}
def code(u1, u2): return (((1.0 / 6.0) * math.pow((-2.0 * math.log(u1)), 0.5)) * math.cos(((2.0 * math.pi) * u2))) + 0.5
function code(u1, u2) return Float64(Float64(Float64(Float64(1.0 / 6.0) * (Float64(-2.0 * log(u1)) ^ 0.5)) * cos(Float64(Float64(2.0 * pi) * u2))) + 0.5) end
function tmp = code(u1, u2) tmp = (((1.0 / 6.0) * ((-2.0 * log(u1)) ^ 0.5)) * cos(((2.0 * pi) * u2))) + 0.5; end
code[u1_, u2_] := N[(N[(N[(N[(1.0 / 6.0), $MachinePrecision] * N[Power[N[(-2.0 * N[Log[u1], $MachinePrecision]), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{6} \cdot {\left(-2 \cdot \log u1\right)}^{0.5}\right) \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5
\end{array}
(FPCore (u1 u2) :precision binary64 (+ (* (sqrt (log (pow u1 -0.05555555555555555))) (cos (* (* 2.0 PI) u2))) 0.5))
double code(double u1, double u2) {
return (sqrt(log(pow(u1, -0.05555555555555555))) * cos(((2.0 * ((double) M_PI)) * u2))) + 0.5;
}
public static double code(double u1, double u2) {
return (Math.sqrt(Math.log(Math.pow(u1, -0.05555555555555555))) * Math.cos(((2.0 * Math.PI) * u2))) + 0.5;
}
def code(u1, u2): return (math.sqrt(math.log(math.pow(u1, -0.05555555555555555))) * math.cos(((2.0 * math.pi) * u2))) + 0.5
function code(u1, u2) return Float64(Float64(sqrt(log((u1 ^ -0.05555555555555555))) * cos(Float64(Float64(2.0 * pi) * u2))) + 0.5) end
function tmp = code(u1, u2) tmp = (sqrt(log((u1 ^ -0.05555555555555555))) * cos(((2.0 * pi) * u2))) + 0.5; end
code[u1_, u2_] := N[(N[(N[Sqrt[N[Log[N[Power[u1, -0.05555555555555555], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\log \left({u1}^{-0.05555555555555555}\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5
\end{array}
Initial program 99.3%
expm1-log1p-u99.1%
expm1-udef99.1%
metadata-eval99.1%
unpow1/299.1%
Applied egg-rr99.1%
expm1-def99.1%
expm1-log1p99.3%
*-commutative99.3%
Simplified99.3%
*-commutative99.3%
rem-cbrt-cube99.1%
unpow1/398.8%
Applied egg-rr44.1%
unpow1/344.1%
*-commutative44.1%
log-pow99.2%
associate-*r*99.2%
metadata-eval99.2%
Simplified99.2%
add-sqr-sqrt98.9%
sqrt-prod99.2%
cbrt-unprod99.5%
pow-prod-up99.5%
metadata-eval99.5%
pow399.5%
add-cbrt-cube99.6%
add-log-exp99.6%
*-commutative99.6%
exp-to-pow99.6%
Applied egg-rr99.6%
Final simplification99.6%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (* (cos (* (* 2.0 PI) u2)) (sqrt (* -0.05555555555555555 (log u1))))))
double code(double u1, double u2) {
return 0.5 + (cos(((2.0 * ((double) M_PI)) * u2)) * sqrt((-0.05555555555555555 * log(u1))));
}
public static double code(double u1, double u2) {
return 0.5 + (Math.cos(((2.0 * Math.PI) * u2)) * Math.sqrt((-0.05555555555555555 * Math.log(u1))));
}
def code(u1, u2): return 0.5 + (math.cos(((2.0 * math.pi) * u2)) * math.sqrt((-0.05555555555555555 * math.log(u1))))
function code(u1, u2) return Float64(0.5 + Float64(cos(Float64(Float64(2.0 * pi) * u2)) * sqrt(Float64(-0.05555555555555555 * log(u1))))) end
function tmp = code(u1, u2) tmp = 0.5 + (cos(((2.0 * pi) * u2)) * sqrt((-0.05555555555555555 * log(u1)))); end
code[u1_, u2_] := N[(0.5 + N[(N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(-0.05555555555555555 * N[Log[u1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{-0.05555555555555555 \cdot \log u1}
\end{array}
Initial program 99.3%
add-sqr-sqrt99.0%
sqrt-unprod99.3%
*-commutative99.3%
*-commutative99.3%
swap-sqr99.4%
unpow1/299.4%
unpow1/299.4%
add-sqr-sqrt99.6%
metadata-eval99.6%
metadata-eval99.6%
metadata-eval99.6%
Applied egg-rr99.6%
*-commutative99.6%
associate-*l*99.6%
metadata-eval99.6%
Simplified99.6%
Final simplification99.6%
(FPCore (u1 u2) :precision binary64 (fma 0.16666666666666666 (sqrt (* (log u1) -2.0)) 0.5))
double code(double u1, double u2) {
return fma(0.16666666666666666, sqrt((log(u1) * -2.0)), 0.5);
}
function code(u1, u2) return fma(0.16666666666666666, sqrt(Float64(log(u1) * -2.0)), 0.5) end
code[u1_, u2_] := N[(0.16666666666666666 * N[Sqrt[N[(N[Log[u1], $MachinePrecision] * -2.0), $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.16666666666666666, \sqrt{\log u1 \cdot -2}, 0.5\right)
\end{array}
Initial program 99.3%
*-commutative99.3%
associate-*r*99.3%
fma-def99.3%
associate-*l*99.3%
metadata-eval99.3%
unpow1/299.3%
Applied egg-rr99.3%
Taylor expanded in u2 around 0 97.9%
Final simplification97.9%
herbie shell --seed 2023174
(FPCore (u1 u2)
:name "normal distribution"
:precision binary64
:pre (and (and (<= 0.0 u1) (<= u1 1.0)) (and (<= 0.0 u2) (<= u2 1.0)))
(+ (* (* (/ 1.0 6.0) (pow (* -2.0 (log u1)) 0.5)) (cos (* (* 2.0 PI) u2))) 0.5))