UniformSampleCone, x

Percentage Accurate: 56.9% → 99.0%
Time: 18.8s
Alternatives: 17
Speedup: 2.2×

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 17 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: 56.9% 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, 0.7× speedup?

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

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

Alternative 2: 96.4% accurate, 0.5× speedup?

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

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

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


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

Alternative 3: 90.8% accurate, 0.7× 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.9999799728393555:\\ \;\;\;\;t_0 \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{{ux}^{2} \cdot \left(\left(maxCos + -1\right) \cdot \left(1 - maxCos\right)\right) + ux \cdot \left(\left(1 + \left(1 - maxCos\right)\right) - maxCos\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (cos (* PI (* 2.0 uy)))))
   (if (<= t_0 0.9999799728393555)
     (* t_0 (sqrt (* ux (- 2.0 (* 2.0 maxCos)))))
     (sqrt
      (+
       (* (pow ux 2.0) (* (+ maxCos -1.0) (- 1.0 maxCos)))
       (* ux (- (+ 1.0 (- 1.0 maxCos)) maxCos)))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = cosf((((float) M_PI) * (2.0f * uy)));
	float tmp;
	if (t_0 <= 0.9999799728393555f) {
		tmp = t_0 * sqrtf((ux * (2.0f - (2.0f * maxCos))));
	} else {
		tmp = sqrtf(((powf(ux, 2.0f) * ((maxCos + -1.0f) * (1.0f - maxCos))) + (ux * ((1.0f + (1.0f - maxCos)) - maxCos))));
	}
	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.9999799728393555))
		tmp = Float32(t_0 * sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos)))));
	else
		tmp = sqrt(Float32(Float32((ux ^ Float32(2.0)) * Float32(Float32(maxCos + Float32(-1.0)) * Float32(Float32(1.0) - maxCos))) + Float32(ux * Float32(Float32(Float32(1.0) + Float32(Float32(1.0) - maxCos)) - maxCos))));
	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.9999799728393555))
		tmp = t_0 * sqrt((ux * (single(2.0) - (single(2.0) * maxCos))));
	else
		tmp = sqrt((((ux ^ single(2.0)) * ((maxCos + single(-1.0)) * (single(1.0) - maxCos))) + (ux * ((single(1.0) + (single(1.0) - maxCos)) - maxCos))));
	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.9999799728393555:\\
\;\;\;\;t_0 \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\

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


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

Alternative 4: 90.8% accurate, 0.7× 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.9999799728393555:\\ \;\;\;\;t_0 \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{2 \cdot \left(ux \cdot \left(1 - maxCos\right)\right) + {ux}^{2} \cdot \left(\left(maxCos + -1\right) \cdot \left(1 - maxCos\right)\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (let* ((t_0 (cos (* PI (* 2.0 uy)))))
   (if (<= t_0 0.9999799728393555)
     (* t_0 (sqrt (* ux (- 2.0 (* 2.0 maxCos)))))
     (sqrt
      (+
       (* 2.0 (* ux (- 1.0 maxCos)))
       (* (pow ux 2.0) (* (+ maxCos -1.0) (- 1.0 maxCos))))))))
float code(float ux, float uy, float maxCos) {
	float t_0 = cosf((((float) M_PI) * (2.0f * uy)));
	float tmp;
	if (t_0 <= 0.9999799728393555f) {
		tmp = t_0 * sqrtf((ux * (2.0f - (2.0f * maxCos))));
	} else {
		tmp = sqrtf(((2.0f * (ux * (1.0f - maxCos))) + (powf(ux, 2.0f) * ((maxCos + -1.0f) * (1.0f - maxCos)))));
	}
	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.9999799728393555))
		tmp = Float32(t_0 * sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos)))));
	else
		tmp = sqrt(Float32(Float32(Float32(2.0) * Float32(ux * Float32(Float32(1.0) - maxCos))) + Float32((ux ^ Float32(2.0)) * Float32(Float32(maxCos + Float32(-1.0)) * Float32(Float32(1.0) - maxCos)))));
	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.9999799728393555))
		tmp = t_0 * sqrt((ux * (single(2.0) - (single(2.0) * maxCos))));
	else
		tmp = sqrt(((single(2.0) * (ux * (single(1.0) - maxCos))) + ((ux ^ single(2.0)) * ((maxCos + single(-1.0)) * (single(1.0) - maxCos)))));
	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.9999799728393555:\\
