UniformSampleCone 2

?

Percentage Accurate: 98.9% → 99.0%
Time: 33.3s
Precision: binary32
Cost: 26720

?

\[\left(\left(\left(\left(\left(-10000 \leq xi \land xi \leq 10000\right) \land \left(-10000 \leq yi \land yi \leq 10000\right)\right) \land \left(-10000 \leq zi \land zi \leq 10000\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq ux \land ux \leq 1\right)\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(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
\[\begin{array}{l} t_0 := uy \cdot \left(2 \cdot \pi\right)\\ t_1 := ux - ux \cdot ux\\ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(maxCos \cdot \left(maxCos \cdot t_1\right)\right) \cdot \left(ux + -1\right), 1\right)}, \mathsf{fma}\left(\cos t_0, xi, \sin t_0 \cdot yi\right), maxCos \cdot \left(t_1 \cdot zi\right)\right) \end{array} \]
(FPCore (xi yi zi ux uy maxCos)
 :precision binary32
 (+
  (+
   (*
    (*
     (cos (* (* uy 2.0) PI))
     (sqrt
      (- 1.0 (* (* (* (- 1.0 ux) maxCos) ux) (* (* (- 1.0 ux) maxCos) ux)))))
    xi)
   (*
    (*
     (sin (* (* uy 2.0) PI))
     (sqrt
      (- 1.0 (* (* (* (- 1.0 ux) maxCos) ux) (* (* (- 1.0 ux) maxCos) ux)))))
    yi))
  (* (* (* (- 1.0 ux) maxCos) ux) zi)))
(FPCore (xi yi zi ux uy maxCos)
 :precision binary32
 (let* ((t_0 (* uy (* 2.0 PI))) (t_1 (- ux (* ux ux))))
   (fma
    (sqrt (fma ux (* (* maxCos (* maxCos t_1)) (+ ux -1.0)) 1.0))
    (fma (cos t_0) xi (* (sin t_0) yi))
    (* maxCos (* t_1 zi)))))
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
	return (((cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - ((((1.0f - ux) * maxCos) * ux) * (((1.0f - ux) * maxCos) * ux))))) * xi) + ((sinf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - ((((1.0f - ux) * maxCos) * ux) * (((1.0f - ux) * maxCos) * ux))))) * yi)) + ((((1.0f - ux) * maxCos) * ux) * zi);
}
float code(float xi, float yi, float zi, float ux, float uy, float maxCos) {
	float t_0 = uy * (2.0f * ((float) M_PI));
	float t_1 = ux - (ux * ux);
	return fmaf(sqrtf(fmaf(ux, ((maxCos * (maxCos * t_1)) * (ux + -1.0f)), 1.0f)), fmaf(cosf(t_0), xi, (sinf(t_0) * yi)), (maxCos * (t_1 * zi)));
}
function code(xi, yi, zi, ux, uy, maxCos)
	return Float32(Float32(Float32(Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(Float32(Float32(Float32(Float32(1.0) - ux) * maxCos) * ux) * Float32(Float32(Float32(Float32(1.0) - ux) * maxCos) * ux))))) * xi) + Float32(Float32(sin(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(Float32(Float32(Float32(Float32(1.0) - ux) * maxCos) * ux) * Float32(Float32(Float32(Float32(1.0) - ux) * maxCos) * ux))))) * yi)) + Float32(Float32(Float32(Float32(Float32(1.0) - ux) * maxCos) * ux) * zi))
end
function code(xi, yi, zi, ux, uy, maxCos)
	t_0 = Float32(uy * Float32(Float32(2.0) * Float32(pi)))
	t_1 = Float32(ux - Float32(ux * ux))
	return fma(sqrt(fma(ux, Float32(Float32(maxCos * Float32(maxCos * t_1)) * Float32(ux + Float32(-1.0))), Float32(1.0))), fma(cos(t_0), xi, Float32(sin(t_0) * yi)), Float32(maxCos * Float32(t_1 * zi)))
end
\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi
\begin{array}{l}
t_0 := uy \cdot \left(2 \cdot \pi\right)\\
t_1 := ux - ux \cdot ux\\
\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(maxCos \cdot \left(maxCos \cdot t_1\right)\right) \cdot \left(ux + -1\right), 1\right)}, \mathsf{fma}\left(\cos t_0, xi, \sin t_0 \cdot yi\right), maxCos \cdot \left(t_1 \cdot zi\right)\right)
\end{array}

