
(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 u1) -0.05555555555555555)) (cos (* (* 2.0 PI) u2))) 0.5))
double code(double u1, double u2) {
return (sqrt((log(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(u1) * -0.05555555555555555)) * Math.cos(((2.0 * Math.PI) * u2))) + 0.5;
}
def code(u1, u2): return (math.sqrt((math.log(u1) * -0.05555555555555555)) * math.cos(((2.0 * math.pi) * u2))) + 0.5
function code(u1, u2) return Float64(Float64(sqrt(Float64(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[(N[Log[u1], $MachinePrecision] * -0.05555555555555555), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(2.0 * Pi), $MachinePrecision] * u2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\log u1 \cdot -0.05555555555555555} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) + 0.5
\end{array}
Initial program 99.4%
pow1/299.4%
add-sqr-sqrt99.0%
sqrt-unprod99.4%
*-commutative99.4%
*-commutative99.4%
swap-sqr99.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%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (sqrt (+ -1.0 (fma -0.05555555555555555 (log u1) 1.0)))))
double code(double u1, double u2) {
return 0.5 + sqrt((-1.0 + fma(-0.05555555555555555, log(u1), 1.0)));
}
function code(u1, u2) return Float64(0.5 + sqrt(Float64(-1.0 + fma(-0.05555555555555555, log(u1), 1.0)))) end
code[u1_, u2_] := N[(0.5 + N[Sqrt[N[(-1.0 + N[(-0.05555555555555555 * N[Log[u1], $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \sqrt{-1 + \mathsf{fma}\left(-0.05555555555555555, \log u1, 1\right)}
\end{array}
Initial program 99.4%
*-commutative99.4%
associate-*l*99.4%
fma-define99.4%
unpow1/299.3%
metadata-eval99.3%
associate-*l*99.3%
Simplified99.3%
Taylor expanded in u2 around 0 98.0%
fma-undefine98.0%
*-commutative98.0%
Applied egg-rr98.0%
pow198.0%
Applied egg-rr51.0%
unpow151.0%
Simplified51.0%
pow-to-exp51.0%
add-log-exp98.3%
associate-*l*98.3%
metadata-eval98.3%
expm1-log1p-u98.0%
expm1-undefine98.0%
Applied egg-rr98.0%
sub-neg98.0%
log1p-undefine98.0%
rem-exp-log98.3%
metadata-eval98.3%
+-commutative98.3%
+-commutative98.3%
*-commutative98.3%
fma-define98.3%
Simplified98.3%
Final simplification98.3%
(FPCore (u1 u2) :precision binary64 (+ (sqrt (* (log u1) -0.05555555555555555)) 0.5))
double code(double u1, double u2) {
return sqrt((log(u1) * -0.05555555555555555)) + 0.5;
}
real(8) function code(u1, u2)
real(8), intent (in) :: u1
real(8), intent (in) :: u2
code = sqrt((log(u1) * (-0.05555555555555555d0))) + 0.5d0
end function
public static double code(double u1, double u2) {
return Math.sqrt((Math.log(u1) * -0.05555555555555555)) + 0.5;
}
def code(u1, u2): return math.sqrt((math.log(u1) * -0.05555555555555555)) + 0.5
function code(u1, u2) return Float64(sqrt(Float64(log(u1) * -0.05555555555555555)) + 0.5) end
function tmp = code(u1, u2) tmp = sqrt((log(u1) * -0.05555555555555555)) + 0.5; end
code[u1_, u2_] := N[(N[Sqrt[N[(N[Log[u1], $MachinePrecision] * -0.05555555555555555), $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\log u1 \cdot -0.05555555555555555} + 0.5
\end{array}
Initial program 99.4%
*-commutative99.4%
associate-*l*99.4%
fma-define99.4%
unpow1/299.3%
metadata-eval99.3%
associate-*l*99.3%
Simplified99.3%
Taylor expanded in u2 around 0 98.0%
pow1/298.0%
Applied egg-rr98.0%
pow1/298.0%
add-cbrt-cube97.7%
add-sqr-sqrt97.8%
pow197.8%
pow1/297.8%
pow-prod-up97.8%
metadata-eval97.8%
Applied egg-rr97.8%
pow1/396.8%
log-pow50.4%
pow-pow50.9%
pow-to-exp50.9%
add-log-exp98.0%
rem-cube-cbrt97.4%
metadata-eval97.4%
pow1/297.4%
fma-define97.4%
*-commutative97.4%
Applied egg-rr98.3%
herbie shell --seed 2024144
(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))