UniformSampleCone, x

Percentage Accurate: 58.1% → 98.9%
Time: 17.3s
Alternatives: 14
Speedup: 3.1×

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 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: 58.1% 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: 98.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \cos \left(2 \cdot \left(uy \cdot {\left(\sqrt[3]{\pi}\right)}^{3}\right)\right) \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right) + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (*
  (cos (* 2.0 (* uy (pow (cbrt PI) 3.0))))
  (sqrt
   (+
    (* ux (- 2.0 (* 2.0 maxCos)))
    (* (pow ux 2.0) (* (- 1.0 maxCos) (+ maxCos -1.0)))))))
float code(float ux, float uy, float maxCos) {
	return cosf((2.0f * (uy * powf(cbrtf(((float) M_PI)), 3.0f)))) * sqrtf(((ux * (2.0f - (2.0f * maxCos))) + (powf(ux, 2.0f) * ((1.0f - maxCos) * (maxCos + -1.0f)))));
}
function code(ux, uy, maxCos)
	return Float32(cos(Float32(Float32(2.0) * Float32(uy * (cbrt(Float32(pi)) ^ Float32(3.0))))) * sqrt(Float32(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos))) + Float32((ux ^ Float32(2.0)) * Float32(Float32(Float32(1.0) - maxCos) * Float32(maxCos + Float32(-1.0)))))))
end
\begin{array}{l}

\\
\cos \left(2 \cdot \left(uy \cdot {\left(\sqrt[3]{\pi}\right)}^{3}\right)\right) \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right) + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 96.6% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\cos \left(\pi \cdot \left(2 \cdot uy\right)\right) \leq 0.9999994039535522:\\ \;\;\;\;\cos \left(2 \cdot \left(uy \cdot \pi\right)\right) \cdot \sqrt{2 \cdot ux - {ux}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(ux, 2 + maxCos \cdot -2, {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (if (<= (cos (* PI (* 2.0 uy))) 0.9999994039535522)
   (* (cos (* 2.0 (* uy PI))) (sqrt (- (* 2.0 ux) (pow ux 2.0))))
   (sqrt
    (fma
     ux
     (+ 2.0 (* maxCos -2.0))
     (* (pow ux 2.0) (* (- 1.0 maxCos) (+ maxCos -1.0)))))))
float code(float ux, float uy, float maxCos) {
	float tmp;
	if (cosf((((float) M_PI) * (2.0f * uy))) <= 0.9999994039535522f) {
		tmp = cosf((2.0f * (uy * ((float) M_PI)))) * sqrtf(((2.0f * ux) - powf(ux, 2.0f)));
	} else {
		tmp = sqrtf(fmaf(ux, (2.0f + (maxCos * -2.0f)), (powf(ux, 2.0f) * ((1.0f - maxCos) * (maxCos + -1.0f)))));
	}
	return tmp;
}
function code(ux, uy, maxCos)
	tmp = Float32(0.0)
	if (cos(Float32(Float32(pi) * Float32(Float32(2.0) * uy))) <= Float32(0.9999994039535522))
		tmp = Float32(cos(Float32(Float32(2.0) * Float32(uy * Float32(pi)))) * sqrt(Float32(Float32(Float32(2.0) * ux) - (ux ^ Float32(2.0)))));
	else
		tmp = sqrt(fma(ux, Float32(Float32(2.0) + Float32(maxCos * Float32(-2.0))), Float32((ux ^ Float32(2.0)) * Float32(Float32(Float32(1.0) - maxCos) * Float32(maxCos + Float32(-1.0))))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\cos \left(\pi \cdot \left(2 \cdot uy\right)\right) \leq 0.9999994039535522:\\
\;\;\;\;\cos \left(2 \cdot \left(uy \cdot \pi\right)\right) \cdot \sqrt{2 \cdot ux - {ux}^{2}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(ux, 2 + maxCos \cdot -2, {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)\right)}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 99.1% accurate, 0.6× speedup?

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

\\
\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{\mathsf{fma}\left(ux, \left(1 - maxCos\right) + \left(1 - maxCos\right), {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 90.7% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\pi \cdot \left(2 \cdot uy\right)\right)\\ \mathbf{if}\;t_0 \leq 0.9999939799308777:\\ \;\;\;\;t_0 \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(ux, 2 + maxCos \cdot -2, {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (cos (* PI (* 2.0 uy)))))
   (if (<= t_0 0.9999939799308777)
     (* t_0 (sqrt (* ux (- 2.0 (* 2.0 maxCos)))))
     (sqrt
      (fma
       ux
       (+ 2.0 (* maxCos -2.0))
       (* (pow ux 2.0) (* (- 1.0 maxCos) (+ maxCos -1.0))))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = cosf((((float) M_PI) * (2.0f * uy)));
	float tmp;
	if (t_0 <= 0.9999939799308777f) {
		tmp = t_0 * sqrtf((ux * (2.0f - (2.0f * maxCos))));
	} else {
		tmp = sqrtf(fmaf(ux, (2.0f + (maxCos * -2.0f)), (powf(ux, 2.0f) * ((1.0f - maxCos) * (maxCos + -1.0f)))));
	}
	return tmp;
}
function code(ux, uy, maxCos)
	t_0 = cos(Float32(Float32(pi) * Float32(Float32(2.0) * uy)))
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.9999939799308777))
		tmp = Float32(t_0 * sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos)))));
	else
		tmp = sqrt(fma(ux, Float32(Float32(2.0) + Float32(maxCos * Float32(-2.0))), Float32((ux ^ Float32(2.0)) * Float32(Float32(Float32(1.0) - maxCos) * Float32(maxCos + Float32(-1.0))))));
	end
	return tmp
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\pi \cdot \left(2 \cdot uy\right)\right)\\
\mathbf{if}\;t_0 \leq 0.9999939799308777:\\
\;\;\;\;t_0 \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(ux, 2 + maxCos \cdot -2, {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)\right)}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 90.7% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\pi \cdot \left(2 \cdot uy\right)\right)\\ t_1 := ux \cdot \left(2 - 2 \cdot maxCos\right)\\ \mathbf{if}\;t_0 \leq 0.9999939799308777:\\ \;\;\;\;t_0 \cdot \sqrt{t_1}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{t_1 + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (cos (* PI (* 2.0 uy)))) (t_1 (* ux (- 2.0 (* 2.0 maxCos)))))
   (if (<= t_0 0.9999939799308777)
     (* t_0 (sqrt t_1))
     (sqrt (+ t_1 (* (pow ux 2.0) (* (- 1.0 maxCos) (+ maxCos -1.0))))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = cosf((((float) M_PI) * (2.0f * uy)));
	float t_1 = ux * (2.0f - (2.0f * maxCos));
	float tmp;
	if (t_0 <= 0.9999939799308777f) {
		tmp = t_0 * sqrtf(t_1);
	} else {
		tmp = sqrtf((t_1 + (powf(ux, 2.0f) * ((1.0f - maxCos) * (maxCos + -1.0f)))));
	}
	return tmp;
}
function code(ux, uy, maxCos)
	t_0 = cos(Float32(Float32(pi) * Float32(Float32(2.0) * uy)))
	t_1 = Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos)))
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.9999939799308777))
		tmp = Float32(t_0 * sqrt(t_1));
	else
		tmp = sqrt(Float32(t_1 + Float32((ux ^ Float32(2.0)) * Float32(Float32(Float32(1.0) - maxCos) * Float32(maxCos + Float32(-1.0))))));
	end
	return tmp
