
(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 5 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 (fma (sqrt (log1p (+ (pow u1 -0.05555555555555555) -1.0))) (cos (* u2 (* 2.0 PI))) 0.5))
double code(double u1, double u2) {
return fma(sqrt(log1p((pow(u1, -0.05555555555555555) + -1.0))), cos((u2 * (2.0 * ((double) M_PI)))), 0.5);
}
function code(u1, u2) return fma(sqrt(log1p(Float64((u1 ^ -0.05555555555555555) + -1.0))), cos(Float64(u2 * Float64(2.0 * pi))), 0.5) end
code[u1_, u2_] := N[(N[Sqrt[N[Log[1 + N[(N[Power[u1, -0.05555555555555555], $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * N[Cos[N[(u2 * N[(2.0 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt{\mathsf{log1p}\left({u1}^{-0.05555555555555555} + -1\right)}, \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right), 0.5\right)
\end{array}
Initial program 99.4%
pow1/299.4%
add-log-exp99.4%
metadata-eval99.4%
*-un-lft-identity99.4%
metadata-eval99.4%
log-prod99.4%
metadata-eval99.4%
add-log-exp99.4%
metadata-eval99.4%
Applied egg-rr99.4%
+-lft-identity99.4%
*-commutative99.4%
Simplified99.4%
Applied egg-rr99.1%
sub-neg99.1%
metadata-eval99.1%
+-commutative99.1%
log1p-undefine99.1%
rem-exp-log99.5%
fma-define99.5%
+-commutative99.5%
associate-+r+99.5%
metadata-eval99.5%
Simplified99.7%
log1p-expm1-u99.7%
expm1-undefine99.6%
exp-to-pow99.7%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (u1 u2) :precision binary64 (fma (sqrt (log (pow u1 -0.05555555555555555))) (cos (* u2 (* 2.0 PI))) 0.5))
double code(double u1, double u2) {
return fma(sqrt(log(pow(u1, -0.05555555555555555))), cos((u2 * (2.0 * ((double) M_PI)))), 0.5);
}
function code(u1, u2) return fma(sqrt(log((u1 ^ -0.05555555555555555))), cos(Float64(u2 * Float64(2.0 * pi))), 0.5) end
code[u1_, u2_] := N[(N[Sqrt[N[Log[N[Power[u1, -0.05555555555555555], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * N[Cos[N[(u2 * N[(2.0 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt{\log \left({u1}^{-0.05555555555555555}\right)}, \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right), 0.5\right)
\end{array}
Initial program 99.4%
pow1/299.4%
add-log-exp99.4%
metadata-eval99.4%
*-un-lft-identity99.4%
metadata-eval99.4%
log-prod99.4%
metadata-eval99.4%
add-log-exp99.4%
metadata-eval99.4%
Applied egg-rr99.4%
+-lft-identity99.4%
*-commutative99.4%
Simplified99.4%
Applied egg-rr99.1%
sub-neg99.1%
metadata-eval99.1%
+-commutative99.1%
log1p-undefine99.1%
rem-exp-log99.5%
fma-define99.5%
+-commutative99.5%
associate-+r+99.5%
metadata-eval99.5%
Simplified99.7%
add-log-exp99.6%
exp-to-pow99.7%
Applied egg-rr99.7%
Final simplification99.7%
(FPCore (u1 u2) :precision binary64 (fma (sqrt (* -0.05555555555555555 (log u1))) (cos (* u2 (* 2.0 PI))) 0.5))
double code(double u1, double u2) {
return fma(sqrt((-0.05555555555555555 * log(u1))), cos((u2 * (2.0 * ((double) M_PI)))), 0.5);
}
function code(u1, u2) return fma(sqrt(Float64(-0.05555555555555555 * log(u1))), cos(Float64(u2 * Float64(2.0 * pi))), 0.5) end
code[u1_, u2_] := N[(N[Sqrt[N[(-0.05555555555555555 * N[Log[u1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(u2 * N[(2.0 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt{-0.05555555555555555 \cdot \log u1}, \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right), 0.5\right)
\end{array}
Initial program 99.4%
pow1/299.4%
add-log-exp99.4%
metadata-eval99.4%
*-un-lft-identity99.4%
metadata-eval99.4%
log-prod99.4%
metadata-eval99.4%
add-log-exp99.4%
metadata-eval99.4%
Applied egg-rr99.4%
+-lft-identity99.4%
*-commutative99.4%
Simplified99.4%
Applied egg-rr99.1%
sub-neg99.1%
metadata-eval99.1%
+-commutative99.1%
log1p-undefine99.1%
rem-exp-log99.5%
fma-define99.5%
+-commutative99.5%
associate-+r+99.5%
metadata-eval99.5%
Simplified99.7%
Final simplification99.7%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (* (cos (* u2 (* 2.0 PI))) (sqrt (* -0.05555555555555555 (log u1))))))
double code(double u1, double u2) {
return 0.5 + (cos((u2 * (2.0 * ((double) M_PI)))) * sqrt((-0.05555555555555555 * log(u1))));
}
public static double code(double u1, double u2) {
return 0.5 + (Math.cos((u2 * (2.0 * Math.PI))) * Math.sqrt((-0.05555555555555555 * Math.log(u1))));
}
def code(u1, u2): return 0.5 + (math.cos((u2 * (2.0 * math.pi))) * math.sqrt((-0.05555555555555555 * math.log(u1))))
function code(u1, u2) return Float64(0.5 + Float64(cos(Float64(u2 * Float64(2.0 * pi))) * sqrt(Float64(-0.05555555555555555 * log(u1))))) end
function tmp = code(u1, u2) tmp = 0.5 + (cos((u2 * (2.0 * pi))) * sqrt((-0.05555555555555555 * log(u1)))); end
code[u1_, u2_] := N[(0.5 + N[(N[Cos[N[(u2 * N[(2.0 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(-0.05555555555555555 * N[Log[u1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{-0.05555555555555555 \cdot \log u1}
\end{array}
Initial program 99.4%
pow1/299.4%
metadata-eval99.4%
rem-square-sqrt99.1%
sqrt-unprod99.4%
metadata-eval99.4%
metadata-eval99.4%
*-commutative99.4%
*-commutative99.4%
swap-sqr99.4%
add-sqr-sqrt99.7%
metadata-eval99.7%
metadata-eval99.7%
metadata-eval99.7%
Applied egg-rr99.7%
*-commutative99.7%
associate-*l*99.7%
metadata-eval99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (* (sqrt (log u1)) (sqrt -0.05555555555555555))))
double code(double u1, double u2) {
return 0.5 + (sqrt(log(u1)) * sqrt(-0.05555555555555555));
}
real(8) function code(u1, u2)
real(8), intent (in) :: u1
real(8), intent (in) :: u2
code = 0.5d0 + (sqrt(log(u1)) * sqrt((-0.05555555555555555d0)))
end function
public static double code(double u1, double u2) {
return 0.5 + (Math.sqrt(Math.log(u1)) * Math.sqrt(-0.05555555555555555));
}
def code(u1, u2): return 0.5 + (math.sqrt(math.log(u1)) * math.sqrt(-0.05555555555555555))
function code(u1, u2) return Float64(0.5 + Float64(sqrt(log(u1)) * sqrt(-0.05555555555555555))) end
function tmp = code(u1, u2) tmp = 0.5 + (sqrt(log(u1)) * sqrt(-0.05555555555555555)); end
code[u1_, u2_] := N[(0.5 + N[(N[Sqrt[N[Log[u1], $MachinePrecision]], $MachinePrecision] * N[Sqrt[-0.05555555555555555], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \sqrt{\log u1} \cdot \sqrt{-0.05555555555555555}
\end{array}
Initial program 99.4%
pow1/299.4%
add-log-exp99.4%
metadata-eval99.4%
*-un-lft-identity99.4%
metadata-eval99.4%
log-prod99.4%
metadata-eval99.4%
add-log-exp99.4%
metadata-eval99.4%
Applied egg-rr99.4%
+-lft-identity99.4%
*-commutative99.4%
Simplified99.4%
Applied egg-rr99.1%
sub-neg99.1%
metadata-eval99.1%
+-commutative99.1%
log1p-undefine99.1%
rem-exp-log99.5%
fma-define99.5%
+-commutative99.5%
associate-+r+99.5%
metadata-eval99.5%
Simplified99.7%
Taylor expanded in u2 around 0 0.0%
+-commutative0.0%
Simplified0.0%
Final simplification0.0%
herbie shell --seed 2024081
(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))