?

Average Error: 42.16% → 0.99%
Time: 13.7s
Precision: binary32
Cost: 45344

?

\[\left(\left(cosTheta_i > 0.9999 \land cosTheta_i \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq u1 \land u1 \leq 1\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq u2 \land u2 \leq 1\right)\]
\[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
\[\begin{array}{l} t_0 := \sin \left(\pi \cdot u2\right)\\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\mathsf{fma}\left(-t_0, t_0, {\sin \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\pi \cdot u2\right)\right)\right)}^{2}\right) + \cos \left(\pi \cdot \left(u2 + u2\right)\right)\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (sin (* PI u2))))
   (*
    (sqrt (- (log1p (- u1))))
    (+
     (fma (- t_0) t_0 (pow (sin (expm1 (log1p (* PI u2)))) 2.0))
     (cos (* PI (+ u2 u2)))))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
}
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = sinf((((float) M_PI) * u2));
	return sqrtf(-log1pf(-u1)) * (fmaf(-t_0, t_0, powf(sinf(expm1f(log1pf((((float) M_PI) * u2)))), 2.0f)) + cosf((((float) M_PI) * (u2 + u2))));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)))
end
function code(cosTheta_i, u1, u2)
	t_0 = sin(Float32(Float32(pi) * u2))
	return Float32(sqrt(Float32(-log1p(Float32(-u1)))) * Float32(fma(Float32(-t_0), t_0, (sin(expm1(log1p(Float32(Float32(pi) * u2)))) ^ Float32(2.0))) + cos(Float32(Float32(pi) * Float32(u2 + u2)))))
end
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)
\begin{array}{l}
t_0 := \sin \left(\pi \cdot u2\right)\\
\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\mathsf{fma}\left(-t_0, t_0, {\sin \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\pi \cdot u2\right)\right)\right)}^{2}\right) + \cos \left(\pi \cdot \left(u2 + u2\right)\right)\right)
\end{array}

Error?

Derivation?

  1. Initial program 42.16

    \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
  2. Simplified0.95

    \[\leadsto \color{blue}{\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
    Proof

    [Start]42.16

    \[ \sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]

    sub-neg [=>]42.16

    \[ \sqrt{-\log \color{blue}{\left(1 + \left(-u1\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]

    log1p-def [=>]0.95

    \[ \sqrt{-\color{blue}{\mathsf{log1p}\left(-u1\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]

    associate-*l* [=>]0.95

    \[ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \color{blue}{\left(2 \cdot \left(\pi \cdot u2\right)\right)} \]
  3. Applied egg-rr0.95

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \color{blue}{\left(\mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), {\sin \left(\pi \cdot u2\right)}^{2}\right) + \cos \left(\pi \cdot \left(u2 + u2\right)\right)\right)} \]
  4. Applied egg-rr0.99

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), {\sin \color{blue}{\left(\mathsf{expm1}\left(\mathsf{log1p}\left(\pi \cdot u2\right)\right)\right)}}^{2}\right) + \cos \left(\pi \cdot \left(u2 + u2\right)\right)\right) \]
  5. Final simplification0.99

    \[\leadsto \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\mathsf{fma}\left(-\sin \left(\pi \cdot u2\right), \sin \left(\pi \cdot u2\right), {\sin \left(\mathsf{expm1}\left(\mathsf{log1p}\left(\pi \cdot u2\right)\right)\right)}^{2}\right) + \cos \left(\pi \cdot \left(u2 + u2\right)\right)\right) \]

Alternatives

Alternative 1
Error0.96%
Cost42144
\[\begin{array}{l} t_0 := \sin \left(\pi \cdot u2\right)\\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\pi \cdot \left(u2 + u2\right)\right) + \mathsf{fma}\left(-t_0, t_0, \sqrt[3]{{t_0}^{6}}\right)\right) \end{array} \]
Alternative 2
Error0.95%
Cost38944
\[\begin{array}{l} t_0 := \sin \left(\pi \cdot u2\right)\\ \sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \left(\cos \left(\pi \cdot \left(u2 + u2\right)\right) + \mathsf{fma}\left(-t_0, t_0, {t_0}^{2}\right)\right) \end{array} \]
Alternative 3
Error0.96%
Cost19456
\[\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(2 \cdot e^{\log \left(\pi \cdot u2\right)}\right) \]
Alternative 4
Error3.9%
Cost16676
\[\begin{array}{l} t_0 := \cos \left(u2 \cdot \left(\pi \cdot 2\right)\right)\\ \mathbf{if}\;t_0 \leq 0.9999998211860657:\\ \;\;\;\;t_0 \cdot \sqrt{u1 + \left(u1 \cdot u1\right) \cdot \left(u1 \cdot 0.3333333333333333 + 0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \end{array} \]
Alternative 5
Error5.29%
Cost13348
\[\begin{array}{l} t_0 := u2 \cdot \left(\pi \cdot 2\right)\\ \mathbf{if}\;t_0 \leq 0.0012000000569969416:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos t_0 \cdot \sqrt{u1 - u1 \cdot \left(u1 \cdot -0.5\right)}\\ \end{array} \]
Alternative 6
Error9.27%
Cost13156
\[\begin{array}{l} t_0 := u2 \cdot \left(\pi \cdot 2\right)\\ \mathbf{if}\;t_0 \leq 0.005200000014156103:\\ \;\;\;\;\sqrt{-\mathsf{log1p}\left(-u1\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos t_0 \cdot \sqrt{u1}\\ \end{array} \]
Alternative 7
Error0.95%
Cost13056
\[\sqrt{-\mathsf{log1p}\left(-u1\right)} \cdot \cos \left(\left(\pi \cdot u2\right) \cdot 2\right) \]
Alternative 8
Error19.58%
Cost6496
\[\sqrt{-\mathsf{log1p}\left(-u1\right)} \]
Alternative 9
Error23.02%
Cost3680
\[\sqrt{u1 + \left(u1 \cdot u1\right) \cdot \left(u1 \cdot \left(u1 \cdot 0.25 + 0.3333333333333333\right) + 0.5\right)} \]
Alternative 10
Error24.26%
Cost3552
\[\sqrt{u1 + \left(u1 \cdot u1\right) \cdot \left(u1 \cdot 0.3333333333333333 + 0.5\right)} \]
Alternative 11
Error26.74%
Cost3424
\[\sqrt{u1 + \left(u1 \cdot u1\right) \cdot 0.5} \]
Alternative 12
Error34.8%
Cost3232
\[\sqrt{u1} \]

Error

Reproduce?

herbie shell --seed 2023089 
(FPCore (cosTheta_i u1 u2)
  :name "Beckmann Sample, near normal, slope_x"
  :precision binary32
  :pre (and (and (and (> cosTheta_i 0.9999) (<= cosTheta_i 1.0)) (and (<= 2.328306437e-10 u1) (<= u1 1.0))) (and (<= 2.328306437e-10 u2) (<= u2 1.0)))
  (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2))))