end
function tmp_2 = code(ux, uy, maxCos)
	t_0 = cos((single(pi) * (single(2.0) * uy)));
	t_1 = ux * (single(2.0) - (single(2.0) * maxCos));
	tmp = single(0.0);
	if (t_0 <= single(0.9999939799308777))
		tmp = t_0 * sqrt(t_1);
	else
		tmp = sqrt((t_1 + ((ux ^ single(2.0)) * ((single(1.0) - maxCos) * (maxCos + single(-1.0))))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\pi \cdot \left(2 \cdot uy\right)\right)\\
t_1 := ux \cdot \left(2 - 2 \cdot maxCos\right)\\
\mathbf{if}\;t_0 \leq 0.9999939799308777:\\
\;\;\;\;t_0 \cdot \sqrt{t_1}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{t_1 + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 89.7% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\pi \cdot \left(2 \cdot uy\right)\right)\\ \mathbf{if}\;t_0 \leq 0.9999499917030334:\\ \;\;\;\;t_0 \cdot \sqrt{2 \cdot ux}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right) + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (cos (* PI (* 2.0 uy)))))
   (if (<= t_0 0.9999499917030334)
     (* t_0 (sqrt (* 2.0 ux)))
     (sqrt
      (+
       (* ux (- 2.0 (* 2.0 maxCos)))
       (* (pow ux 2.0) (* (- 1.0 maxCos) (+ maxCos -1.0))))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = cosf((((float) M_PI) * (2.0f * uy)));
	float tmp;
	if (t_0 <= 0.9999499917030334f) {
		tmp = t_0 * sqrtf((2.0f * ux));
	} else {
		tmp = sqrtf(((ux * (2.0f - (2.0f * maxCos))) + (powf(ux, 2.0f) * ((1.0f - maxCos) * (maxCos + -1.0f)))));
	}
	return tmp;
}
function code(ux, uy, maxCos)
	t_0 = cos(Float32(Float32(pi) * Float32(Float32(2.0) * uy)))
	tmp = Float32(0.0)
	if (t_0 <= Float32(0.9999499917030334))
		tmp = Float32(t_0 * sqrt(Float32(Float32(2.0) * ux)));
	else
		tmp = sqrt(Float32(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos))) + Float32((ux ^ Float32(2.0)) * Float32(Float32(Float32(1.0) - maxCos) * Float32(maxCos + Float32(-1.0))))));
	end
	return tmp
