Beckmann Sample, normalization factor

Percentage Accurate: 97.8% → 97.9%
Time: 2.3s
Alternatives: 7
Speedup: 1.0×

Specification

?
\[\left(0 < cosTheta \land cosTheta < 0.9999\right) \land \left(-1 < c \land c < 1\right)\]
\[\begin{array}{l} \\ \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}} \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))))))
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))));
}
function code(cosTheta, c)
	return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(Float32(Float32(Float32(1.0) / sqrt(Float32(pi))) * Float32(sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp(Float32(Float32(-cosTheta) * cosTheta)))))
end
function tmp = code(cosTheta, c)
	tmp = single(1.0) / ((single(1.0) + c) + (((single(1.0) / sqrt(single(pi))) * (sqrt(((single(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp((-cosTheta * cosTheta))));
end
\begin{array}{l}

\\
\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}}
\end{array}

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 7 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: 97.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \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}} \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))))))
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))));
}
function code(cosTheta, c)
	return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(Float32(Float32(Float32(1.0) / sqrt(Float32(pi))) * Float32(sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp(Float32(Float32(-cosTheta) * cosTheta)))))
end
function tmp = code(cosTheta, c)
	tmp = single(1.0) / ((single(1.0) + c) + (((single(1.0) / sqrt(single(pi))) * (sqrt(((single(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp((-cosTheta * cosTheta))));
end
\begin{array}{l}

\\
\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}}
\end{array}

Alternative 1: 97.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)} \end{array} \]
(FPCore (cosTheta c)
 :precision binary32
 (/
  1.0
  (+
   1.0
   (+
    c
    (*
     (/ (exp (* -1.0 (pow cosTheta 2.0))) cosTheta)
     (sqrt (/ (- 1.0 (* 2.0 cosTheta)) PI)))))))
float code(float cosTheta, float c) {
	return 1.0f / (1.0f + (c + ((expf((-1.0f * powf(cosTheta, 2.0f))) / cosTheta) * sqrtf(((1.0f - (2.0f * cosTheta)) / ((float) M_PI))))));
}
function code(cosTheta, c)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(Float32(exp(Float32(Float32(-1.0) * (cosTheta ^ Float32(2.0)))) / cosTheta) * sqrt(Float32(Float32(Float32(1.0) - Float32(Float32(2.0) * cosTheta)) / Float32(pi)))))))
end
function tmp = code(cosTheta, c)
	tmp = single(1.0) / (single(1.0) + (c + ((exp((single(-1.0) * (cosTheta ^ single(2.0)))) / cosTheta) * sqrt(((single(1.0) - (single(2.0) * cosTheta)) / single(pi))))));
end
\begin{array}{l}

\\
\frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)}
\end{array}
Derivation
  1. Initial program 97.8%

    \[\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. Taylor expanded in c around 0

    \[\leadsto \frac{1}{\color{blue}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)}} \]
  3. Step-by-step derivation
    1. lower-+.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{\left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)}} \]
    2. lower-+.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \color{blue}{\frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}}\right)} \]
    3. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \color{blue}{\sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}}\right)} \]
    4. lower-/.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\color{blue}{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}}\right)} \]
    5. lower-exp.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{\color{blue}{1 - 2 \cdot cosTheta}}{\mathsf{PI}\left(\right)}}\right)} \]
    6. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{\color{blue}{1} - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    7. lower-pow.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    8. lower-sqrt.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    9. lower-/.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    10. lower--.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    11. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    12. lift-PI.f3297.9

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)} \]
  4. Applied rewrites97.9%

    \[\leadsto \frac{1}{\color{blue}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)}} \]
  5. Add Preprocessing

