UniformSampleCone, x

Percentage Accurate: 57.7% → 99.0%
Time: 15.4s
Alternatives: 19
Speedup: 9.8×

Specification

?
\[\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)\]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0} \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))))
   (* (cos (* (* uy 2.0) PI)) (sqrt (- 1.0 (* t_0 t_0))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = (1.0f - ux) + (ux * maxCos);
	return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - (t_0 * t_0)));
}
function code(ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))
	return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0))))
end
function tmp = code(ux, uy, maxCos)
	t_0 = (single(1.0) - ux) + (ux * maxCos);
	tmp = cos(((uy * single(2.0)) * single(pi))) * sqrt((single(1.0) - (t_0 * t_0)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}

Sampling outcomes in binary32 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 19 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 57.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0} \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (+ (- 1.0 ux) (* ux maxCos))))
   (* (cos (* (* uy 2.0) PI)) (sqrt (- 1.0 (* t_0 t_0))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = (1.0f - ux) + (ux * maxCos);
	return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - (t_0 * t_0)));
}
function code(ux, uy, maxCos)
	t_0 = Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))
	return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(t_0 * t_0))))
end
function tmp = code(ux, uy, maxCos)
	t_0 = (single(1.0) - ux) + (ux * maxCos);
	tmp = cos(((uy * single(2.0)) * single(pi))) * sqrt((single(1.0) - (t_0 * t_0)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(1 - ux\right) + ux \cdot maxCos\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}

Alternative 1: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), maxCos \cdot -2\right), ux, 2 \cdot ux\right)} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (*
  (cos (* (* uy 2.0) PI))
  (sqrt
   (fma
    (fma (- 1.0 maxCos) (fma ux maxCos (- ux)) (* maxCos -2.0))
    ux
    (* 2.0 ux)))))
float code(float ux, float uy, float maxCos) {
	return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf(fmaf(fmaf((1.0f - maxCos), fmaf(ux, maxCos, -ux), (maxCos * -2.0f)), ux, (2.0f * ux)));
}
function code(ux, uy, maxCos)
	return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(fma(fma(Float32(Float32(1.0) - maxCos), fma(ux, maxCos, Float32(-ux)), Float32(maxCos * Float32(-2.0))), ux, Float32(Float32(2.0) * ux))))
end
\begin{array}{l}

