UniformSampleCone, y

Percentage Accurate: 57.4% → 98.3%
Time: 16.6s
Alternatives: 14
Speedup: 5.0×

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\\ \sin \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))))
   (* (sin (* (* 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 sinf(((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(sin(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 = sin(((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\\
\sin \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 14 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.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(1 - ux\right) + ux \cdot maxCos\\ \sin \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))))
   (* (sin (* (* 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 sinf(((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(sin(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 = sin(((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\\
\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - t\_0 \cdot t\_0}
\end{array}
\end{array}

Alternative 1: 98.3% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(maxCos + -1, ux \cdot \left(1 - maxCos\right), -\mathsf{fma}\left(maxCos, -2, 2\right)\right)\\
\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\frac{ux \cdot \left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot t\_0\right)}{t\_0}}
\end{array}
\end{array}
Derivation
  1. Initial program 56.9%

    \[\sin \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 \sin \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 \sin \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 \sin \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. +-commutativeN/A

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

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

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

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

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

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

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

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

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\frac{ux \cdot \left(\mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), \mathsf{fma}\left(maxCos, -2, 2\right)\right) \cdot \mathsf{fma}\left(maxCos + -1, ux \cdot \left(1 - maxCos\right), -\mathsf{fma}\left(maxCos, -2, 2\right)\right)\right)}{\mathsf{fma}\left(maxCos + -1, ux \cdot \left(1 - maxCos\right), -\mathsf{fma}\left(maxCos, -2, 2\right)\right)}} \]
    3. Add Preprocessing

    Alternative 2: 84.0% accurate, 0.7× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 + \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(ux + -1\right) - ux \cdot maxCos\right)} \leq 0.00014000000373926014:\\ \;\;\;\;2 \cdot \left(\left(uy \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 + \mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), maxCos \cdot -2\right)\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(ux, maxCos, 1 - ux\right), \mathsf{fma}\left(-ux, maxCos + -1, -1\right), 1\right)} \cdot \left(uy \cdot \left(\pi \cdot \mathsf{fma}\left(-1.3333333333333333 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 2\right)\right)\right)\\ \end{array} \end{array} \]
    (FPCore (ux uy maxCos)
     :precision binary32
     (if (<=
          (*
           (sin (* (* uy 2.0) PI))
           (sqrt
            (+
             1.0
             (* (+ (- 1.0 ux) (* ux maxCos)) (- (+ ux -1.0) (* ux maxCos))))))
          0.00014000000373926014)
       (*
        2.0
        (*
         (* uy PI)
         (sqrt
          (*
           ux
           (+ 2.0 (fma ux (* (+ maxCos -1.0) (- 1.0 maxCos)) (* maxCos -2.0)))))))
       (*
        (sqrt
         (fma (fma ux maxCos (- 1.0 ux)) (fma (- ux) (+ maxCos -1.0) -1.0) 1.0))
        (* uy (* PI (fma (* -1.3333333333333333 (* uy uy)) (* PI PI) 2.0))))))
    float code(float ux, float uy, float maxCos) {
    	float tmp;
    	if ((sinf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f + (((1.0f - ux) + (ux * maxCos)) * ((ux + -1.0f) - (ux * maxCos)))))) <= 0.00014000000373926014f) {
    		tmp = 2.0f * ((uy * ((float) M_PI)) * sqrtf((ux * (2.0f + fmaf(ux, ((maxCos + -1.0f) * (1.0f - maxCos)), (maxCos * -2.0f))))));
    	} else {
    		tmp = sqrtf(fmaf(fmaf(ux, maxCos, (1.0f - ux)), fmaf(-ux, (maxCos + -1.0f), -1.0f), 1.0f)) * (uy * (((float) M_PI) * fmaf((-1.3333333333333333f * (uy * uy)), (((float) M_PI) * ((float) M_PI)), 2.0f)));
    	}
    	return tmp;
    }
    
    function code(ux, uy, maxCos)
    	tmp = Float32(0.0)
    	if (Float32(sin(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(ux + Float32(-1.0)) - Float32(ux * maxCos)))))) <= Float32(0.00014000000373926014))
    		tmp = Float32(Float32(2.0) * Float32(Float32(uy * Float32(pi)) * sqrt(Float32(ux * Float32(Float32(2.0) + fma(ux, Float32(Float32(maxCos + Float32(-1.0)) * Float32(Float32(1.0) - maxCos)), Float32(maxCos * Float32(-2.0))))))));
    	else
    		tmp = Float32(sqrt(fma(fma(ux, maxCos, Float32(Float32(1.0) - ux)), fma(Float32(-ux), Float32(maxCos + Float32(-1.0)), Float32(-1.0)), Float32(1.0))) * Float32(uy * Float32(Float32(pi) * fma(Float32(Float32(-1.3333333333333333) * Float32(uy * uy)), Float32(Float32(pi) * Float32(pi)), Float32(2.0)))));
    	end
    	return tmp
    end
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 + \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(\left(ux + -1\right) - ux \cdot maxCos\right)} \leq 0.00014000000373926014:\\
    \;\;\;\;2 \cdot \left(\left(uy \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 + \mathsf{fma}\left(ux, \left(maxCos + -1\right) \cdot \left(1 - maxCos\right), maxCos \cdot -2\right)\right)}\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(ux, maxCos, 1 - ux\right), \mathsf{fma}\left(-ux, maxCos + -1, -1\right), 1\right)} \cdot \left(uy \cdot \left(\pi \cdot \mathsf{fma}\left(-1.3333333333333333 \cdot \left(uy \cdot uy\right), \pi \cdot \pi, 2\right)\right)\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if (*.f32 (sin.f32 (*.f32 (*.f32 uy #s(literal 2 binary32)) (PI.f32))) (sqrt.f32 (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)))))) < 1.40000004e-4

      1. Initial program 51.3%

        \[\sin \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 uy around 0

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

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto 2 \cdot \left(\left(uy \cdot \mathsf{PI}\left(\right)\right) \cdot \sqrt{ux \cdot \left(2 + \left(-2 \cdot maxCos + ux \cdot \left(\left(1 + -1 \cdot maxCos\right) \cdot \left(maxCos - 1\right)\right)\right)\right)}\right) \]
        3. Step-by-step derivation
          1. Applied rewrites88.6%

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

          if 1.40000004e-4 < (*.f32 (sin.f32 (*.f32 (*.f32 uy #s(literal 2 binary32)) (PI.f32))) (sqrt.f32 (-.f32 #s(literal 1 binary32) (*.f32 (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos)) (+.f32 (-.f32 #s(literal 1 binary32) ux) (*.f32 ux maxCos))))))

          1. Initial program 79.5%

            \[\sin \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 uy around 0

            \[\leadsto \color{blue}{uy \cdot \left(\frac{-4}{3} \cdot \left(\left({uy}^{2} \cdot {\mathsf{PI}\left(\right)}^{3}\right) \cdot \sqrt{1 - {\left(\left(1 + maxCos \cdot ux\right) - ux\right)}^{2}}\right) + 2 \cdot \left(\mathsf{PI}\left(\right) \cdot \sqrt{1 - {\left(\left(1 + maxCos \cdot ux\right) - ux\right)}^{2}}\right)\right)} \]
          4. Applied rewrites67.2%

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

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

        Reproduce

        ?
        herbie shell --seed 2024229 
        (FPCore (ux uy maxCos)
          :name "UniformSampleCone, y"
          :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)))
          (* (sin (* (* uy 2.0) PI)) (sqrt (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))