Local Percentage Accuracy?

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.

Herbie found 12 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Derivation?

  1. Initial program 99.0%

    \[\left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  2. Simplified99.1%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right) \cdot zi\right)} \]
    Step-by-step derivation

    [Start]99.0

    \[ \left(\left(\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot xi + \left(\sin \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{1 - \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right)}\right) \cdot yi\right) + \left(\left(\left(1 - ux\right) \cdot maxCos\right) \cdot ux\right) \cdot zi \]
  3. Applied egg-rr99.1%

    \[\leadsto \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), \color{blue}{0 + maxCos \cdot \left(\left(ux - ux \cdot ux\right) \cdot zi\right)}\right) \]
    Step-by-step derivation

    [Start]99.1

    \[ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right) \cdot zi\right) \]

    add-log-exp [=>]88.3

    \[ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), \color{blue}{\log \left(e^{\left(maxCos \cdot \left(ux - ux \cdot ux\right)\right) \cdot zi}\right)}\right) \]

    *-un-lft-identity [=>]88.3

    \[ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), \log \color{blue}{\left(1 \cdot e^{\left(maxCos \cdot \left(ux - ux \cdot ux\right)\right) \cdot zi}\right)}\right) \]

    log-prod [=>]88.3

    \[ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), \color{blue}{\log 1 + \log \left(e^{\left(maxCos \cdot \left(ux - ux \cdot ux\right)\right) \cdot zi}\right)}\right) \]

    metadata-eval [=>]88.3

    \[ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), \color{blue}{0} + \log \left(e^{\left(maxCos \cdot \left(ux - ux \cdot ux\right)\right) \cdot zi}\right)\right) \]

    add-log-exp [<=]99.1

    \[ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), 0 + \color{blue}{\left(maxCos \cdot \left(ux - ux \cdot ux\right)\right) \cdot zi}\right) \]

    associate-*l* [=>]99.1

    \[ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(ux + -1\right) \cdot \left(maxCos \cdot \left(maxCos \cdot \left(ux - ux \cdot ux\right)\right)\right), 1\right)}, \mathsf{fma}\left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right), xi, \sin \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot yi\right), 0 + \color{blue}{maxCos \cdot \left(\left(ux - ux \cdot ux\right) \cdot zi\right)}\right) \]
  4. Final simplification99.1%

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

Alternatives