end
function tmp_2 = code(ux, uy, maxCos)
	t_0 = cos((single(pi) * (single(2.0) * uy)));
	tmp = single(0.0);
	if (t_0 <= single(0.9999499917030334))
		tmp = t_0 * sqrt((single(2.0) * ux));
	else
		tmp = sqrt(((ux * (single(2.0) - (single(2.0) * maxCos))) + ((ux ^ single(2.0)) * ((single(1.0) - maxCos) * (maxCos + single(-1.0))))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\pi \cdot \left(2 \cdot uy\right)\right)\\
\mathbf{if}\;t_0 \leq 0.9999499917030334:\\
\;\;\;\;t_0 \cdot \sqrt{2 \cdot ux}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right) + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 99.0% accurate, 0.8× speedup?

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

\\
\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right) + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)} \cdot \cos \left(2 \cdot \left(uy \cdot \pi\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 80.5% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right) + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (sqrt
  (+
   (* ux (- 2.0 (* 2.0 maxCos)))
   (* (pow ux 2.0) (* (- 1.0 maxCos) (+ maxCos -1.0))))))
float code(float ux, float uy, float maxCos) {
	return sqrtf(((ux * (2.0f - (2.0f * maxCos))) + (powf(ux, 2.0f) * ((1.0f - maxCos) * (maxCos + -1.0f)))));
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = sqrt(((ux * (2.0e0 - (2.0e0 * maxcos))) + ((ux ** 2.0e0) * ((1.0e0 - maxcos) * (maxcos + (-1.0e0))))))
end function
function code(ux, uy, maxCos)
	return sqrt(Float32(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos))) + Float32((ux ^ Float32(2.0)) * Float32(Float32(Float32(1.0) - maxCos) * Float32(maxCos + Float32(-1.0))))))
end
function tmp = code(ux, uy, maxCos)
	tmp = sqrt(((ux * (single(2.0) - (single(2.0) * maxCos))) + ((ux ^ single(2.0)) * ((single(1.0) - maxCos) * (maxCos + single(-1.0))))));
end
\begin{array}{l}