\\
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), maxCos \cdot -2\right), ux, 2 \cdot ux\right)}
\end{array}
Derivation
  1. Initial program 60.9%

    \[\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. Add Preprocessing
  3. Taylor expanded in ux around 0

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
  4. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
    2. cancel-sign-sub-invN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \left(\mathsf{neg}\left(2\right)\right) \cdot maxCos\right)}} \]
    3. metadata-evalN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \color{blue}{-2} \cdot maxCos\right)} \]
    4. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(-2 \cdot maxCos + \left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)\right)}} \]
    5. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{maxCos \cdot -2} + \left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)\right)} \]
    6. lower-fma.f32N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\mathsf{fma}\left(maxCos, -2, 2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)}} \]
    7. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2}\right)} \]
    8. mul-1-negN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\mathsf{neg}\left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)} + 2\right)} \]
    9. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{{\left(maxCos - 1\right)}^{2} \cdot ux}\right)\right) + 2\right)} \]
    10. unpow2N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{\left(\left(maxCos - 1\right) \cdot \left(maxCos - 1\right)\right)} \cdot ux\right)\right) + 2\right)} \]
    11. associate-*l*N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{\left(maxCos - 1\right) \cdot \left(\left(maxCos - 1\right) \cdot ux\right)}\right)\right) + 2\right)} \]
    12. *-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\left(maxCos - 1\right) \cdot \color{blue}{\left(ux \cdot \left(maxCos - 1\right)\right)}\right)\right) + 2\right)} \]
    13. distribute-lft-neg-inN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\mathsf{neg}\left(\left(maxCos - 1\right)\right)\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right)} + 2\right)} \]
    14. neg-sub0N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(0 - \left(maxCos - 1\right)\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    15. associate-+l-N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\left(0 - maxCos\right) + 1\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    16. neg-sub0N/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)} + 1\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    17. mul-1-negN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\color{blue}{-1 \cdot maxCos} + 1\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
    18. +-commutativeN/A

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(1 + -1 \cdot maxCos\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
  5. Applied rewrites98.9%

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)}} \]
  6. Step-by-step derivation
    1. Applied rewrites99.0%

      \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), maxCos \cdot -2\right), \color{blue}{ux}, 2 \cdot ux\right)} \]
    2. Add Preprocessing

    Alternative 2: 97.4% accurate, 0.6× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\left(uy \cdot 2\right) \cdot \pi\right)\\ \mathbf{if}\;t\_0 \leq 0.999750018119812:\\ \;\;\;\;t\_0 \cdot \sqrt{ux \cdot \left(ux \cdot \left(\frac{2}{ux} + -1\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \cdot \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 1\right)\\ \end{array} \end{array} \]
    (FPCore (ux uy maxCos)
     :precision binary32
     (let* ((t_0 (cos (* (* uy 2.0) PI))))
       (if (<= t_0 0.999750018119812)
         (* t_0 (sqrt (* ux (* ux (+ (/ 2.0 ux) -1.0)))))
         (*
          (sqrt
           (*
            ux
            (fma maxCos -2.0 (fma (- 1.0 maxCos) (fma ux maxCos (- ux)) 2.0))))
          (fma (* -2.0 (* uy uy)) (* PI PI) 1.0)))))
    float code(float ux, float uy, float maxCos) {
    	float t_0 = cosf(((uy * 2.0f) * ((float) M_PI)));
    	float tmp;
    	if (t_0 <= 0.999750018119812f) {
    		tmp = t_0 * sqrtf((ux * (ux * ((2.0f / ux) + -1.0f))));
    	} else {
    		tmp = sqrtf((ux * fmaf(maxCos, -2.0f, fmaf((1.0f - maxCos), fmaf(ux, maxCos, -ux), 2.0f)))) * fmaf((-2.0f * (uy * uy)), (((float) M_PI) * ((float) M_PI)), 1.0f);
    	}
    	return tmp;
    }
    
    function code(ux, uy, maxCos)
    	t_0 = cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi)))
    	tmp = Float32(0.0)
    	if (t_0 <= Float32(0.999750018119812))
    		tmp = Float32(t_0 * sqrt(Float32(ux * Float32(ux * Float32(Float32(Float32(2.0) / ux) + Float32(-1.0))))));
    	else
    		tmp = Float32(sqrt(Float32(ux * fma(maxCos, Float32(-2.0), fma(Float32(Float32(1.0) - maxCos), fma(ux, maxCos, Float32(-ux)), Float32(2.0))))) * fma(Float32(Float32(-2.0) * Float32(uy * uy)), Float32(Float32(pi) * Float32(pi)), Float32(1.0)));
    	end
    	return tmp
    end
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := \cos \left(\left(uy \cdot 2\right) \cdot \pi\right)\\
    \mathbf{if}\;t\_0 \leq 0.999750018119812:\\
    \;\;\;\;t\_0 \cdot \sqrt{ux \cdot \left(ux \cdot \left(\frac{2}{ux} + -1\right)\right)}\\
    
    \mathbf{else}:\\
    \;\;\;\;\sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \cdot \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 1\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (cos.f32 (*.f32 (*.f32 uy #s(literal 2 binary32)) (PI.f32))) < 0.999750018

      1. Initial program 58.9%

        \[\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. Add Preprocessing
      3. Taylor expanded in maxCos around 0

        \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{1 - {\left(1 - ux\right)}^{2}}} \]
      4. Step-by-step derivation
        1. sub-negN/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{1 + \left(\mathsf{neg}\left({\left(1 - ux\right)}^{2}\right)\right)}} \]
        2. +-commutativeN/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{\left(\mathsf{neg}\left({\left(1 - ux\right)}^{2}\right)\right) + 1}} \]
        3. unpow2N/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(\mathsf{neg}\left(\color{blue}{\left(1 - ux\right) \cdot \left(1 - ux\right)}\right)\right) + 1} \]
        4. distribute-rgt-neg-inN/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{\left(1 - ux\right) \cdot \left(\mathsf{neg}\left(\left(1 - ux\right)\right)\right)} + 1} \]
        5. mul-1-negN/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\left(1 - ux\right) \cdot \color{blue}{\left(-1 \cdot \left(1 - ux\right)\right)} + 1} \]
        6. lower-fma.f32N/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{\mathsf{fma}\left(1 - ux, -1 \cdot \left(1 - ux\right), 1\right)}} \]
        7. lower--.f32N/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(\color{blue}{1 - ux}, -1 \cdot \left(1 - ux\right), 1\right)} \]
        8. mul-1-negN/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(1 - ux, \color{blue}{\mathsf{neg}\left(\left(1 - ux\right)\right)}, 1\right)} \]
        9. lower-neg.f32N/A

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\mathsf{fma}\left(1 - ux, \color{blue}{\mathsf{neg}\left(\left(1 - ux\right)\right)}, 1\right)} \]
        10. lower--.f3256.7

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\mathsf{fma}\left(1 - ux, -\color{blue}{\left(1 - ux\right)}, 1\right)} \]
      5. Applied rewrites56.7%

        \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{\mathsf{fma}\left(1 - ux, -\left(1 - ux\right), 1\right)}} \]
      6. Taylor expanded in ux around 0

        \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(2 + -1 \cdot ux\right)}} \]
      7. Step-by-step derivation
        1. Applied rewrites91.8%

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \color{blue}{\left(2 - ux\right)}} \]
        2. Taylor expanded in ux around inf

          \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{{ux}^{2} \cdot \color{blue}{\left(2 \cdot \frac{1}{ux} - 1\right)}} \]
        3. Step-by-step derivation
          1. Applied rewrites91.8%

            \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(\frac{2}{ux} + -1\right) \cdot ux\right) \cdot \color{blue}{ux}} \]

          if 0.999750018 < (cos.f32 (*.f32 (*.f32 uy #s(literal 2 binary32)) (PI.f32)))

          1. Initial program 57.4%

            \[\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. Add Preprocessing
          3. Taylor expanded in ux around 0

            \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
          4. Step-by-step derivation
            1. lower-*.f32N/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{\color{blue}{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}} \]
            2. cancel-sign-sub-invN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \left(\mathsf{neg}\left(2\right)\right) \cdot maxCos\right)}} \]
            3. metadata-evalN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) + \color{blue}{-2} \cdot maxCos\right)} \]
            4. +-commutativeN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\left(-2 \cdot maxCos + \left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)\right)}} \]
            5. *-commutativeN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(\color{blue}{maxCos \cdot -2} + \left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)\right)} \]
            6. lower-fma.f32N/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \color{blue}{\mathsf{fma}\left(maxCos, -2, 2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)}} \]
            7. +-commutativeN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{-1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right) + 2}\right)} \]
            8. mul-1-negN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\mathsf{neg}\left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right)} + 2\right)} \]
            9. *-commutativeN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{{\left(maxCos - 1\right)}^{2} \cdot ux}\right)\right) + 2\right)} \]
            10. unpow2N/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{\left(\left(maxCos - 1\right) \cdot \left(maxCos - 1\right)\right)} \cdot ux\right)\right) + 2\right)} \]
            11. associate-*l*N/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\color{blue}{\left(maxCos - 1\right) \cdot \left(\left(maxCos - 1\right) \cdot ux\right)}\right)\right) + 2\right)} \]
            12. *-commutativeN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\mathsf{neg}\left(\left(maxCos - 1\right) \cdot \color{blue}{\left(ux \cdot \left(maxCos - 1\right)\right)}\right)\right) + 2\right)} \]
            13. distribute-lft-neg-inN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\mathsf{neg}\left(\left(maxCos - 1\right)\right)\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right)} + 2\right)} \]
            14. neg-sub0N/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(0 - \left(maxCos - 1\right)\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
            15. associate-+l-N/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(\left(0 - maxCos\right) + 1\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
            16. neg-sub0N/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\color{blue}{\left(\mathsf{neg}\left(maxCos\right)\right)} + 1\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
            17. mul-1-negN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \left(\color{blue}{-1 \cdot maxCos} + 1\right) \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
            18. +-commutativeN/A

              \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \color{blue}{\left(1 + -1 \cdot maxCos\right)} \cdot \left(ux \cdot \left(maxCos - 1\right)\right) + 2\right)} \]
          5. Applied rewrites99.4%

            \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)}} \]
          6. Taylor expanded in uy around 0

            \[\leadsto \color{blue}{\left(1 + -2 \cdot \left({uy}^{2} \cdot {\mathsf{PI}\left(\right)}^{2}\right)\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
          7. Step-by-step derivation
            1. +-commutativeN/A

              \[\leadsto \color{blue}{\left(-2 \cdot \left({uy}^{2} \cdot {\mathsf{PI}\left(\right)}^{2}\right) + 1\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            2. associate-*r*N/A

              \[\leadsto \left(\color{blue}{\left(-2 \cdot {uy}^{2}\right) \cdot {\mathsf{PI}\left(\right)}^{2}} + 1\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            3. lower-fma.f32N/A

              \[\leadsto \color{blue}{\mathsf{fma}\left(-2 \cdot {uy}^{2}, {\mathsf{PI}\left(\right)}^{2}, 1\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            4. lower-*.f32N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{-2 \cdot {uy}^{2}}, {\mathsf{PI}\left(\right)}^{2}, 1\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            5. unpow2N/A

              \[\leadsto \mathsf{fma}\left(-2 \cdot \color{blue}{\left(uy \cdot uy\right)}, {\mathsf{PI}\left(\right)}^{2}, 1\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            6. lower-*.f32N/A

              \[\leadsto \mathsf{fma}\left(-2 \cdot \color{blue}{\left(uy \cdot uy\right)}, {\mathsf{PI}\left(\right)}^{2}, 1\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            7. unpow2N/A

              \[\leadsto \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \color{blue}{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}, 1\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            8. lower-*.f32N/A

              \[\leadsto \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \color{blue}{\mathsf{PI}\left(\right) \cdot \mathsf{PI}\left(\right)}, 1\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            9. lower-PI.f32N/A

              \[\leadsto \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \color{blue}{\mathsf{PI}\left(\right)} \cdot \mathsf{PI}\left(\right), 1\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, \mathsf{neg}\left(ux\right)\right), 2\right)\right)} \]
            10. lower-PI.f3299.4

              \[\leadsto \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \color{blue}{\pi}, 1\right) \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \]
          8. Applied rewrites99.4%

            \[\leadsto \color{blue}{\mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 1\right)} \cdot \sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \]
        4. Recombined 2 regimes into one program.
        5. Final simplification97.4%

          \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \leq 0.999750018119812:\\ \;\;\;\;\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(ux \cdot \left(\frac{2}{ux} + -1\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{ux \cdot \mathsf{fma}\left(maxCos, -2, \mathsf{fma}\left(1 - maxCos, \mathsf{fma}\left(ux, maxCos, -ux\right), 2\right)\right)} \cdot \mathsf{fma}\left(-2 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 1\right)\\ \end{array} \]
        6. Add Preprocessing

        Reproduce

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