Alternative 2: 97.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \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}} \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))))))
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))));
}
function code(cosTheta, c)
	return Float32(Float32(1.0) / Float32(Float32(Float32(1.0) + c) + Float32(Float32(Float32(Float32(1.0) / sqrt(Float32(pi))) * Float32(sqrt(Float32(Float32(Float32(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp(Float32(Float32(-cosTheta) * cosTheta)))))
end
function tmp = code(cosTheta, c)
	tmp = single(1.0) / ((single(1.0) + c) + (((single(1.0) / sqrt(single(pi))) * (sqrt(((single(1.0) - cosTheta) - cosTheta)) / cosTheta)) * exp((-cosTheta * cosTheta))));
end
\begin{array}{l}

\\
\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}}
\end{array}
Derivation
  1. Initial program 97.8%

    \[\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. Add Preprocessing

Alternative 3: 95.8% accurate, 1.5× speedup?

\[\begin{array}{l} \\ cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right)\right) \end{array} \]
(FPCore (cosTheta c)
 :precision binary32
 (*
  cosTheta
  (+
   (sqrt PI)
   (* -1.0 (* cosTheta (* PI (+ 1.0 (+ c (* -1.0 (sqrt (/ 1.0 PI)))))))))))
float code(float cosTheta, float c) {
	return cosTheta * (sqrtf(((float) M_PI)) + (-1.0f * (cosTheta * (((float) M_PI) * (1.0f + (c + (-1.0f * sqrtf((1.0f / ((float) M_PI))))))))));
}
function code(cosTheta, c)
	return Float32(cosTheta * Float32(sqrt(Float32(pi)) + Float32(Float32(-1.0) * Float32(cosTheta * Float32(Float32(pi) * Float32(Float32(1.0) + Float32(c + Float32(Float32(-1.0) * sqrt(Float32(Float32(1.0) / Float32(pi)))))))))))
end
function tmp = code(cosTheta, c)
	tmp = cosTheta * (sqrt(single(pi)) + (single(-1.0) * (cosTheta * (single(pi) * (single(1.0) + (c + (single(-1.0) * sqrt((single(1.0) / single(pi))))))))));
end
\begin{array}{l}

\\
cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right)\right)
\end{array}
Derivation
  1. Initial program 97.8%

    \[\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. Taylor expanded in cosTheta around 0

    \[\leadsto \color{blue}{cosTheta \cdot \left(\sqrt{\mathsf{PI}\left(\right)} + -1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right)} \]
  3. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto cosTheta \cdot \color{blue}{\left(\sqrt{\mathsf{PI}\left(\right)} + -1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right)} \]
    2. lower-+.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\mathsf{PI}\left(\right)} + \color{blue}{-1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)}\right) \]
    3. lift-sqrt.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\mathsf{PI}\left(\right)} + \color{blue}{-1} \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right) \]
    4. lift-PI.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right) \]
    5. lower-*.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \color{blue}{\left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)}\right) \]
    6. lower-*.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \color{blue}{\left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)}\right)\right) \]
    7. lower-*.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \color{blue}{\left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)}\right)\right)\right) \]
    8. lift-PI.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(\color{blue}{1} + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right) \]
    9. lower-+.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \color{blue}{\left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)}\right)\right)\right)\right) \]
    10. lower-+.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \left(c + \color{blue}{-1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}}\right)\right)\right)\right)\right) \]
  4. Applied rewrites95.8%

    \[\leadsto \color{blue}{cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right)\right)} \]
  5. Add Preprocessing

Alternative 4: 95.7% accurate, 1.6× speedup?

\[\begin{array}{l} \\ cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right) \end{array} \]
(FPCore (cosTheta c)
 :precision binary32
 (*
  cosTheta
  (+
   (sqrt PI)
   (* -1.0 (* cosTheta (* PI (+ 1.0 (* -1.0 (sqrt (/ 1.0 PI))))))))))
float code(float cosTheta, float c) {
	return cosTheta * (sqrtf(((float) M_PI)) + (-1.0f * (cosTheta * (((float) M_PI) * (1.0f + (-1.0f * sqrtf((1.0f / ((float) M_PI)))))))));
}
function code(cosTheta, c)
	return Float32(cosTheta * Float32(sqrt(Float32(pi)) + Float32(Float32(-1.0) * Float32(cosTheta * Float32(Float32(pi) * Float32(Float32(1.0) + Float32(Float32(-1.0) * sqrt(Float32(Float32(1.0) / Float32(pi))))))))))
end
function tmp = code(cosTheta, c)
	tmp = cosTheta * (sqrt(single(pi)) + (single(-1.0) * (cosTheta * (single(pi) * (single(1.0) + (single(-1.0) * sqrt((single(1.0) / single(pi)))))))));
