Beckmann Sample, normalization factor

Percentage Accurate: 97.8% → 98.5%
Time: 4.7s
Alternatives: 11
Speedup: 1.1×

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 11 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: 98.5% accurate, 0.8× speedup?

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

\\
\frac{1}{1 + \left(c + \frac{e^{-1 \cdot {cosTheta}^{2}} \cdot \sqrt{1 - 2 \cdot cosTheta}}{cosTheta \cdot \sqrt{\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}} \cdot \sqrt{1 - 2 \cdot cosTheta}}{cosTheta \cdot \sqrt{\mathsf{PI}\left(\right)}}\right)}} \]
  3. Applied rewrites98.5%

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

Alternative 2: 98.5% accurate, 1.1× speedup?

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

\\
\frac{1}{\left(1 + c\right) + \sqrt{\left(1 - cosTheta\right) - cosTheta} \cdot \frac{e^{-cosTheta \cdot cosTheta}}{\sqrt{\pi} \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. Applied rewrites98.5%

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

Alternative 3: 97.9% accurate, 1.1× speedup?

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

\\
\frac{1}{\left(1 + c\right) + \sqrt{\frac{1 + -2 \cdot cosTheta}{\pi}} \cdot \frac{e^{-cosTheta \cdot cosTheta}}{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. Applied rewrites97.9%

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

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

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

Alternative 4: 97.9% accurate, 1.1× speedup?

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

\\
\frac{1}{\left(1 + c\right) + \sqrt{\frac{\left(1 - cosTheta\right) - cosTheta}{\pi}} \cdot \frac{e^{-cosTheta \cdot cosTheta}}{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. Applied rewrites97.9%

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

Alternative 5: 97.5% accurate, 1.3× speedup?

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

\\
\frac{1}{1 + \left(c + \frac{1 + cosTheta \cdot \left(cosTheta \cdot \left(0.5 \cdot cosTheta - 1.5\right) - 1\right)}{cosTheta \cdot \sqrt{\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}} \cdot \sqrt{1 - 2 \cdot cosTheta}}{cosTheta \cdot \sqrt{\mathsf{PI}\left(\right)}}\right)}} \]
  3. Applied rewrites98.5%

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

    \[\leadsto \frac{1}{1 + \left(c + \frac{1 + cosTheta \cdot \left(cosTheta \cdot \left(\frac{1}{2} \cdot cosTheta - \frac{3}{2}\right) - 1\right)}{\color{blue}{cosTheta} \cdot \sqrt{\pi}}\right)} \]
  5. Applied rewrites97.5%

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

Alternative 6: 96.9% accurate, 1.6× speedup?

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

\\
\frac{1}{1 + \left(c + \frac{1 + cosTheta \cdot \left(-1.5 \cdot cosTheta - 1\right)}{cosTheta \cdot \sqrt{\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}} \cdot \sqrt{1 - 2 \cdot cosTheta}}{cosTheta \cdot \sqrt{\mathsf{PI}\left(\right)}}\right)}} \]
  3. Applied rewrites98.5%

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

    \[\leadsto \frac{1}{1 + \left(c + \frac{1 + cosTheta \cdot \left(\frac{-3}{2} \cdot cosTheta - 1\right)}{\color{blue}{cosTheta} \cdot \sqrt{\pi}}\right)} \]
  5. Applied rewrites96.9%

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

Alternative 7: 95.4% accurate, 1.8× speedup?

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

\\
\frac{1}{\left(1 + \left(c + \frac{1}{cosTheta \cdot \sqrt{\pi}}\right)\right) - \frac{1}{\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 \frac{1}{\color{blue}{\frac{cosTheta \cdot \left(\left(1 + c\right) - \frac{1}{\sqrt{\mathsf{PI}\left(\right)}}\right) + \frac{1}{\sqrt{\mathsf{PI}\left(\right)}}}{cosTheta}}} \]
  3. Applied rewrites94.8%

    \[\leadsto \frac{1}{\color{blue}{\frac{\mathsf{fma}\left(cosTheta, \left(1 + c\right) - \frac{1}{\sqrt{\pi}}, \frac{1}{\sqrt{\pi}}\right)}{cosTheta}}} \]
  4. Taylor expanded in cosTheta around inf

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

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

Alternative 8: 95.4% accurate, 1.9× speedup?

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

\\
\frac{1}{1 + \left(c + \frac{1 + -1 \cdot cosTheta}{cosTheta \cdot \sqrt{\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}} \cdot \sqrt{1 - 2 \cdot cosTheta}}{cosTheta \cdot \sqrt{\mathsf{PI}\left(\right)}}\right)}} \]
  3. Applied rewrites98.5%

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

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

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

Alternative 9: 92.7% 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. Applied rewrites92.7%

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

Alternative 10: 29.1% accurate, 46.5× speedup?

\[\begin{array}{l} \\ cosTheta \end{array} \]
(FPCore (cosTheta c) :precision binary32 cosTheta)
float code(float cosTheta, float c) {
	return cosTheta;
}
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 = costheta
end function
function code(cosTheta, c)
	return cosTheta
end
function tmp = code(cosTheta, c)
	tmp = cosTheta;
end
\begin{array}{l}

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

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

    \[\leadsto \color{blue}{\frac{1}{c}} \]
  4. Applied rewrites10.8%

    \[\leadsto \color{blue}{1} \]
  5. Applied rewrites29.1%

    \[\leadsto cosTheta \]
  6. Add Preprocessing

Alternative 11: 10.8% accurate, 46.5× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (cosTheta c) :precision binary32 1.0)
float code(float cosTheta, float c) {
	return 1.0f;
}
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
end function
function code(cosTheta, c)
	return Float32(1.0)
end
function tmp = code(cosTheta, c)
	tmp = single(1.0);
end
\begin{array}{l}

\\
1
\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. Applied rewrites5.0%

    \[\leadsto \color{blue}{\frac{1}{c}} \]
  4. Applied rewrites10.8%

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

Reproduce

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