Average Error: 13.8 → 0.4
Time: 10.1s
Precision: binary32
\[\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} \mathbf{if}\;1 - u1 \leq 0.9700000286102295:\\ \;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{-\left(-\mathsf{fma}\left(0.3333333333333333, {u1}^{3}, \mathsf{fma}\left(0.5, u1 \cdot u1, \mathsf{fma}\left(0.25, {u1}^{4}, u1\right)\right)\right)\right)} \cdot \cos \log \left({\left({\left(e^{2}\right)}^{\pi}\right)}^{u2}\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
 (if (<= (- 1.0 u1) 0.9700000286102295)
   (* (sqrt (- (log (- 1.0 u1)))) (cos (* (* 2.0 PI) u2)))
   (*
    (sqrt
     (-
      (-
       (fma
        0.3333333333333333
        (pow u1 3.0)
        (fma 0.5 (* u1 u1) (fma 0.25 (pow u1 4.0) u1))))))
    (cos (log (pow (pow (exp 2.0) PI) 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 tmp;
	if ((1.0f - u1) <= 0.9700000286102295f) {
		tmp = sqrtf(-logf((1.0f - u1))) * cosf(((2.0f * ((float) M_PI)) * u2));
	} else {
		tmp = sqrtf(-(-fmaf(0.3333333333333333f, powf(u1, 3.0f), fmaf(0.5f, (u1 * u1), fmaf(0.25f, powf(u1, 4.0f), u1))))) * cosf(logf(powf(powf(expf(2.0f), ((float) M_PI)), u2)));
	}
	return tmp;
}
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)
	tmp = Float32(0.0)
	if (Float32(Float32(1.0) - u1) <= Float32(0.9700000286102295))
		tmp = Float32(sqrt(Float32(-log(Float32(Float32(1.0) - u1)))) * cos(Float32(Float32(Float32(2.0) * Float32(pi)) * u2)));
	else
		tmp = Float32(sqrt(Float32(-Float32(-fma(Float32(0.3333333333333333), (u1 ^ Float32(3.0)), fma(Float32(0.5), Float32(u1 * u1), fma(Float32(0.25), (u1 ^ Float32(4.0)), u1)))))) * cos(log(((exp(Float32(2.0)) ^ Float32(pi)) ^ u2))));
	end
	return tmp
end
\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.9700000286102295:\\
\;\;\;\;\sqrt{-\log \left(1 - u1\right)} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right)\\

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


\end{array}

Error

Bits error versus cosTheta_i

Bits error versus u1

Bits error versus u2

Derivation

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

    1. Initial program 0.8

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

    if 0.970000029 < (-.f32 1 u1)

    1. Initial program 16.2

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

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

      \[\leadsto \sqrt{-\color{blue}{\left(-\mathsf{fma}\left(0.3333333333333333, {u1}^{3}, \mathsf{fma}\left(0.5, u1 \cdot u1, \mathsf{fma}\left(0.25, {u1}^{4}, u1\right)\right)\right)\right)}} \cdot \cos \left(\left(2 \cdot \pi\right) \cdot u2\right) \]
    4. Applied egg-rr0.3

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

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

Reproduce

herbie shell --seed 2022155 
(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))))