
(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 (pow u1 -0.05555555555555555))) (cos (* 2.0 (* u2 PI))) 0.5))
double code(double u1, double u2) {
return fma(sqrt(log(pow(u1, -0.05555555555555555))), cos((2.0 * (u2 * ((double) M_PI)))), 0.5);
}
function code(u1, u2) return fma(sqrt(log((u1 ^ -0.05555555555555555))), cos(Float64(2.0 * Float64(u2 * pi))), 0.5) end
code[u1_, u2_] := N[(N[Sqrt[N[Log[N[Power[u1, -0.05555555555555555], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * N[Cos[N[(2.0 * N[(u2 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt{\log \left({u1}^{-0.05555555555555555}\right)}, \cos \left(2 \cdot \left(u2 \cdot \pi\right)\right), 0.5\right)
\end{array}
Initial program 99.5%
pow1/299.5%
*-commutative99.5%
associate-*r*99.5%
metadata-eval99.5%
associate-*r*99.5%
expm1-log1p-u98.6%
fma-undefine98.6%
expm1-undefine98.6%
Applied egg-rr98.6%
log1p-undefine98.6%
rem-exp-log99.3%
+-commutative99.3%
associate--l+99.4%
metadata-eval99.4%
+-rgt-identity99.4%
fma-undefine99.4%
*-commutative99.4%
associate-*r*99.5%
fma-undefine99.5%
Simplified99.7%
add-log-exp99.7%
exp-to-pow99.7%
Applied egg-rr99.7%
(FPCore (u1 u2) :precision binary64 (fma (sqrt (* -0.05555555555555555 (log u1))) (cos (* 2.0 (* u2 PI))) 0.5))
double code(double u1, double u2) {
return fma(sqrt((-0.05555555555555555 * log(u1))), cos((2.0 * (u2 * ((double) M_PI)))), 0.5);
}
function code(u1, u2) return fma(sqrt(Float64(-0.05555555555555555 * log(u1))), cos(Float64(2.0 * Float64(u2 * pi))), 0.5) end
code[u1_, u2_] := N[(N[Sqrt[N[(-0.05555555555555555 * N[Log[u1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(2.0 * N[(u2 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sqrt{-0.05555555555555555 \cdot \log u1}, \cos \left(2 \cdot \left(u2 \cdot \pi\right)\right), 0.5\right)
\end{array}
Initial program 99.5%
pow1/299.5%
*-commutative99.5%
associate-*r*99.5%
metadata-eval99.5%
associate-*r*99.5%
expm1-log1p-u98.6%
fma-undefine98.6%
expm1-undefine98.6%
Applied egg-rr98.6%
log1p-undefine98.6%
rem-exp-log99.3%
+-commutative99.3%
associate--l+99.4%
metadata-eval99.4%
+-rgt-identity99.4%
fma-undefine99.4%
*-commutative99.4%
associate-*r*99.5%
fma-undefine99.5%
Simplified99.7%
Final simplification99.7%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (* (sqrt (* -0.05555555555555555 (log u1))) (cos (* u2 (* 2.0 PI))))))
double code(double u1, double u2) {
return 0.5 + (sqrt((-0.05555555555555555 * log(u1))) * cos((u2 * (2.0 * ((double) M_PI)))));
}
public static double code(double u1, double u2) {
return 0.5 + (Math.sqrt((-0.05555555555555555 * Math.log(u1))) * Math.cos((u2 * (2.0 * Math.PI))));
}
def code(u1, u2): return 0.5 + (math.sqrt((-0.05555555555555555 * math.log(u1))) * math.cos((u2 * (2.0 * math.pi))))
function code(u1, u2) return Float64(0.5 + Float64(sqrt(Float64(-0.05555555555555555 * log(u1))) * cos(Float64(u2 * Float64(2.0 * pi))))) end
function tmp = code(u1, u2) tmp = 0.5 + (sqrt((-0.05555555555555555 * log(u1))) * cos((u2 * (2.0 * pi)))); end
code[u1_, u2_] := N[(0.5 + N[(N[Sqrt[N[(-0.05555555555555555 * N[Log[u1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(u2 * N[(2.0 * Pi), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \sqrt{-0.05555555555555555 \cdot \log u1} \cdot \cos \left(u2 \cdot \left(2 \cdot \pi\right)\right)
\end{array}
Initial program 99.5%
expm1-log1p-u99.2%
expm1-undefine99.2%
metadata-eval99.2%
pow1/299.2%
Applied egg-rr99.2%
log1p-undefine99.2%
rem-exp-log99.4%
+-commutative99.4%
associate--l+99.5%
metadata-eval99.5%
+-rgt-identity99.5%
rem-square-sqrt99.0%
fabs-sqr99.0%
rem-square-sqrt99.5%
rem-sqrt-square99.5%
swap-sqr99.4%
metadata-eval99.4%
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 (+ (sqrt (log (pow u1 -0.05555555555555555))) 0.5))
double code(double u1, double u2) {
return sqrt(log(pow(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(Math.pow(u1, -0.05555555555555555))) + 0.5;
}
def code(u1, u2): return math.sqrt(math.log(math.pow(u1, -0.05555555555555555))) + 0.5
function code(u1, u2) return Float64(sqrt(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[Log[N[Power[u1, -0.05555555555555555], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] + 0.5), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\log \left({u1}^{-0.05555555555555555}\right)} + 0.5
\end{array}
Initial program 99.5%
*-commutative99.5%
associate-*l*99.5%
fma-define99.5%
unpow1/299.5%
metadata-eval99.5%
associate-*l*99.5%
Simplified99.5%
Taylor expanded in u2 around 0 97.8%
fma-undefine97.8%
*-commutative97.8%
Applied egg-rr97.8%
add-sqr-sqrt97.4%
sqrt-unprod97.8%
*-commutative97.8%
*-commutative97.8%
swap-sqr97.8%
add-sqr-sqrt98.0%
*-commutative98.0%
metadata-eval98.0%
Applied egg-rr98.0%
associate-*l*98.0%
metadata-eval98.0%
Simplified98.0%
add-log-exp99.7%
exp-to-pow99.7%
Applied egg-rr98.0%
(FPCore (u1 u2) :precision binary64 (+ 0.5 (sqrt (* -0.05555555555555555 (log u1)))))
double code(double u1, double u2) {
return 0.5 + sqrt((-0.05555555555555555 * log(u1)));
}
real(8) function code(u1, u2)
real(8), intent (in) :: u1
real(8), intent (in) :: u2
code = 0.5d0 + sqrt(((-0.05555555555555555d0) * log(u1)))
end function
public static double code(double u1, double u2) {
return 0.5 + Math.sqrt((-0.05555555555555555 * Math.log(u1)));
}
def code(u1, u2): return 0.5 + math.sqrt((-0.05555555555555555 * math.log(u1)))
function code(u1, u2) return Float64(0.5 + sqrt(Float64(-0.05555555555555555 * log(u1)))) end
function tmp = code(u1, u2) tmp = 0.5 + sqrt((-0.05555555555555555 * log(u1))); end
code[u1_, u2_] := N[(0.5 + N[Sqrt[N[(-0.05555555555555555 * N[Log[u1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 + \sqrt{-0.05555555555555555 \cdot \log u1}
\end{array}
Initial program 99.5%
*-commutative99.5%
associate-*l*99.5%
fma-define99.5%
unpow1/299.5%
metadata-eval99.5%
associate-*l*99.5%
Simplified99.5%
Taylor expanded in u2 around 0 97.8%
fma-undefine97.8%
*-commutative97.8%
Applied egg-rr97.8%
add-sqr-sqrt97.4%
sqrt-unprod97.8%
*-commutative97.8%
*-commutative97.8%
swap-sqr97.8%
add-sqr-sqrt98.0%
*-commutative98.0%
metadata-eval98.0%
Applied egg-rr98.0%
associate-*l*98.0%
metadata-eval98.0%
Simplified98.0%
Final simplification98.0%
herbie shell --seed 2024110
(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))