Beckmann Sample, normalization factor

Percentage Accurate: 97.8% → 98.3%
Time: 13.7s
Alternatives: 8
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}

Sampling outcomes in binary32 precision:

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 8 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.3% accurate, 1.0× speedup?

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

\\
\frac{1}{\left(1 + c\right) + \frac{\frac{1}{cosTheta}}{\sqrt{\frac{\pi}{\mathsf{fma}\left(cosTheta, -2, 1\right)}}} \cdot e^{cosTheta \cdot \left(-cosTheta\right)}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 98.5% accurate, 1.0× speedup?

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

\\
\frac{1}{\left(1 + c\right) + \frac{\frac{\sqrt{1 + cosTheta \cdot -2}}{cosTheta \cdot \sqrt{\pi}}}{e^{cosTheta \cdot cosTheta}}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 98.0% accurate, 1.0× speedup?

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

\\
\frac{1}{\left(1 + c\right) + e^{cosTheta \cdot \left(-cosTheta\right)} \cdot \frac{\sqrt{\frac{\mathsf{fma}\left(cosTheta, -2, 1\right)}{\pi}}}{cosTheta}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 96.7% accurate, 1.3× speedup?

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

\\
\frac{1}{\left(1 + c\right) + \frac{1}{\sqrt{\pi}} \cdot \left(\left(cosTheta \cdot -1.5 + \left(\frac{1}{cosTheta} + 0.5 \cdot {cosTheta}^{2}\right)\right) + -1\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 96.0% accurate, 1.9× speedup?

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

\\
\frac{1}{\left(1 + c\right) + \frac{1}{\sqrt{\pi}} \cdot \left(\left(\frac{1}{cosTheta} + cosTheta \cdot -1.5\right) + -1\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 94.6% accurate, 2.0× speedup?

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

\\
\frac{1}{\left(1 + c\right) + \frac{1}{\sqrt{\pi}} \cdot \left(\frac{1}{cosTheta} + -1\right)}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 92.6% accurate, 2.1× 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
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 10.9% accurate, 421.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (cosTheta c) :precision binary32 1.0)
float code(float cosTheta, float c) {
	return 1.0f;
}
real(4) function code(costheta, c)
    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
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

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