\;\;\;\;t_0 \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\

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


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

Alternative 5: 98.4% accurate, 0.7× speedup?

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

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

Alternative 6: 97.7% accurate, 0.7× speedup?

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

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

Alternative 7: 91.3% accurate, 1.0× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;\cos \left(\pi \cdot \left(2 \cdot uy\right)\right) \cdot \sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\


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

Alternative 8: 89.8% accurate, 1.0× speedup?

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

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


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

Alternative 9: 85.4% accurate, 1.0× speedup?

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

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

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


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

Alternative 10: 82.0% accurate, 1.0× speedup?

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

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

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


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

Alternative 11: 75.0% accurate, 1.6× speedup?

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

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

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


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

Alternative 12: 75.0% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;ux \leq 0.0002099999983329326:\\ \;\;\;\;\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1 + \left(\left(1 - ux\right) + ux \cdot maxCos\right) \cdot \left(-1 + ux \cdot \left(1 - maxCos\right)\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (if (<= ux 0.0002099999983329326)
   (sqrt (* ux (- 2.0 (* 2.0 maxCos))))
   (sqrt
    (+ 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ -1.0 (* ux (- 1.0 maxCos))))))))
float code(float ux, float uy, float maxCos) {
	float tmp;
	if (ux <= 0.0002099999983329326f) {
		tmp = sqrtf((ux * (2.0f - (2.0f * maxCos))));
	} else {
		tmp = sqrtf((1.0f + (((1.0f - ux) + (ux * maxCos)) * (-1.0f + (ux * (1.0f - maxCos))))));
	}
	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.0002099999983329326e0) then
        tmp = sqrt((ux * (2.0e0 - (2.0e0 * maxcos))))
    else
        tmp = sqrt((1.0e0 + (((1.0e0 - ux) + (ux * maxcos)) * ((-1.0e0) + (ux * (1.0e0 - maxcos))))))
    end if
    code = tmp
end function
function code(ux, uy, maxCos)
	tmp = Float32(0.0)
	if (ux <= Float32(0.0002099999983329326))
		tmp = sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos))));
	else
		tmp = sqrt(Float32(Float32(1.0) + Float32(Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos)) * Float32(Float32(-1.0) + Float32(ux * Float32(Float32(1.0) - maxCos))))));
	end
	return tmp
