Average Error: 13.6 → 0.4
Time: 22.2s
Precision: binary32
\[cosTheta_i > 0.9999 \land cosTheta_i \leq 1 \land 2.328306437 \cdot 10^{-10} \leq u1 \land u1 \leq 1 \land 2.328306437 \cdot 10^{-10} \leq u2 \land u2 \leq 1\]
\[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\]
\[\begin{array}{l} \mathbf{if}\;1 - u1 \leq 0.9656239748001099:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{\left(u1 - \left(u1 \cdot u1\right) \cdot \left(-0.5 - u1 \cdot 0.3333333333333333\right)\right) - {u1}^{4} \cdot -0.25}\\ \end{array}\]
\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)
\begin{array}{l}
\mathbf{if}\;1 - u1 \leq 0.9656239748001099:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\

\mathbf{else}:\\
\;\;\;\;\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{\left(u1 - \left(u1 \cdot u1\right) \cdot \left(-0.5 - u1 \cdot 0.3333333333333333\right)\right) - {u1}^{4} \cdot -0.25}\\

\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
 (if (<= (- 1.0 u1) 0.9656239748001099)
   (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2)))
   (*
    (cos (* (* 2.0 PI) u2))
    (sqrt
     (-
      (- u1 (* (* u1 u1) (- -0.5 (* u1 0.3333333333333333))))
      (* (pow u1 4.0) -0.25))))))
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 tmp;
	if ((1.0f - u1) <= 0.9656239748001099f) {
		tmp = sqrtf(-logf(1.0f - u1)) * cosf((2.0f * ((float) M_PI)) * u2);
	} else {
		tmp = cosf((2.0f * ((float) M_PI)) * u2) * sqrtf((u1 - ((u1 * u1) * (-0.5f - (u1 * 0.3333333333333333f)))) - (powf(u1, 4.0f) * -0.25f));
	}
	return tmp;
}

Error

Bits error versus cosTheta_i

Bits error versus u1

Bits error versus u2

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Split input into 2 regimes
  2. if (-.f32 1 u1) < 0.965623975

    1. Initial program 0.8

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

    if 0.965623975 < (-.f32 1 u1)

    1. Initial program 15.9

      \[\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\]
    2. Taylor expanded around 0 0.3

      \[\leadsto \sqrt{-\color{blue}{\left(-\left(0.5 \cdot {u1}^{2} + \left(0.3333333333333333 \cdot {u1}^{3} + \left(u1 + 0.25 \cdot {u1}^{4}\right)\right)\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\]
    3. Simplified0.3

      \[\leadsto \sqrt{-\color{blue}{\left(\left(\left(u1 \cdot u1\right) \cdot \left(-0.5 - u1 \cdot 0.3333333333333333\right) - u1\right) + {u1}^{4} \cdot -0.25\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;1 - u1 \leq 0.9656239748001099:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \cdot \sqrt{\left(u1 - \left(u1 \cdot u1\right) \cdot \left(-0.5 - u1 \cdot 0.3333333333333333\right)\right) - {u1}^{4} \cdot -0.25}\\ \end{array}\]

Reproduce

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