Alternative 1
Accuracy99.0%
Cost26720
\[\begin{array}{l} t_0 := uy \cdot \left(2 \cdot \pi\right)\\ t_1 := maxCos \cdot \left(ux - ux \cdot ux\right)\\ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(maxCos \cdot t_1\right) \cdot \left(ux + -1\right), 1\right)}, \mathsf{fma}\left(\cos t_0, xi, \sin t_0 \cdot yi\right), t_1 \cdot zi\right) \end{array} \]
Alternative 2
Accuracy99.0%
Cost26720
\[\begin{array}{l} t_0 := uy \cdot \left(2 \cdot \pi\right)\\ t_1 := ux - ux \cdot ux\\ \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ux, \left(maxCos \cdot \left(maxCos \cdot t_1\right)\right) \cdot \left(ux + -1\right), 1\right)}, \mathsf{fma}\left(\cos t_0, xi, \sin t_0 \cdot yi\right), t_1 \cdot \left(maxCos \cdot zi\right)\right) \end{array} \]
Alternative 3
Accuracy99.0%
Cost24224
\[\begin{array}{l} t_0 := maxCos \cdot \left(1 - ux\right)\\ t_1 := uy \cdot \left(2 \cdot \pi\right)\\ t_2 := \sqrt{1 + \left(ux \cdot t_0\right) \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)}\\ \mathsf{fma}\left(\cos t_1 \cdot t_2, xi, \sin t_1 \cdot \left(yi \cdot t_2\right)\right) + t_0 \cdot \left(ux \cdot zi\right) \end{array} \]
Alternative 4
Accuracy98.9%
Cost20448
\[\begin{array}{l} t_0 := uy \cdot \left(2 \cdot \pi\right)\\ t_1 := maxCos \cdot \left(1 - ux\right)\\ t_1 \cdot \left(ux \cdot zi\right) + \mathsf{fma}\left(\cos t_0 \cdot \sqrt{1 + \left(ux \cdot t_1\right) \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)}, xi, \sin t_0 \cdot yi\right) \end{array} \]
Alternative 5
Accuracy98.8%
Cost20320
\[\begin{array}{l} t_0 := uy \cdot \left(2 \cdot \pi\right)\\ \left(maxCos \cdot \left(1 - ux\right)\right) \cdot \left(ux \cdot zi\right) + \mathsf{fma}\left(\cos t_0 \cdot \sqrt{1 + \left(ux \cdot maxCos\right) \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)}, xi, \sin t_0 \cdot yi\right) \end{array} \]
Alternative 6
Accuracy98.9%
Cost17312
\[\begin{array}{l} t_0 := \pi \cdot \left(uy \cdot 2\right)\\ t_1 := ux \cdot \left(maxCos \cdot \left(1 - ux\right)\right)\\ \left(xi \cdot \left(\sqrt{1 + t_1 \cdot \left(ux \cdot \left(maxCos \cdot \left(ux + -1\right)\right)\right)} \cdot \cos t_0\right) + yi \cdot \sin t_0\right) + zi \cdot t_1 \end{array} \]
Alternative 7
Accuracy90.3%
Cost17248
\[\mathsf{fma}\left(ux, \left(maxCos \cdot zi\right) \cdot \left(1 - ux\right), \sqrt{1 + ux \cdot \left(ux \cdot \left(maxCos \cdot \left(maxCos \cdot \left(\left(1 - ux\right) \cdot \left(ux + -1\right)\right)\right)\right)\right)} \cdot \left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot xi + 2 \cdot \left(yi \cdot \left(uy \cdot \pi\right)\right)\right)\right) \]
Alternative 8
Accuracy90.1%
Cost16992
\[\mathsf{fma}\left(ux, maxCos \cdot \left(zi - ux \cdot zi\right), \sqrt{1 - ux \cdot \left(ux \cdot \left(maxCos \cdot maxCos\right)\right)} \cdot \left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot xi + 2 \cdot \left(uy \cdot \left(\pi \cdot yi\right)\right)\right)\right) \]
Alternative 9
Accuracy90.1%
Cost16992
\[\mathsf{fma}\left(ux, \left(maxCos \cdot zi\right) \cdot \left(1 - ux\right), \left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot xi + 2 \cdot \left(yi \cdot \left(uy \cdot \pi\right)\right)\right) \cdot \sqrt{1 - ux \cdot \left(ux \cdot \left(maxCos \cdot maxCos\right)\right)}\right) \]
Alternative 10
Accuracy87.4%
Cost16864
\[\mathsf{fma}\left(ux, maxCos \cdot zi, \left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot xi + 2 \cdot \left(uy \cdot \left(\pi \cdot yi\right)\right)\right) \cdot \sqrt{1 - \left(ux \cdot ux\right) \cdot \left(maxCos \cdot maxCos\right)}\right) \]
Alternative 11
Accuracy87.4%
Cost16864
\[\mathsf{fma}\left(ux, maxCos \cdot zi, \left(\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot xi + 2 \cdot \left(yi \cdot \left(uy \cdot \pi\right)\right)\right) \cdot \sqrt{1 - ux \cdot \left(ux \cdot \left(maxCos \cdot maxCos\right)\right)}\right) \]

Reproduce?

herbie shell --seed 2023161 
(FPCore (xi yi zi ux uy maxCos)
  :name "UniformSampleCone 2"
  :precision binary32
  :pre (and (and (and (and (and (and (<= -10000.0 xi) (<= xi 10000.0)) (and (<= -10000.0 yi) (<= yi 10000.0))) (and (<= -10000.0 zi) (<= zi 10000.0))) (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) maxCos) ux) (* (* (- 1.0 ux) maxCos) ux))))) xi) (* (* (sin (* (* uy 2.0) PI)) (sqrt (- 1.0 (* (* (* (- 1.0 ux) maxCos) ux) (* (* (- 1.0 ux) maxCos) ux))))) yi)) (* (* (* (- 1.0 ux) maxCos) ux) zi)))