end
\begin{array}{l}

\\
cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right)
\end{array}
Derivation
  1. Initial program 97.8%

    \[\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. Taylor expanded in cosTheta around 0

    \[\leadsto \color{blue}{cosTheta \cdot \left(\sqrt{\mathsf{PI}\left(\right)} + -1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right)} \]
  3. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto cosTheta \cdot \color{blue}{\left(\sqrt{\mathsf{PI}\left(\right)} + -1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right)} \]
    2. lower-+.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\mathsf{PI}\left(\right)} + \color{blue}{-1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)}\right) \]
    3. lift-sqrt.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\mathsf{PI}\left(\right)} + \color{blue}{-1} \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right) \]
    4. lift-PI.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right) \]
    5. lower-*.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \color{blue}{\left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)}\right) \]
    6. lower-*.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \color{blue}{\left(\mathsf{PI}\left(\right) \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)}\right)\right) \]
    7. lower-*.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\mathsf{PI}\left(\right) \cdot \color{blue}{\left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)}\right)\right)\right) \]
    8. lift-PI.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(\color{blue}{1} + \left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right)\right) \]
    9. lower-+.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \color{blue}{\left(c + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)}\right)\right)\right)\right) \]
    10. lower-+.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \left(c + \color{blue}{-1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}}\right)\right)\right)\right)\right) \]
  4. Applied rewrites95.8%

    \[\leadsto \color{blue}{cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + \left(c + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right)\right)} \]
  5. Taylor expanded in c around 0

    \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \color{blue}{\sqrt{\frac{1}{\mathsf{PI}\left(\right)}}}\right)\right)\right)\right) \]
  6. Step-by-step derivation
    1. lift-/.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \sqrt{\frac{1}{\mathsf{PI}\left(\right)}}\right)\right)\right)\right) \]
    2. lift-PI.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right) \]
    3. lift-sqrt.f32N/A

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right) \]
    4. lift-*.f3295.7

      \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \sqrt{\frac{1}{\pi}}\right)\right)\right)\right) \]
  7. Applied rewrites95.7%

    \[\leadsto cosTheta \cdot \left(\sqrt{\pi} + -1 \cdot \left(cosTheta \cdot \left(\pi \cdot \left(1 + -1 \cdot \color{blue}{\sqrt{\frac{1}{\pi}}}\right)\right)\right)\right) \]
  8. Add Preprocessing

Alternative 5: 95.2% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \frac{1}{1 + \left(c + \frac{1}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)} \end{array} \]
(FPCore (cosTheta c)
 :precision binary32
 (/
  1.0
  (+ 1.0 (+ c (* (/ 1.0 cosTheta) (sqrt (/ (- 1.0 (* 2.0 cosTheta)) PI)))))))
float code(float cosTheta, float c) {
	return 1.0f / (1.0f + (c + ((1.0f / cosTheta) * sqrtf(((1.0f - (2.0f * cosTheta)) / ((float) M_PI))))));
}
function code(cosTheta, c)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + Float32(c + Float32(Float32(Float32(1.0) / cosTheta) * sqrt(Float32(Float32(Float32(1.0) - Float32(Float32(2.0) * cosTheta)) / Float32(pi)))))))
end
function tmp = code(cosTheta, c)
	tmp = single(1.0) / (single(1.0) + (c + ((single(1.0) / cosTheta) * sqrt(((single(1.0) - (single(2.0) * cosTheta)) / single(pi))))));
end
\begin{array}{l}

