Average Error: 13.6 → 0.3
Time: 18.3s
Precision: binary32
Cost: 13568
\[\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)\]
\[\cos \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)} \]
\[\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\mathsf{fma}\left(ux, maxCos \cdot \left(\left(-2 + 2 \cdot ux\right) - ux \cdot maxCos\right), ux + ux \cdot \left(1 - ux\right)\right)} \]
(FPCore (ux uy maxCos)
 :precision binary32
 (*
  (cos (* (* uy 2.0) PI))
  (sqrt
   (- 1.0 (* (+ (- 1.0 ux) (* ux maxCos)) (+ (- 1.0 ux) (* ux maxCos)))))))
(FPCore (ux uy maxCos)
 :precision binary32
 (*
  (cos (* (* uy 2.0) PI))
  (sqrt
   (fma
    ux
    (* maxCos (- (+ -2.0 (* 2.0 ux)) (* ux maxCos)))
    (+ ux (* ux (- 1.0 ux)))))))
float code(float ux, float uy, float maxCos) {
	return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf((1.0f - (((1.0f - ux) + (ux * maxCos)) * ((1.0f - ux) + (ux * maxCos)))));
}
float code(float ux, float uy, float maxCos) {
	return cosf(((uy * 2.0f) * ((float) M_PI))) * sqrtf(fmaf(ux, (maxCos * ((-2.0f + (2.0f * ux)) - (ux * maxCos))), (ux + (ux * (1.0f - ux)))));
}
function code(ux, uy, maxCos)
	return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(Float32(Float32(1.0) - Float32(Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos)) * Float32(Float32(Float32(1.0) - ux) + Float32(ux * maxCos))))))
end
function code(ux, uy, maxCos)
	return Float32(cos(Float32(Float32(uy * Float32(2.0)) * Float32(pi))) * sqrt(fma(ux, Float32(maxCos * Float32(Float32(Float32(-2.0) + Float32(Float32(2.0) * ux)) - Float32(ux * maxCos))), Float32(ux + Float32(ux * Float32(Float32(1.0) - ux))))))
end
\cos \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)}
\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\mathsf{fma}\left(ux, maxCos \cdot \left(\left(-2 + 2 \cdot ux\right) - ux \cdot maxCos\right), ux + ux \cdot \left(1 - ux\right)\right)}

Error

