Average Error: 0.7 → 0.5
Time: 10.1s
Precision: binary32
\[\left(0 < cosTheta \land cosTheta < 0.9999\right) \land \left(-1 < c \land c < 1\right)\]
\[\frac{1}{\left(1 + c\right) + \left(\frac{1}{\sqrt{\pi}} \cdot \frac{\sqrt{\left(1 - cosTheta\right) - cosTheta}}{cosTheta}\right) \cdot e^{\left(-cosTheta\right) \cdot cosTheta}} \]
\[\begin{array}{l} t_0 := \sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)\\ \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{\mathsf{fma}\left(t_0, 1 - c \cdot c, \sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)} \cdot \left(1 - c\right)\right)} \cdot \left(t_0 \cdot \left(1 - c\right)\right)\right)\right) \end{array} \]
\frac{1}{\left(1 + c\right) + \left(\frac{1}{\sqrt{\pi}} \cdot \frac{\sqrt{\left(1 - cosTheta\right) - cosTheta}}{cosTheta}\right) \cdot e^{\left(-cosTheta\right) \cdot cosTheta}}
\begin{array}{l}
t_0 := \sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)\\
\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{\mathsf{fma}\left(t_0, 1 - c \cdot c, \sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)} \cdot \left(1 - c\right)\right)} \cdot \left(t_0 \cdot \left(1 - c\right)\right)\right)\right)
\end{array}
(FPCore (cosTheta c)
 :precision binary32
 (/
  1.0
  (+
   (+ 1.0 c)
   (*
    (* (/ 1.0 (sqrt PI)) (/ (sqrt (- (- 1.0 cosTheta) cosTheta)) cosTheta))
    (exp (* (- cosTheta) cosTheta))))))
(FPCore (cosTheta c)
 :precision binary32
 (let* ((t_0 (* (sqrt PI) (* cosTheta (exp (* cosTheta cosTheta))))))
   (expm1
    (log1p
     (*
      (/
       1.0
       (fma t_0 (- 1.0 (* c c)) (* (sqrt (fma cosTheta -2.0 1.0)) (- 1.0 c))))
      (* t_0 (- 1.0 c)))))))
float code(float cosTheta, float c) {
	return 1.0f / ((1.0f + c) + (((1.0f / sqrtf(((float) M_PI))) * (sqrtf(((1.0f - cosTheta) - cosTheta)) / cosTheta)) * expf((-cosTheta * cosTheta))));
}
float code(float cosTheta, float c) {
	float t_0 = sqrtf(((float) M_PI)) * (cosTheta * expf((cosTheta * cosTheta)));
	return expm1f(log1pf(((1.0f / fmaf(t_0, (1.0f - (c * c)), (sqrtf(fmaf(cosTheta, -2.0f, 1.0f)) * (1.0f - c)))) * (t_0 * (1.0f - c)))));
}

Error

Bits error versus cosTheta

Bits error versus c

Derivation

  1. Initial program 0.7

    \[\frac{1}{\left(1 + c\right) + \left(\frac{1}{\sqrt{\pi}} \cdot \frac{\sqrt{\left(1 - cosTheta\right) - cosTheta}}{cosTheta}\right) \cdot e^{\left(-cosTheta\right) \cdot cosTheta}} \]
  2. Simplified0.5

    \[\leadsto \color{blue}{\frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\pi} \cdot \left(cosTheta \cdot {\left(e^{cosTheta}\right)}^{cosTheta}\right)}}} \]
  3. Applied add-cube-cbrt_binary320.5

    \[\leadsto \frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\pi} \cdot \left(cosTheta \cdot \color{blue}{\left(\left(\sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}} \cdot \sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}}\right) \cdot \sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}}\right)}\right)}} \]
  4. Applied associate-*r*_binary320.5

    \[\leadsto \frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\pi} \cdot \color{blue}{\left(\left(cosTheta \cdot \left(\sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}} \cdot \sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}}\right)\right) \cdot \sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}}\right)}}} \]
  5. Applied expm1-log1p-u_binary320.5

    \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\pi} \cdot \left(\left(cosTheta \cdot \left(\sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}} \cdot \sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}}\right)\right) \cdot \sqrt[3]{{\left(e^{cosTheta}\right)}^{cosTheta}}\right)}}\right)\right)} \]
  6. Simplified0.5

    \[\leadsto \mathsf{expm1}\left(\color{blue}{\mathsf{log1p}\left(\frac{1}{\left(1 + c\right) + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)}}\right)}\right) \]
  7. Applied flip-+_binary320.5

    \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{\color{blue}{\frac{1 \cdot 1 - c \cdot c}{1 - c}} + \frac{\sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)}}\right)\right) \]
  8. Applied frac-add_binary320.5

    \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{\color{blue}{\frac{\left(1 \cdot 1 - c \cdot c\right) \cdot \left(\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)\right) + \left(1 - c\right) \cdot \sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}}{\left(1 - c\right) \cdot \left(\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)\right)}}}\right)\right) \]
  9. Applied associate-/r/_binary320.5

    \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\frac{1}{\left(1 \cdot 1 - c \cdot c\right) \cdot \left(\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)\right) + \left(1 - c\right) \cdot \sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)}} \cdot \left(\left(1 - c\right) \cdot \left(\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)\right)\right)}\right)\right) \]
  10. Simplified0.5

    \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{\frac{1}{\mathsf{fma}\left(\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right), 1 - c \cdot c, \sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)} \cdot \left(1 - c\right)\right)}} \cdot \left(\left(1 - c\right) \cdot \left(\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)\right)\right)\right)\right) \]
  11. Final simplification0.5

    \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{\mathsf{fma}\left(\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right), 1 - c \cdot c, \sqrt{\mathsf{fma}\left(cosTheta, -2, 1\right)} \cdot \left(1 - c\right)\right)} \cdot \left(\left(\sqrt{\pi} \cdot \left(cosTheta \cdot e^{cosTheta \cdot cosTheta}\right)\right) \cdot \left(1 - c\right)\right)\right)\right) \]

Reproduce

herbie shell --seed 2022125 
(FPCore (cosTheta c)
  :name "Beckmann Sample, normalization factor"
  :precision binary32
  :pre (and (and (< 0.0 cosTheta) (< cosTheta 0.9999)) (and (< -1.0 c) (< c 1.0)))
  (/ 1.0 (+ (+ 1.0 c) (* (* (/ 1.0 (sqrt PI)) (/ (sqrt (- (- 1.0 cosTheta) cosTheta)) cosTheta)) (exp (* (- cosTheta) cosTheta))))))