\\
\frac{1}{1 + \left(c + \frac{1}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)}
\end{array}
Derivation
  1. Initial program 97.8%

    \[\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. Taylor expanded in c around 0

    \[\leadsto \frac{1}{\color{blue}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)}} \]
  3. Step-by-step derivation
    1. lower-+.f32N/A

      \[\leadsto \frac{1}{1 + \color{blue}{\left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)}} \]
    2. lower-+.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \color{blue}{\frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}}\right)} \]
    3. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \color{blue}{\sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}}\right)} \]
    4. lower-/.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\color{blue}{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}}\right)} \]
    5. lower-exp.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{\color{blue}{1 - 2 \cdot cosTheta}}{\mathsf{PI}\left(\right)}}\right)} \]
    6. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{\color{blue}{1} - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    7. lower-pow.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    8. lower-sqrt.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    9. lower-/.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    10. lower--.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    11. lower-*.f32N/A

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\mathsf{PI}\left(\right)}}\right)} \]
    12. lift-PI.f3297.9

      \[\leadsto \frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)} \]
  4. Applied rewrites97.9%

    \[\leadsto \frac{1}{\color{blue}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}}}{cosTheta} \cdot \sqrt{\frac{1 - 2 \cdot cosTheta}{\pi}}\right)}} \]
  5. Taylor expanded in cosTheta around 0

    \[\leadsto \frac{1}{1 + \left(c + \frac{1}{cosTheta} \cdot \sqrt{\frac{\color{blue}{1 - 2 \cdot cosTheta}}{\pi}}\right)} \]
  6. Step-by-step derivation
    1. Applied rewrites95.2%

      \[\leadsto \frac{1}{1 + \left(c + \frac{1}{cosTheta} \cdot \sqrt{\frac{\color{blue}{1 - 2 \cdot cosTheta}}{\pi}}\right)} \]
    2. Add Preprocessing

    Alternative 6: 93.0% accurate, 7.8× speedup?

    \[\begin{array}{l} \\ cosTheta \cdot \sqrt{\pi} \end{array} \]
    (FPCore (cosTheta c) :precision binary32 (* cosTheta (sqrt PI)))
    float code(float cosTheta, float c) {
    	return cosTheta * sqrtf(((float) M_PI));
    }
    
    function code(cosTheta, c)
    	return Float32(cosTheta * sqrt(Float32(pi)))
    end
    
    function tmp = code(cosTheta, c)
    	tmp = cosTheta * sqrt(single(pi));
    end
    
    \begin{array}{l}
    
    \\
    cosTheta \cdot \sqrt{\pi}
    \end{array}
    
    Derivation
    1. Initial program 97.8%

      \[\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. Taylor expanded in cosTheta around 0

      \[\leadsto \color{blue}{cosTheta \cdot \sqrt{\mathsf{PI}\left(\right)}} \]
    3. Step-by-step derivation
      1. lower-*.f32N/A

        \[\leadsto cosTheta \cdot \color{blue}{\sqrt{\mathsf{PI}\left(\right)}} \]
      2. lift-sqrt.f32N/A

        \[\leadsto cosTheta \cdot \sqrt{\mathsf{PI}\left(\right)} \]
      3. lift-PI.f3293.0

        \[\leadsto cosTheta \cdot \sqrt{\pi} \]
    4. Applied rewrites93.0%

      \[\leadsto \color{blue}{cosTheta \cdot \sqrt{\pi}} \]
    5. Add Preprocessing

    Alternative 7: 5.0% accurate, 10.0× speedup?

    \[\begin{array}{l} \\ \frac{1}{c} \end{array} \]
    (FPCore (cosTheta c) :precision binary32 (/ 1.0 c))
    float code(float cosTheta, float c) {
    	return 1.0f / c;
    }
    
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(4) function code(costheta, c)
    use fmin_fmax_functions
        real(4), intent (in) :: costheta
        real(4), intent (in) :: c
        code = 1.0e0 / c
    end function
    
    function code(cosTheta, c)
    	return Float32(Float32(1.0) / c)
    end
    
    function tmp = code(cosTheta, c)
    	tmp = single(1.0) / c;
    end
    
    \begin{array}{l}
    
    \\
    \frac{1}{c}
    \end{array}
    
    Derivation
    1. Initial program 97.8%

      \[\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. Taylor expanded in c around inf

      \[\leadsto \color{blue}{\frac{1}{c}} \]
    3. Step-by-step derivation
      1. lower-/.f325.0

        \[\leadsto \frac{1}{\color{blue}{c}} \]
    4. Applied rewrites5.0%

      \[\leadsto \color{blue}{\frac{1}{c}} \]
    5. Add Preprocessing

    Reproduce

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