Derivation

  1. Initial program 13.6

    \[\cos \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 maxCos around 0 13.6

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{\left(1 + \left(-2 \cdot \left(\left(1 - ux\right) \cdot \left(maxCos \cdot ux\right)\right) + -1 \cdot \left({maxCos}^{2} \cdot {ux}^{2}\right)\right)\right) - {\left(1 - ux\right)}^{2}}} \]
  3. Simplified0.3

    \[\leadsto \cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{\color{blue}{\mathsf{fma}\left(ux, maxCos \cdot \left(\left(-2 - ux \cdot -2\right) - maxCos \cdot ux\right), ux + ux \cdot \left(1 - ux\right)\right)}} \]
    Proof
    (fma.f32 ux (*.f32 maxCos (-.f32 (-.f32 -2 (*.f32 ux -2)) (*.f32 maxCos ux))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (*.f32 maxCos (-.f32 (Rewrite<= unsub-neg_binary32 (+.f32 -2 (neg.f32 (*.f32 ux -2)))) (*.f32 maxCos ux))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (*.f32 maxCos (-.f32 (+.f32 -2 (Rewrite<= distribute-lft-neg-out_binary32 (*.f32 (neg.f32 ux) -2))) (*.f32 maxCos ux))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (*.f32 maxCos (-.f32 (+.f32 (Rewrite<= metadata-eval (*.f32 1 -2)) (*.f32 (neg.f32 ux) -2)) (*.f32 maxCos ux))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (*.f32 maxCos (-.f32 (Rewrite<= distribute-rgt-in_binary32 (*.f32 -2 (+.f32 1 (neg.f32 ux)))) (*.f32 maxCos ux))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (*.f32 maxCos (-.f32 (*.f32 -2 (Rewrite<= sub-neg_binary32 (-.f32 1 ux))) (*.f32 maxCos ux))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (Rewrite<= distribute-lft-out--_binary32 (-.f32 (*.f32 maxCos (*.f32 -2 (-.f32 1 ux))) (*.f32 maxCos (*.f32 maxCos ux)))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (Rewrite<= *-commutative_binary32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos)) (*.f32 maxCos (*.f32 maxCos ux))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (Rewrite<= associate-*l*_binary32 (*.f32 (*.f32 maxCos maxCos) ux))) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (Rewrite<= unpow2_binary32 (pow.f32 maxCos 2)) ux)) (+.f32 ux (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (+.f32 (Rewrite<= remove-double-neg_binary32 (neg.f32 (neg.f32 ux))) (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (+.f32 (Rewrite=> neg-sub0_binary32 (-.f32 0 (neg.f32 ux))) (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (+.f32 (-.f32 (Rewrite<= metadata-eval (-.f32 1 1)) (neg.f32 ux)) (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (+.f32 (Rewrite<= associate--r+_binary32 (-.f32 1 (+.f32 1 (neg.f32 ux)))) (*.f32 ux (-.f32 1 ux)))): 134 points increase in error, 91 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (+.f32 (-.f32 1 (Rewrite<= sub-neg_binary32 (-.f32 1 ux))) (*.f32 ux (-.f32 1 ux)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (Rewrite<= cancel-sign-sub_binary32 (-.f32 (-.f32 1 (-.f32 1 ux)) (*.f32 (neg.f32 ux) (-.f32 1 ux))))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (Rewrite<= associate--r+_binary32 (-.f32 1 (+.f32 (-.f32 1 ux) (*.f32 (neg.f32 ux) (-.f32 1 ux)))))): 131 points increase in error, 90 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (-.f32 1 (+.f32 (Rewrite<= *-lft-identity_binary32 (*.f32 1 (-.f32 1 ux))) (*.f32 (neg.f32 ux) (-.f32 1 ux))))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (-.f32 1 (Rewrite<= distribute-rgt-in_binary32 (*.f32 (-.f32 1 ux) (+.f32 1 (neg.f32 ux)))))): 11 points increase in error, 10 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (-.f32 1 (*.f32 (-.f32 1 ux) (Rewrite<= sub-neg_binary32 (-.f32 1 ux))))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (-.f32 1 (Rewrite<= unpow2_binary32 (pow.f32 (-.f32 1 ux) 2)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (Rewrite=> sub-neg_binary32 (+.f32 1 (neg.f32 (pow.f32 (-.f32 1 ux) 2))))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (Rewrite=> +-commutative_binary32 (+.f32 (neg.f32 (pow.f32 (-.f32 1 ux) 2)) 1))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (+.f32 (Rewrite=> neg-sub0_binary32 (-.f32 0 (pow.f32 (-.f32 1 ux) 2))) 1)): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (Rewrite=> associate-+l-_binary32 (-.f32 0 (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)))): 0 points increase in error, 0 points decrease in error
    (fma.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux)) (Rewrite=> sub0-neg_binary32 (neg.f32 (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)))): 0 points increase in error, 0 points decrease in error
    (Rewrite<= fma-neg_binary32 (-.f32 (*.f32 ux (-.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) (*.f32 (pow.f32 maxCos 2) ux))) (-.f32 (pow.f32 (-.f32 1 ux) 2) 1))): 1 points increase in error, 0 points decrease in error
    (-.f32 (Rewrite<= distribute-rgt-out--_binary32 (-.f32 (*.f32 (*.f32 (*.f32 -2 (-.f32 1 ux)) maxCos) ux) (*.f32 (*.f32 (pow.f32 maxCos 2) ux) ux))) (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)): 0 points increase in error, 0 points decrease in error
    (-.f32 (-.f32 (Rewrite<= associate-*r*_binary32 (*.f32 (*.f32 -2 (-.f32 1 ux)) (*.f32 maxCos ux))) (*.f32 (*.f32 (pow.f32 maxCos 2) ux) ux)) (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)): 2 points increase in error, 6 points decrease in error
    (-.f32 (-.f32 (Rewrite<= associate-*r*_binary32 (*.f32 -2 (*.f32 (-.f32 1 ux) (*.f32 maxCos ux)))) (*.f32 (*.f32 (pow.f32 maxCos 2) ux) ux)) (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)): 0 points increase in error, 0 points decrease in error
    (-.f32 (-.f32 (*.f32 -2 (*.f32 (-.f32 1 ux) (*.f32 maxCos ux))) (Rewrite<= associate-*r*_binary32 (*.f32 (pow.f32 maxCos 2) (*.f32 ux ux)))) (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)): 0 points increase in error, 0 points decrease in error
    (-.f32 (-.f32 (*.f32 -2 (*.f32 (-.f32 1 ux) (*.f32 maxCos ux))) (*.f32 (pow.f32 maxCos 2) (Rewrite<= unpow2_binary32 (pow.f32 ux 2)))) (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)): 0 points increase in error, 0 points decrease in error
    (-.f32 (Rewrite<= unsub-neg_binary32 (+.f32 (*.f32 -2 (*.f32 (-.f32 1 ux) (*.f32 maxCos ux))) (neg.f32 (*.f32 (pow.f32 maxCos 2) (pow.f32 ux 2))))) (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)): 0 points increase in error, 0 points decrease in error
    (-.f32 (+.f32 (*.f32 -2 (*.f32 (-.f32 1 ux) (*.f32 maxCos ux))) (Rewrite<= mul-1-neg_binary32 (*.f32 -1 (*.f32 (pow.f32 maxCos 2) (pow.f32 ux 2))))) (-.f32 (pow.f32 (-.f32 1 ux) 2) 1)): 0 points increase in error, 0 points decrease in error
    (Rewrite<= associate-+l-_binary32 (+.f32 (-.f32 (+.f32 (*.f32 -2 (*.f32 (-.f32 1 ux) (*.f32 maxCos ux))) (*.f32 -1 (*.f32 (pow.f32 maxCos 2) (pow.f32 ux 2)))) (pow.f32 (-.f32 1 ux) 2)) 1)): 10 points increase in error, 66 points decrease in error
    (Rewrite<= +-commutative_binary32 (+.f32 1 (-.f32 (+.f32 (*.f32 -2 (*.f32 (-.f32 1 ux) (*.f32 maxCos ux))) (*.f32 -1 (*.f32 (pow.f32 maxCos 2) (pow.f32 ux 2)))) (pow.f32 (-.f32 1 ux) 2)))): 0 points increase in error, 0 points decrease in error
    (Rewrite<= associate--l+_binary32 (-.f32 (+.f32 1 (+.f32 (*.f32 -2 (*.f32 (-.f32 1 ux) (*.f32 maxCos ux))) (*.f32 -1 (*.f32 (pow.f32 maxCos 2) (pow.f32 ux 2))))) (pow.f32 (-.f32 1 ux) 2))): 2 points increase in error, 0 points decrease in error
  4. Final simplification0.3

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

Alternatives

Alternative 1
Error3.4
Cost16420
\[\begin{array}{l} \mathbf{if}\;\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \leq 0.9999651908874512:\\ \;\;\;\;\sqrt{2 \cdot ux} \cdot \cos \left(2 \cdot \left(uy \cdot \pi\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{\left(1 - maxCos\right) \cdot \left(\left(maxCos + -1\right) \cdot {ux}^{2}\right) + ux \cdot \left(\left(1 - \left(maxCos + -1\right)\right) - maxCos\right)}\\ \end{array} \]
Alternative 2
Error0.3
Cost10304
\[\cos \left(uy \cdot \left(2 \cdot \pi\right)\right) \cdot \sqrt{ux \cdot \left(ux \cdot \left(\left(maxCos + -1\right) \cdot \left(1 - maxCos\right)\right) + 2 \cdot \left(1 - maxCos\right)\right)} \]
Alternative 3
Error0.3
Cost10176
\[\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(\left(maxCos + -1\right) \cdot \left(-2 - ux \cdot \left(maxCos + -1\right)\right)\right)} \]
Alternative 4
Error1.3
Cost10052
\[\begin{array}{l} \mathbf{if}\;maxCos \leq 4.999999873689376 \cdot 10^{-5}:\\ \;\;\;\;\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux + ux \cdot \left(1 - ux\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{ux \cdot \left(ux \cdot \left(\left(maxCos + -1\right) \cdot \left(1 - maxCos\right)\right) + 2 \cdot \left(1 - maxCos\right)\right)}\\ \end{array} \]
Alternative 5
Error1.3
Cost9988
\[\begin{array}{l} \mathbf{if}\;maxCos \leq 4.999999873689376 \cdot 10^{-5}:\\ \;\;\;\;\cos \left(\left(uy \cdot 2\right) \cdot \pi\right) \cdot \sqrt{ux \cdot \left(2 - ux\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{ux \cdot \left(ux \cdot \left(\left(maxCos + -1\right) \cdot \left(1 - maxCos\right)\right) + 2 \cdot \left(1 - maxCos\right)\right)}\\ \end{array} \]
Alternative 6
Error6.4
Cost7040
\[\sqrt{\left(1 - maxCos\right) \cdot \left(\left(maxCos + -1\right) \cdot {ux}^{2}\right) + ux \cdot \left(\left(1 - \left(maxCos + -1\right)\right) - maxCos\right)} \]
Alternative 7
Error6.4
Cost3744
\[\sqrt{ux \cdot \left(ux \cdot \left(\left(maxCos + -1\right) \cdot \left(1 - maxCos\right)\right) + 2 \cdot \left(1 - maxCos\right)\right)} \]
Alternative 8
Error11.3
Cost3424
\[\sqrt{ux \cdot \left(2 + maxCos \cdot -2\right)} \]
Alternative 9
Error7.8
Cost3424
\[\sqrt{ux + ux \cdot \left(1 - ux\right)} \]
Alternative 10
Error12.1
Cost3296
\[\sqrt{2 \cdot ux} \]

Error

Reproduce

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