UniformSampleCone, y

Percentage Accurate: 57.7% → 98.3%
Time: 4.2s
Alternatives: 8
Speedup: 4.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} 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} \]
(FPCore (ux uy maxCos)
  :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)))
  (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}
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}

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 8 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?

\[\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} 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} \]
(FPCore (ux uy maxCos)
  :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)))
  (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}
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}

Alternative 1: 98.3% accurate, 1.1× speedup?

\[\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)\]
\[\sin \left(\left(\pi + \pi\right) \cdot uy\right) \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \mathsf{fma}\left(ux, maxCos, 2 - ux\right)} \]
(FPCore (ux uy maxCos)
  :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 (* (+ PI PI) uy))
 (sqrt (* (- ux (* maxCos ux)) (fma ux maxCos (- 2.0 ux))))))
float code(float ux, float uy, float maxCos) {
	return sinf(((((float) M_PI) + ((float) M_PI)) * uy)) * sqrtf(((ux - (maxCos * ux)) * fmaf(ux, maxCos, (2.0f - ux))));
}
function code(ux, uy, maxCos)
	return Float32(sin(Float32(Float32(Float32(pi) + Float32(pi)) * uy)) * sqrt(Float32(Float32(ux - Float32(maxCos * ux)) * fma(ux, maxCos, Float32(Float32(2.0) - ux)))))