\\
\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right) + {ux}^{2} \cdot \left(\left(1 - maxCos\right) \cdot \left(maxCos + -1\right)\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 9: 75.6% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := ux \cdot \left(maxCos + -1\right)\\ \mathbf{if}\;ux \leq 0.00014000000373926014:\\ \;\;\;\;\sqrt{ux \cdot \left(--2\right) - 2 \cdot \left(ux \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1 + \left(1 + t_0\right) \cdot \left(-1 - t_0\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (* ux (+ maxCos -1.0))))
   (if (<= ux 0.00014000000373926014)
     (sqrt (- (* ux (- -2.0)) (* 2.0 (* ux maxCos))))
     (sqrt (+ 1.0 (* (+ 1.0 t_0) (- -1.0 t_0)))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = ux * (maxCos + -1.0f);
	float tmp;
	if (ux <= 0.00014000000373926014f) {
		tmp = sqrtf(((ux * -(-2.0f)) - (2.0f * (ux * maxCos))));
	} else {
		tmp = sqrtf((1.0f + ((1.0f + t_0) * (-1.0f - t_0))));
	}
	return tmp;
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    real(4) :: t_0
    real(4) :: tmp
    t_0 = ux * (maxcos + (-1.0e0))
    if (ux <= 0.00014000000373926014e0) then
        tmp = sqrt(((ux * -(-2.0e0)) - (2.0e0 * (ux * maxcos))))
    else
        tmp = sqrt((1.0e0 + ((1.0e0 + t_0) * ((-1.0e0) - t_0))))
    end if
    code = tmp
end function
function code(ux, uy, maxCos)
	t_0 = Float32(ux * Float32(maxCos + Float32(-1.0)))
	tmp = Float32(0.0)
	if (ux <= Float32(0.00014000000373926014))
		tmp = sqrt(Float32(Float32(ux * Float32(-Float32(-2.0))) - Float32(Float32(2.0) * Float32(ux * maxCos))));
	else
		tmp = sqrt(Float32(Float32(1.0) + Float32(Float32(Float32(1.0) + t_0) * Float32(Float32(-1.0) - t_0))));
	end
	return tmp
end
function tmp_2 = code(ux, uy, maxCos)
	t_0 = ux * (maxCos + single(-1.0));
	tmp = single(0.0);
	if (ux <= single(0.00014000000373926014))
		tmp = sqrt(((ux * -single(-2.0)) - (single(2.0) * (ux * maxCos))));
	else
		tmp = sqrt((single(1.0) + ((single(1.0) + t_0) * (single(-1.0) - t_0))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := ux \cdot \left(maxCos + -1\right)\\
\mathbf{if}\;ux \leq 0.00014000000373926014:\\
\;\;\;\;\sqrt{ux \cdot \left(--2\right) - 2 \cdot \left(ux \cdot maxCos\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{1 + \left(1 + t_0\right) \cdot \left(-1 - t_0\right)}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 10: 74.3% accurate, 2.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;ux \leq 0.0002300000051036477:\\ \;\;\;\;\sqrt{ux \cdot \left(--2\right) - 2 \cdot \left(ux \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1 - \left(1 + ux \cdot \left(maxCos + -1\right)\right) \cdot \left(1 - ux\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (if (<= ux 0.0002300000051036477)
   (sqrt (- (* ux (- -2.0)) (* 2.0 (* ux maxCos))))
   (sqrt (- 1.0 (* (+ 1.0 (* ux (+ maxCos -1.0))) (- 1.0 ux))))))
float code(float ux, float uy, float maxCos) {
	float tmp;
	if (ux <= 0.0002300000051036477f) {
		tmp = sqrtf(((ux * -(-2.0f)) - (2.0f * (ux * maxCos))));
	} else {
		tmp = sqrtf((1.0f - ((1.0f + (ux * (maxCos + -1.0f))) * (1.0f - ux))));
	}
	return tmp;
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    real(4) :: tmp
    if (ux <= 0.0002300000051036477e0) then
        tmp = sqrt(((ux * -(-2.0e0)) - (2.0e0 * (ux * maxcos))))
    else
        tmp = sqrt((1.0e0 - ((1.0e0 + (ux * (maxcos + (-1.0e0)))) * (1.0e0 - ux))))
    end if
    code = tmp
end function
function code(ux, uy, maxCos)
	tmp = Float32(0.0)
	if (ux <= Float32(0.0002300000051036477))
		tmp = sqrt(Float32(Float32(ux * Float32(-Float32(-2.0))) - Float32(Float32(2.0) * Float32(ux * maxCos))));
	else
		tmp = sqrt(Float32(Float32(1.0) - Float32(Float32(Float32(1.0) + Float32(ux * Float32(maxCos + Float32(-1.0)))) * Float32(Float32(1.0) - ux))));
	end
	return tmp
end
function tmp_2 = code(ux, uy, maxCos)
	tmp = single(0.0);
	if (ux <= single(0.0002300000051036477))
		tmp = sqrt(((ux * -single(-2.0)) - (single(2.0) * (ux * maxCos))));
	else
		tmp = sqrt((single(1.0) - ((single(1.0) + (ux * (maxCos + single(-1.0)))) * (single(1.0) - ux))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;ux \leq 0.0002300000051036477:\\
\;\;\;\;\sqrt{ux \cdot \left(--2\right) - 2 \cdot \left(ux \cdot maxCos\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{1 - \left(1 + ux \cdot \left(maxCos + -1\right)\right) \cdot \left(1 - ux\right)}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 11: 74.1% accurate, 2.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;ux \leq 0.0002300000051036477:\\ \;\;\;\;\sqrt{ux \cdot \left(--2\right) - 2 \cdot \left(ux \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1 + \left(1 - ux\right) \cdot \left(ux + -1\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (if (<= ux 0.0002300000051036477)
   (sqrt (- (* ux (- -2.0)) (* 2.0 (* ux maxCos))))
   (sqrt (+ 1.0 (* (- 1.0 ux) (+ ux -1.0))))))
float code(float ux, float uy, float maxCos) {
	float tmp;
	if (ux <= 0.0002300000051036477f) {
		tmp = sqrtf(((ux * -(-2.0f)) - (2.0f * (ux * maxCos))));
	} else {
		tmp = sqrtf((1.0f + ((1.0f - ux) * (ux + -1.0f))));
	}
	return tmp;
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    real(4) :: tmp
    if (ux <= 0.0002300000051036477e0) then
        tmp = sqrt(((ux * -(-2.0e0)) - (2.0e0 * (ux * maxcos))))
    else
        tmp = sqrt((1.0e0 + ((1.0e0 - ux) * (ux + (-1.0e0)))))
    end if
    code = tmp
end function
function code(ux, uy, maxCos)
	tmp = Float32(0.0)
	if (ux <= Float32(0.0002300000051036477))
		tmp = sqrt(Float32(Float32(ux * Float32(-Float32(-2.0))) - Float32(Float32(2.0) * Float32(ux * maxCos))));
	else
		tmp = sqrt(Float32(Float32(1.0) + Float32(Float32(Float32(1.0) - ux) * Float32(ux + Float32(-1.0)))));
	end
	return tmp
end
function tmp_2 = code(ux, uy, maxCos)
	tmp = single(0.0);
	if (ux <= single(0.0002300000051036477))
		tmp = sqrt(((ux * -single(-2.0)) - (single(2.0) * (ux * maxCos))));
	else
		tmp = sqrt((single(1.0) + ((single(1.0) - ux) * (ux + single(-1.0)))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;ux \leq 0.0002300000051036477:\\
\;\;\;\;\sqrt{ux \cdot \left(--2\right) - 2 \cdot \left(ux \cdot maxCos\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{1 + \left(1 - ux\right) \cdot \left(ux + -1\right)}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 12: 64.9% accurate, 2.9× speedup?

\[\begin{array}{l} \\ \sqrt{ux \cdot \left(--2\right) - 2 \cdot \left(ux \cdot maxCos\right)} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (sqrt (- (* ux (- -2.0)) (* 2.0 (* ux maxCos)))))
float code(float ux, float uy, float maxCos) {
	return sqrtf(((ux * -(-2.0f)) - (2.0f * (ux * maxCos))));
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = sqrt(((ux * -(-2.0e0)) - (2.0e0 * (ux * maxcos))))
end function
function code(ux, uy, maxCos)
	return sqrt(Float32(Float32(ux * Float32(-Float32(-2.0))) - Float32(Float32(2.0) * Float32(ux * maxCos))))
end
function tmp = code(ux, uy, maxCos)
	tmp = sqrt(((ux * -single(-2.0)) - (single(2.0) * (ux * maxCos))));
end
\begin{array}{l}

\\
\sqrt{ux \cdot \left(--2\right) - 2 \cdot \left(ux \cdot maxCos\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 13: 64.9% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (sqrt (* ux (- 2.0 (* 2.0 maxCos)))))
float code(float ux, float uy, float maxCos) {
	return sqrtf((ux * (2.0f - (2.0f * maxCos))));
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = sqrt((ux * (2.0e0 - (2.0e0 * maxcos))))
end function
function code(ux, uy, maxCos)
	return sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos))))
end
function tmp = code(ux, uy, maxCos)
	tmp = sqrt((ux * (single(2.0) - (single(2.0) * maxCos))));
end
\begin{array}{l}

\\
\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 14: 62.4% accurate, 3.1× speedup?

\[\begin{array}{l} \\ \sqrt{ux \cdot \left(--2\right)} \end{array} \]
(FPCore (ux uy maxCos) :precision binary32 (sqrt (* ux (- -2.0))))
float code(float ux, float uy, float maxCos) {
	return sqrtf((ux * -(-2.0f)));
}
real(4) function code(ux, uy, maxcos)
    real(4), intent (in) :: ux
    real(4), intent (in) :: uy
    real(4), intent (in) :: maxcos
    code = sqrt((ux * -(-2.0e0)))
end function
function code(ux, uy, maxCos)
	return sqrt(Float32(ux * Float32(-Float32(-2.0))))
end
function tmp = code(ux, uy, maxCos)
	tmp = sqrt((ux * -single(-2.0)));
end
\begin{array}{l}

\\
\sqrt{ux \cdot \left(--2\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

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