Average Error: 13.7 → 0.3
Time: 8.1s
Precision: binary32
\[\left(\left(2.328306437 \cdot 10^{-10} \leq ux \land ux \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq uy \land uy \leq 1\right)\right) \land \left(0 \leq maxCos \land maxCos \leq 1\right)\]
\[\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
\[\sqrt{2 \cdot \mathsf{fma}\left(maxCos, ux \cdot ux, ux\right) - \left(2 \cdot \left(maxCos \cdot ux\right) + \left(ux \cdot ux\right) \cdot \mathsf{fma}\left(maxCos, maxCos, 1\right)\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \pi\right)\right) \]
(FPCore (ux uy maxCos)
 :precision binary32
 (*
  (cos (* (* uy 2.0) PI))
  (sqrt
   (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))
(FPCore (ux uy maxCos)
 :precision binary32
 (*
  (sqrt
   (-
    (* 2.0 (fma maxCos (* ux ux) ux))
    (+ (* 2.0 (* maxCos ux)) (* (* ux ux) (fma maxCos maxCos 1.0)))))
  (cos (* 2.0 (* uy PI)))))
float code(float ux, float uy, float maxCos) {
	return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - (((1.0f - ux) + (ux * maxCos)) * ((1.0f - ux) + (ux * maxCos)))));
}
float code(float ux, float uy, float maxCos) {
	return sqrtf(((2.0f * fmaf(maxCos, (ux * ux), ux)) - ((2.0f * (maxCos * ux)) + ((ux * ux) * fmaf(maxCos, maxCos, 1.0f))))) * cosf((2.0f * (uy * ((float) M_PI))));
}
function code(ux, uy, maxCos)
	return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos)) * Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))))))
end
function code(ux, uy, maxCos)
	return Float32(sqrt(Float32(Float32(Float32(2.0) * fma(maxCos, Float32(ux * ux), ux)) - Float32(Float32(Float32(2.0) * Float32(maxCos * ux)) + Float32(Float32(ux * ux) * fma(maxCos, maxCos, Float32(1.0)))))) * cos(Float32(Float32(2.0) * Float32(uy * Float32(pi)))))
end
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)}
\sqrt{2 \cdot \mathsf{fma}\left(maxCos, ux \cdot ux, ux\right) - \left(2 \cdot \left(maxCos \cdot ux\right) + \left(ux \cdot ux\right) \cdot \mathsf{fma}\left(maxCos, maxCos, 1\right)\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \pi\right)\right)

Error

Bits error versus ux

Bits error versus uy

Bits error versus maxCos

Derivation

  1. Initial program 13.7

    \[\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(1 - ux\right) + ux \cdot maxCos\right)} \]
  2. Taylor expanded in uy around inf 0.3

    \[\leadsto \color{blue}{\cos \left(2 \cdot \left(\pi \cdot uy\right)\right) \cdot \sqrt{\left(2 \cdot \left(maxCos \cdot {ux}^{2}\right) + 2 \cdot ux\right) - \left({ux}^{2} + \left({maxCos}^{2} \cdot {ux}^{2} + 2 \cdot \left(maxCos \cdot ux\right)\right)\right)}} \]
  3. Simplified0.3

    \[\leadsto \color{blue}{\sqrt{2 \cdot \mathsf{fma}\left(maxCos, ux \cdot ux, ux\right) - \left(2 \cdot \left(maxCos \cdot ux\right) + \mathsf{fma}\left(maxCos, maxCos, 1\right) \cdot \left(ux \cdot ux\right)\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \pi\right)\right)} \]
  4. Final simplification0.3

    \[\leadsto \sqrt{2 \cdot \mathsf{fma}\left(maxCos, ux \cdot ux, ux\right) - \left(2 \cdot \left(maxCos \cdot ux\right) + \left(ux \cdot ux\right) \cdot \mathsf{fma}\left(maxCos, maxCos, 1\right)\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \pi\right)\right) \]

Reproduce

herbie shell --seed 2022160 
(FPCore (ux uy maxCos)
  :name "UniformSampleCone, x"
  :precision binary32
  :pre (and (and (and (<= 2.328306437e-10 ux) (<= ux 1.0)) (and (<= 2.328306437e-10 uy) (<= uy 1.0))) (and (<= 0.0 maxCos) (<= maxCos 1.0)))
  (* (cos (* (* uy 2.0) PI)) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))