end
\sin \left(\left(\pi + \pi\right) \cdot uy\right) \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \mathsf{fma}\left(ux, maxCos, 2 - ux\right)}
Derivation
  1. Initial program 57.7%

    \[\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. Step-by-step derivation
    1. Applied rewrites98.3%

      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(ux - maxCos \cdot ux\right) - 0\right) \cdot \left(-\left(\left(ux - maxCos \cdot ux\right) - 2\right)\right)} \]
    2. Step-by-step derivation
      1. Applied rewrites98.3%

        \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(-\left(\left(ux - maxCos \cdot ux\right) - 2\right)\right)} \]
      2. Step-by-step derivation
        1. Applied rewrites98.3%

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

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

          Alternative 2: 97.1% accurate, 1.2× speedup?

          \[\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)\]
          \[\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(2 - ux\right)} \]
          (FPCore (ux uy maxCos)
            :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 (* (- ux (* maxCos ux)) (- 2.0 ux)))))
          float code(float ux, float uy, float maxCos) {
          	return sinf(((uy * 2.0f) * ((float) M_PI))) * sqrtf(((ux - (maxCos * ux)) * (2.0f - ux)));
          }
          
          function code(ux, uy, maxCos)
          	return Float32(sin(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(ux - Float32(maxCos * ux)) * Float32(Float32(2.0) - ux))))
          end
          
          function tmp = code(ux, uy, maxCos)
          	tmp = sin(((uy * single(2.0)) * single(pi))) * sqrt(((ux - (maxCos * ux)) * (single(2.0) - ux)));
          end
          
          \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(2 - ux\right)}
          
          Derivation
          1. Initial program 57.7%

            \[\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. Step-by-step derivation
            1. Applied rewrites98.3%

              \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(\left(ux - maxCos \cdot ux\right) - 0\right) \cdot \left(-\left(\left(ux - maxCos \cdot ux\right) - 2\right)\right)} \]
            2. Step-by-step derivation
              1. Applied rewrites98.3%

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

                \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(2 - ux\right)} \]
              3. Step-by-step derivation
                1. Applied rewrites97.1%

                  \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(2 - ux\right)} \]
                2. Add Preprocessing

                Alternative 3: 95.8% accurate, 1.2× speedup?

                \[\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} \mathbf{if}\;maxCos \leq 5.500000042957254 \cdot 10^{-6}:\\ \;\;\;\;\sqrt{\left(2 - ux\right) \cdot ux} \cdot \sin \left(\pi \cdot \left(uy + uy\right)\right)\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right)\\ \end{array} \]
                (FPCore (ux uy maxCos)
                  :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)))
                  (if (<= maxCos 5.500000042957254e-6)
                  (* (sqrt (* (- 2.0 ux) ux)) (sin (* PI (+ uy uy))))
                  (*
                   2.0
                   (*
                    uy
                    (*
                     PI
                     (sqrt
                      (*
                       ux
                       (-
                        (+ 2.0 (* -1.0 (* ux (pow (- maxCos 1.0) 2.0))))
                        (* 2.0 maxCos)))))))))
                float code(float ux, float uy, float maxCos) {
                	float tmp;
                	if (maxCos <= 5.500000042957254e-6f) {
                		tmp = sqrtf(((2.0f - ux) * ux)) * sinf((((float) M_PI) * (uy + uy)));
                	} else {
                		tmp = 2.0f * (uy * (((float) M_PI) * sqrtf((ux * ((2.0f + (-1.0f * (ux * powf((maxCos - 1.0f), 2.0f)))) - (2.0f * maxCos))))));
                	}
                	return tmp;
                }
                
                function code(ux, uy, maxCos)
                	tmp = Float32(0.0)
                	if (maxCos <= Float32(5.500000042957254e-6))
                		tmp = Float32(sqrt(Float32(Float32(Float32(2.0) - ux) * ux)) * sin(Float32(Float32(pi) * Float32(uy + uy))));
                	else
                		tmp = Float32(Float32(2.0) * Float32(uy * Float32(Float32(pi) * sqrt(Float32(ux * Float32(Float32(Float32(2.0) + Float32(Float32(-1.0) * Float32(ux * (Float32(maxCos - Float32(1.0)) ^ Float32(2.0))))) - Float32(Float32(2.0) * maxCos)))))));
                	end
                	return tmp
                end
                
                function tmp_2 = code(ux, uy, maxCos)
                	tmp = single(0.0);
                	if (maxCos <= single(5.500000042957254e-6))
                		tmp = sqrt(((single(2.0) - ux) * ux)) * sin((single(pi) * (uy + uy)));
                	else
                		tmp = single(2.0) * (uy * (single(pi) * sqrt((ux * ((single(2.0) + (single(-1.0) * (ux * ((maxCos - single(1.0)) ^ single(2.0))))) - (single(2.0) * maxCos))))));
                	end
                	tmp_2 = tmp;
                end
                
                \begin{array}{l}
                \mathbf{if}\;maxCos \leq 5.500000042957254 \cdot 10^{-6}:\\
                \;\;\;\;\sqrt{\left(2 - ux\right) \cdot ux} \cdot \sin \left(\pi \cdot \left(uy + uy\right)\right)\\
                
                \mathbf{else}:\\
                \;\;\;\;2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right)\\
                
                
                \end{array}
                
                Derivation
                1. Split input into 2 regimes
                2. if maxCos < 5.50000004e-6

                  1. Initial program 57.7%

                    \[\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. Step-by-step derivation
                    1. Applied rewrites98.3%

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

                      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)} \]
                    3. Step-by-step derivation
                      1. Applied rewrites92.3%

                        \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)} \]
                      2. Step-by-step derivation
                        1. Applied rewrites92.3%

                          \[\leadsto \sqrt{\left(2 - ux\right) \cdot ux} \cdot \sin \left(\pi \cdot \left(uy + uy\right)\right) \]

                        if 5.50000004e-6 < maxCos

                        1. Initial program 57.7%

                          \[\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. Taylor expanded in uy around 0

                          \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{1 - {\left(\left(1 + maxCos \cdot ux\right) - ux\right)}^{2}}\right)\right) \]
                        3. Step-by-step derivation
                          1. Applied rewrites50.8%

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

                            \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right) \]
                          3. Step-by-step derivation
                            1. Applied rewrites81.9%

                              \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right) \]
                          4. Recombined 2 regimes into one program.
                          5. Add Preprocessing

                          Alternative 4: 89.4% accurate, 1.3× speedup?

                          \[\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} \mathbf{if}\;uy \leq 0.0008299999753944576:\\ \;\;\;\;2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot 2}\\ \end{array} \]
                          (FPCore (ux uy maxCos)
                            :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)))
                            (if (<= uy 0.0008299999753944576)
                            (*
                             2.0
                             (*
                              uy
                              (*
                               PI
                               (sqrt
                                (*
                                 ux
                                 (-
                                  (+ 2.0 (* -1.0 (* ux (pow (- maxCos 1.0) 2.0))))
                                  (* 2.0 maxCos)))))))
                            (* (sin (* (* uy 2.0) PI)) (sqrt (* ux 2.0)))))
                          float code(float ux, float uy, float maxCos) {
                          	float tmp;
                          	if (uy <= 0.0008299999753944576f) {
                          		tmp = 2.0f * (uy * (((float) M_PI) * sqrtf((ux * ((2.0f + (-1.0f * (ux * powf((maxCos - 1.0f), 2.0f)))) - (2.0f * maxCos))))));
                          	} else {
                          		tmp = sinf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((ux * 2.0f));
                          	}
                          	return tmp;
                          }
                          
                          function code(ux, uy, maxCos)
                          	tmp = Float32(0.0)
                          	if (uy <= Float32(0.0008299999753944576))
                          		tmp = Float32(Float32(2.0) * Float32(uy * Float32(Float32(pi) * sqrt(Float32(ux * Float32(Float32(Float32(2.0) + Float32(Float32(-1.0) * Float32(ux * (Float32(maxCos - Float32(1.0)) ^ Float32(2.0))))) - Float32(Float32(2.0) * maxCos)))))));
                          	else
                          		tmp = Float32(sin(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(ux * Float32(2.0))));
                          	end
                          	return tmp
                          end
                          
                          function tmp_2 = code(ux, uy, maxCos)
                          	tmp = single(0.0);
                          	if (uy <= single(0.0008299999753944576))
                          		tmp = single(2.0) * (uy * (single(pi) * sqrt((ux * ((single(2.0) + (single(-1.0) * (ux * ((maxCos - single(1.0)) ^ single(2.0))))) - (single(2.0) * maxCos))))));
                          	else
                          		tmp = sin(((uy * single(2.0)) * single(pi))) * sqrt((ux * single(2.0)));
                          	end
                          	tmp_2 = tmp;
                          end
                          
                          \begin{array}{l}
                          \mathbf{if}\;uy \leq 0.0008299999753944576:\\
                          \;\;\;\;2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right)\\
                          
                          \mathbf{else}:\\
                          \;\;\;\;\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot 2}\\
                          
                          
                          \end{array}
                          
                          Derivation
                          1. Split input into 2 regimes
                          2. if uy < 8.29999975e-4

                            1. Initial program 57.7%

                              \[\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. Taylor expanded in uy around 0

                              \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{1 - {\left(\left(1 + maxCos \cdot ux\right) - ux\right)}^{2}}\right)\right) \]
                            3. Step-by-step derivation
                              1. Applied rewrites50.8%

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

                                \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right) \]
                              3. Step-by-step derivation
                                1. Applied rewrites81.9%

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

                                if 8.29999975e-4 < uy

                                1. Initial program 57.7%

                                  \[\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. Step-by-step derivation
                                  1. Applied rewrites98.3%

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

                                    \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)} \]
                                  3. Step-by-step derivation
                                    1. Applied rewrites92.3%

                                      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)} \]
                                    2. Taylor expanded in ux around 0

                                      \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot 2} \]
                                    3. Step-by-step derivation
                                      1. Applied rewrites72.8%

                                        \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot 2} \]
                                    4. Recombined 2 regimes into one program.
                                    5. Add Preprocessing

                                    Alternative 5: 81.9% accurate, 1.4× speedup?

                                    \[\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)\]
                                    \[2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right) \]
                                    (FPCore (ux uy maxCos)
                                      :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)))
                                      (*
                                     2.0
                                     (*
                                      uy
                                      (*
                                       PI
                                       (sqrt
                                        (*
                                         ux
                                         (-
                                          (+ 2.0 (* -1.0 (* ux (pow (- maxCos 1.0) 2.0))))
                                          (* 2.0 maxCos))))))))
                                    float code(float ux, float uy, float maxCos) {
                                    	return 2.0f * (uy * (((float) M_PI) * sqrtf((ux * ((2.0f + (-1.0f * (ux * powf((maxCos - 1.0f), 2.0f)))) - (2.0f * maxCos))))));
                                    }
                                    
                                    function code(ux, uy, maxCos)
                                    	return Float32(Float32(2.0) * Float32(uy * Float32(Float32(pi) * sqrt(Float32(ux * Float32(Float32(Float32(2.0) + Float32(Float32(-1.0) * Float32(ux * (Float32(maxCos - Float32(1.0)) ^ Float32(2.0))))) - Float32(Float32(2.0) * maxCos)))))))
                                    end
                                    
                                    function tmp = code(ux, uy, maxCos)
                                    	tmp = single(2.0) * (uy * (single(pi) * sqrt((ux * ((single(2.0) + (single(-1.0) * (ux * ((maxCos - single(1.0)) ^ single(2.0))))) - (single(2.0) * maxCos))))));
                                    end
                                    
                                    2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right)
                                    
                                    Derivation
                                    1. Initial program 57.7%

                                      \[\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. Taylor expanded in uy around 0

                                      \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{1 - {\left(\left(1 + maxCos \cdot ux\right) - ux\right)}^{2}}\right)\right) \]
                                    3. Step-by-step derivation
                                      1. Applied rewrites50.8%

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

                                        \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right) \]
                                      3. Step-by-step derivation
                                        1. Applied rewrites81.9%

                                          \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(\left(2 + -1 \cdot \left(ux \cdot {\left(maxCos - 1\right)}^{2}\right)\right) - 2 \cdot maxCos\right)}\right)\right) \]
                                        2. Add Preprocessing

                                        Alternative 6: 81.9% accurate, 2.4× speedup?

                                        \[\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)\]
                                        \[2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(\left(2 + maxCos \cdot ux\right) - ux\right)}\right)\right) \]
                                        (FPCore (ux uy maxCos)
                                          :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)))
                                          (*
                                         2.0
                                         (*
                                          uy
                                          (*
                                           PI
                                           (sqrt (* (- ux (* maxCos ux)) (- (+ 2.0 (* maxCos ux)) ux)))))))
                                        float code(float ux, float uy, float maxCos) {
                                        	return 2.0f * (uy * (((float) M_PI) * sqrtf(((ux - (maxCos * ux)) * ((2.0f + (maxCos * ux)) - ux)))));
                                        }
                                        
                                        function code(ux, uy, maxCos)
                                        	return Float32(Float32(2.0) * Float32(uy * Float32(Float32(pi) * sqrt(Float32(Float32(ux - Float32(maxCos * ux)) * Float32(Float32(Float32(2.0) + Float32(maxCos * ux)) - ux))))))
                                        end
                                        
                                        function tmp = code(ux, uy, maxCos)
                                        	tmp = single(2.0) * (uy * (single(pi) * sqrt(((ux - (maxCos * ux)) * ((single(2.0) + (maxCos * ux)) - ux)))));
                                        end
                                        
                                        2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(\left(2 + maxCos \cdot ux\right) - ux\right)}\right)\right)
                                        
                                        Derivation
                                        1. Initial program 57.7%

                                          \[\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. Step-by-step derivation
                                          1. Applied rewrites98.3%

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

                                            \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)} \]
                                          3. Step-by-step derivation
                                            1. Applied rewrites92.3%

                                              \[\leadsto \sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)} \]
                                            2. Taylor expanded in uy around 0

                                              \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(\left(2 + maxCos \cdot ux\right) - ux\right)}\right)\right) \]
                                            3. Step-by-step derivation
                                              1. Applied rewrites81.9%

                                                \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{\left(ux - maxCos \cdot ux\right) \cdot \left(\left(2 + maxCos \cdot ux\right) - ux\right)}\right)\right) \]
                                              2. Add Preprocessing

                                              Alternative 7: 66.2% accurate, 3.5× speedup?

                                              \[\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)\]
                                              \[\left(\left(uy + uy\right) \cdot \sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux}\right) \cdot \pi \]
                                              (FPCore (ux uy maxCos)
                                                :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)))
                                                (* (* (+ uy uy) (sqrt (* (fma -2.0 maxCos 2.0) ux))) PI))
                                              float code(float ux, float uy, float maxCos) {
                                              	return ((uy + uy) * sqrtf((fmaf(-2.0f, maxCos, 2.0f) * ux))) * ((float) M_PI);
                                              }
                                              
                                              function code(ux, uy, maxCos)
                                              	return Float32(Float32(Float32(uy + uy) * sqrt(Float32(fma(Float32(-2.0), maxCos, Float32(2.0)) * ux))) * Float32(pi))
                                              end
                                              
                                              \left(\left(uy + uy\right) \cdot \sqrt{\mathsf{fma}\left(-2, maxCos, 2\right) \cdot ux}\right) \cdot \pi
                                              
                                              Derivation
                                              1. Initial program 57.7%

                                                \[\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. Taylor expanded in uy around 0

                                                \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{1 - {\left(\left(1 + maxCos \cdot ux\right) - ux\right)}^{2}}\right)\right) \]
                                              3. Step-by-step derivation
                                                1. Applied rewrites50.8%

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

                                                  \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\right)\right) \]
                                                3. Step-by-step derivation
                                                  1. Applied rewrites66.2%

                                                    \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\right)\right) \]
                                                  2. Step-by-step derivation
                                                    1. Applied rewrites66.2%

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

                                                    Alternative 8: 63.6% accurate, 4.8× speedup?

                                                    \[\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)\]
                                                    \[\left(\pi \cdot \left(uy + uy\right)\right) \cdot \sqrt{ux + ux} \]
                                                    (FPCore (ux uy maxCos)
                                                      :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)))
                                                      (* (* PI (+ uy uy)) (sqrt (+ ux ux))))
                                                    float code(float ux, float uy, float maxCos) {
                                                    	return (((float) M_PI) * (uy + uy)) * sqrtf((ux + ux));
                                                    }
                                                    
                                                    function code(ux, uy, maxCos)
                                                    	return Float32(Float32(Float32(pi) * Float32(uy + uy)) * sqrt(Float32(ux + ux)))
                                                    end
                                                    
                                                    function tmp = code(ux, uy, maxCos)
                                                    	tmp = (single(pi) * (uy + uy)) * sqrt((ux + ux));
                                                    end
                                                    
                                                    \left(\pi \cdot \left(uy + uy\right)\right) \cdot \sqrt{ux + ux}
                                                    
                                                    Derivation
                                                    1. Initial program 57.7%

                                                      \[\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. Taylor expanded in uy around 0

                                                      \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{1 - {\left(\left(1 + maxCos \cdot ux\right) - ux\right)}^{2}}\right)\right) \]
                                                    3. Step-by-step derivation
                                                      1. Applied rewrites50.8%

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

                                                        \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\right)\right) \]
                                                      3. Step-by-step derivation
                                                        1. Applied rewrites66.2%

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

                                                          \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{2 \cdot ux}\right)\right) \]
                                                        3. Step-by-step derivation
                                                          1. Applied rewrites63.6%

                                                            \[\leadsto 2 \cdot \left(uy \cdot \left(\pi \cdot \sqrt{2 \cdot ux}\right)\right) \]
                                                          2. Applied rewrites63.6%

                                                            \[\leadsto \left(\pi \cdot \left(uy + uy\right)\right) \cdot \sqrt{ux + ux} \]
                                                          3. Add Preprocessing

                                                          Reproduce

                                                          ?
                                                          herbie shell --seed 2026035 +o sampling:rival3
                                                          (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)))))))