end
function tmp_2 = code(ux, uy, maxCos)
	tmp = single(0.0);
	if (ux <= single(0.0002099999983329326))
		tmp = sqrt((ux * (single(2.0) - (single(2.0) * maxCos))));
	else
		tmp = sqrt((single(1.0) + (((single(1.0) - ux) + (ux * maxCos)) * (single(-1.0) + (ux * (single(1.0) - maxCos))))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

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

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


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

Alternative 13: 75.0% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;ux \leq 0.0002099999983329326:\\ \;\;\;\;\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1 + \left(-1 + ux \cdot \left(1 - maxCos\right)\right) \cdot \left(\left(1 + ux \cdot maxCos\right) - ux\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (if (<= ux 0.0002099999983329326)
   (sqrt (* ux (- 2.0 (* 2.0 maxCos))))
   (sqrt
    (+ 1.0 (* (+ -1.0 (* ux (- 1.0 maxCos))) (- (+ 1.0 (* ux maxCos)) ux))))))
float code(float ux, float uy, float maxCos) {
	float tmp;
	if (ux <= 0.0002099999983329326f) {
		tmp = sqrtf((ux * (2.0f - (2.0f * maxCos))));
	} else {
		tmp = sqrtf((1.0f + ((-1.0f + (ux * (1.0f - maxCos))) * ((1.0f + (ux * maxCos)) - 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.0002099999983329326e0) then
        tmp = sqrt((ux * (2.0e0 - (2.0e0 * maxcos))))
    else
        tmp = sqrt((1.0e0 + (((-1.0e0) + (ux * (1.0e0 - maxcos))) * ((1.0e0 + (ux * maxcos)) - ux))))
    end if
    code = tmp
end function
function code(ux, uy, maxCos)
	tmp = Float32(0.0)
	if (ux <= Float32(0.0002099999983329326))
		tmp = sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos))));
	else
		tmp = sqrt(Float32(Float32(1.0) + Float32(Float32(Float32(-1.0) + Float32(ux * Float32(Float32(1.0) - maxCos))) * Float32(Float32(Float32(1.0) + Float32(ux * maxCos)) - ux))));
	end
	return tmp
end
function tmp_2 = code(ux, uy, maxCos)
	tmp = single(0.0);
	if (ux <= single(0.0002099999983329326))
		tmp = sqrt((ux * (single(2.0) - (single(2.0) * maxCos))));
	else
		tmp = sqrt((single(1.0) + ((single(-1.0) + (ux * (single(1.0) - maxCos))) * ((single(1.0) + (ux * maxCos)) - ux))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

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

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


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

Alternative 14: 74.0% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;ux \leq 0.0002099999983329326:\\ \;\;\;\;\sqrt{ux \cdot \left(2 - 2 \cdot maxCos\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{1 + \left(1 - ux\right) \cdot \left(-1 + ux \cdot \left(1 - maxCos\right)\right)}\\ \end{array} \end{array} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (if (<= ux 0.0002099999983329326)
   (sqrt (* ux (- 2.0 (* 2.0 maxCos))))
   (sqrt (+ 1.0 (* (- 1.0 ux) (+ -1.0 (* ux (- 1.0 maxCos))))))))
float code(float ux, float uy, float maxCos) {
	float tmp;
	if (ux <= 0.0002099999983329326f) {
		tmp = sqrtf((ux * (2.0f - (2.0f * maxCos))));
	} else {
		tmp = sqrtf((1.0f + ((1.0f - ux) * (-1.0f + (ux * (1.0f - maxCos))))));
	}
	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.0002099999983329326e0) then
        tmp = sqrt((ux * (2.0e0 - (2.0e0 * maxcos))))
    else
        tmp = sqrt((1.0e0 + ((1.0e0 - ux) * ((-1.0e0) + (ux * (1.0e0 - maxcos))))))
    end if
    code = tmp
end function
function code(ux, uy, maxCos)
	tmp = Float32(0.0)
	if (ux <= Float32(0.0002099999983329326))
		tmp = sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * maxCos))));
	else
		tmp = sqrt(Float32(Float32(1.0) + Float32(Float32(Float32(1.0) - ux) * Float32(Float32(-1.0) + Float32(ux * Float32(Float32(1.0) - maxCos))))));
	end
	return tmp
end
function tmp_2 = code(ux, uy, maxCos)
	tmp = single(0.0);
	if (ux <= single(0.0002099999983329326))
		tmp = sqrt((ux * (single(2.0) - (single(2.0) * maxCos))));
	else
		tmp = sqrt((single(1.0) + ((single(1.0) - ux) * (single(-1.0) + (ux * (single(1.0) - maxCos))))));
	end
	tmp_2 = tmp;
end
\begin{array}{l}

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

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


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

Alternative 15: 73.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;ux \leq 0.0002099999983329326:\\ \;\;\;\;\sqrt{ux \cdot \left(2 - 2 \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.0002099999983329326)
   (sqrt (* ux (- 2.0 (* 2.0 maxCos))))
   (sqrt (+ 1.0 (* (- 1.0 ux) (+ ux -1.0))))))
float code(float ux, float uy, float maxCos) {
	float tmp;
	if (ux <= 0.0002099999983329326f) {
		tmp = sqrtf((ux * (2.0f - (2.0f * 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.0002099999983329326e0) then
        tmp = sqrt((ux * (2.0e0 - (2.0e0 * 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.0002099999983329326))
		tmp = sqrt(Float32(ux * Float32(Float32(2.0) - Float32(Float32(2.0) * 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.0002099999983329326))
		tmp = sqrt((ux * (single(2.0) - (single(2.0) * 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.0002099999983329326:\\
\;\;\;\;\sqrt{ux \cdot \left(2 - 2 \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 16: 64.8% accurate, 2.1× 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 17: 62.3% accurate, 2.2× speedup?

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

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

Reproduce

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