
(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 (* (log u1) -0.05555555555555555)) (cos (* u2 (* 2.0 PI))) 0.5))
double code(double u1, double u2) {
return fma(sqrt((log(u1) * -0.05555555555555555)), cos((u2 * (2.0 * ((double) M_PI)))), 0.5);
}
function code(u1, u2) return fma(sqrt(Float64(log(u1) * -0.05555555555555555)), cos(Float64(u2 * Float64(2.0 * pi))), 0.5) end
code[u1_, u2_] := N[(N[Sqrt[N[(N[Log[u1], $MachinePrecision] * -0.05555555555555555), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(u2 * N[(2.0 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt{\log u1 \cdot -0.05555555555555555}, \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right), 0.5\right)
\end{array}
Initial program 99.4%
Applied egg-rr99.0%
sub-neg99.0%
metadata-eval99.0%
+-commutative99.0%
log1p-undefine98.9%
rem-exp-log99.3%
fma-define99.3%
+-commutative99.3%
associate-+r+99.3%
metadata-eval99.3%
associate-+r+99.4%
Simplified99.7%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (* (sqrt (* (log u1) -0.05555555555555555)) (cos (* u2 (* 2.0 PI))))))
double code(double u1, double u2) {
return 0.5 + (sqrt((log(u1) * -0.05555555555555555)) * cos((u2 * (2.0 * ((double) M_PI)))));
}
public static double code(double u1, double u2) {
return 0.5 + (Math.sqrt((Math.log(u1) * -0.05555555555555555)) * Math.cos((u2 * (2.0 * Math.PI))));
}
def code(u1, u2): return 0.5 + (math.sqrt((math.log(u1) * -0.05555555555555555)) * math.cos((u2 * (2.0 * math.pi))))
function code(u1, u2) return Float64(0.5 + Float64(sqrt(Float64(log(u1) * -0.05555555555555555)) * cos(Float64(u2 * Float64(2.0 * pi))))) end
function tmp = code(u1, u2) tmp = 0.5 + (sqrt((log(u1) * -0.05555555555555555)) * cos((u2 * (2.0 * pi)))); end
code[u1_, u2_] := N[(0.5 + N[(N[Sqrt[N[(N[Log[u1], $MachinePrecision] * -0.05555555555555555), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(u2 * N[(2.0 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \sqrt{\log u1 \cdot -0.05555555555555555} \cdot \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)
\end{array}
Initial program 99.4%
pow1/299.4%
pow199.4%
metadata-eval99.4%
Applied egg-rr99.4%
unpow199.4%
rem-square-sqrt99.1%
fabs-sqr99.1%
rem-square-sqrt99.4%
rem-sqrt-square99.4%
swap-sqr99.5%
metadata-eval99.5%
rem-square-sqrt99.7%
*-commutative99.7%
*-commutative99.7%
associate-*l*99.7%
metadata-eval99.7%
Simplified99.7%
Final simplification99.7%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (sqrt (log (pow u1 -0.05555555555555555)))))
double code(double u1, double u2) {
return 0.5 + sqrt(log(pow(u1, -0.05555555555555555)));
}
real(8) function code(u1, u2)
real(8), intent (in) :: u1
real(8), intent (in) :: u2
code = 0.5d0 + sqrt(log((u1 ** (-0.05555555555555555d0))))
end function
public static double code(double u1, double u2) {
return 0.5 + Math.sqrt(Math.log(Math.pow(u1, -0.05555555555555555)));
}
def code(u1, u2): return 0.5 + math.sqrt(math.log(math.pow(u1, -0.05555555555555555)))
function code(u1, u2) return Float64(0.5 + sqrt(log((u1 ^ -0.05555555555555555)))) end
function tmp = code(u1, u2) tmp = 0.5 + sqrt(log((u1 ^ -0.05555555555555555))); end
code[u1_, u2_] := N[(0.5 + N[Sqrt[N[Log[N[Power[u1, -0.05555555555555555], $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \sqrt{\log \left({u1}^{-0.05555555555555555}\right)}
\end{array}
Initial program 99.4%
*-commutative99.4%
associate-*l*99.4%
fma-define99.4%
unpow1/299.4%
metadata-eval99.4%
associate-*l*99.4%
Simplified99.4%
Taylor expanded in u2 around 0 97.5%
fma-undefine97.5%
*-commutative97.5%
*-commutative97.5%
Applied egg-rr97.5%
add-sqr-sqrt97.2%
sqrt-unprod97.5%
*-commutative97.5%
*-commutative97.5%
swap-sqr97.6%
add-sqr-sqrt97.8%
metadata-eval97.8%
Applied egg-rr97.8%
associate-*l*97.8%
metadata-eval97.8%
*-commutative97.8%
Simplified97.8%
add-log-exp97.8%
*-commutative97.8%
exp-to-pow97.8%
Applied egg-rr97.8%
Final simplification97.8%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (pow (* (log u1) -0.05555555555555555) 0.5)))
double code(double u1, double u2) {
return 0.5 + pow((log(u1) * -0.05555555555555555), 0.5);
}
real(8) function code(u1, u2)
real(8), intent (in) :: u1
real(8), intent (in) :: u2
code = 0.5d0 + ((log(u1) * (-0.05555555555555555d0)) ** 0.5d0)
end function
public static double code(double u1, double u2) {
return 0.5 + Math.pow((Math.log(u1) * -0.05555555555555555), 0.5);
}
def code(u1, u2): return 0.5 + math.pow((math.log(u1) * -0.05555555555555555), 0.5)
function code(u1, u2) return Float64(0.5 + (Float64(log(u1) * -0.05555555555555555) ^ 0.5)) end
function tmp = code(u1, u2) tmp = 0.5 + ((log(u1) * -0.05555555555555555) ^ 0.5); end
code[u1_, u2_] := N[(0.5 + N[Power[N[(N[Log[u1], $MachinePrecision] * -0.05555555555555555), $MachinePrecision], 0.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + {\left(\log u1 \cdot -0.05555555555555555\right)}^{0.5}
\end{array}
Initial program 99.4%
*-commutative99.4%
associate-*l*99.4%
fma-define99.4%
unpow1/299.4%
metadata-eval99.4%
associate-*l*99.4%
Simplified99.4%
Taylor expanded in u2 around 0 97.5%
fma-undefine97.5%
*-commutative97.5%
*-commutative97.5%
Applied egg-rr97.5%
add-sqr-sqrt97.2%
sqrt-unprod97.5%
*-commutative97.5%
*-commutative97.5%
swap-sqr97.6%
add-sqr-sqrt97.8%
metadata-eval97.8%
Applied egg-rr97.8%
associate-*l*97.8%
metadata-eval97.8%
*-commutative97.8%
Simplified97.8%
pow1/297.8%
*-commutative97.8%
Applied egg-rr97.8%
Final simplification97.8%
(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.4%
metadata-eval99.4%
associate-*l*99.4%
Simplified99.4%
Taylor expanded in u2 around 0 97.5%
fma-undefine97.5%
*-commutative97.5%
*-commutative97.5%
Applied egg-rr97.5%
add-sqr-sqrt97.2%
sqrt-unprod97.5%
*-commutative97.5%
*-commutative97.5%
swap-sqr97.6%
add-sqr-sqrt97.8%
metadata-eval97.8%
Applied egg-rr97.8%
associate-*l*97.8%
metadata-eval97.8%
*-commutative97.8%
Simplified97.8%
Final simplification97.8%
herbie shell --seed 2024132
(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))