Beckmann Distribution sample, tan2theta, alphax != alphay, u1 <= 0.5

Percentage Accurate: 60.5% → 98.4%
Time: 19.3s
Alternatives: 10
Speedup: 8.9×

Specification

?
\[\left(\left(\left(\left(0.0001 \leq alphax \land alphax \leq 1\right) \land \left(0.0001 \leq alphay \land alphay \leq 1\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq u0 \land u0 \leq 1\right)\right) \land \left(0 \leq cos2phi \land cos2phi \leq 1\right)\right) \land 0 \leq sin2phi\]
\[\begin{array}{l} \\ \frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log (- 1.0 u0)))
  (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -logf((1.0f - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = -log((1.0e0 - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log(Float32(Float32(1.0) - u0))) / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(sin2phi / Float32(alphay * alphay))))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = -log((single(1.0) - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
end
\begin{array}{l}

\\
\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\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 10 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: 60.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log (- 1.0 u0)))
  (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -logf((1.0f - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = -log((1.0e0 - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log(Float32(Float32(1.0) - u0))) / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(sin2phi / Float32(alphay * alphay))))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = -log((single(1.0) - u0)) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
end
\begin{array}{l}

\\
\frac{-\log \left(1 - u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\end{array}

Alternative 1: 98.4% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \frac{-\mathsf{log1p}\left(-u0\right)}{{alphax}^{-2} \cdot cos2phi + \frac{\frac{sin2phi}{alphay}}{alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log1p (- u0)))
  (+ (* (pow alphax -2.0) cos2phi) (/ (/ sin2phi alphay) alphay))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -log1pf(-u0) / ((powf(alphax, -2.0f) * cos2phi) + ((sin2phi / alphay) / alphay));
}
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log1p(Float32(-u0))) / Float32(Float32((alphax ^ Float32(-2.0)) * cos2phi) + Float32(Float32(sin2phi / alphay) / alphay)))
end
\begin{array}{l}

\\
\frac{-\mathsf{log1p}\left(-u0\right)}{{alphax}^{-2} \cdot cos2phi + \frac{\frac{sin2phi}{alphay}}{alphay}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 98.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log1p (- u0)))
  (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -log1pf(-u0) / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log1p(Float32(-u0))) / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(sin2phi / Float32(alphay * alphay))))
end
\begin{array}{l}

\\
\frac{-\mathsf{log1p}\left(-u0\right)}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 98.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{sin2phi}{alphay}}{alphay} + \frac{cos2phi}{alphax \cdot alphax}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log1p (- u0)))
  (+ (/ (/ sin2phi alphay) alphay) (/ cos2phi (* alphax alphax)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -log1pf(-u0) / (((sin2phi / alphay) / alphay) + (cos2phi / (alphax * alphax)));
}
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log1p(Float32(-u0))) / Float32(Float32(Float32(sin2phi / alphay) / alphay) + Float32(cos2phi / Float32(alphax * alphax))))
end
\begin{array}{l}

\\
\frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{sin2phi}{alphay}}{alphay} + \frac{cos2phi}{alphax \cdot alphax}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 98.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{sin2phi}{alphay}}{alphay} + \frac{\frac{cos2phi}{alphax}}{alphax}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (- (log1p (- u0)))
  (+ (/ (/ sin2phi alphay) alphay) (/ (/ cos2phi alphax) alphax))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return -log1pf(-u0) / (((sin2phi / alphay) / alphay) + ((cos2phi / alphax) / alphax));
}
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(-log1p(Float32(-u0))) / Float32(Float32(Float32(sin2phi / alphay) / alphay) + Float32(Float32(cos2phi / alphax) / alphax)))
end
\begin{array}{l}

\\
\frac{-\mathsf{log1p}\left(-u0\right)}{\frac{\frac{sin2phi}{alphay}}{alphay} + \frac{\frac{cos2phi}{alphax}}{alphax}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 76.0% accurate, 6.8× speedup?

\[\begin{array}{l} \\ \frac{alphax \cdot \left(u0 \cdot alphay\right)}{\frac{alphax \cdot sin2phi}{alphay} + \frac{cos2phi \cdot alphay}{alphax}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/
  (* alphax (* u0 alphay))
  (+ (/ (* alphax sin2phi) alphay) (/ (* cos2phi alphay) alphax))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return (alphax * (u0 * alphay)) / (((alphax * sin2phi) / alphay) + ((cos2phi * alphay) / alphax));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = (alphax * (u0 * alphay)) / (((alphax * sin2phi) / alphay) + ((cos2phi * alphay) / alphax))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(alphax * Float32(u0 * alphay)) / Float32(Float32(Float32(alphax * sin2phi) / alphay) + Float32(Float32(cos2phi * alphay) / alphax)))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = (alphax * (u0 * alphay)) / (((alphax * sin2phi) / alphay) + ((cos2phi * alphay) / alphax));
end
\begin{array}{l}

\\
\frac{alphax \cdot \left(u0 \cdot alphay\right)}{\frac{alphax \cdot sin2phi}{alphay} + \frac{cos2phi \cdot alphay}{alphax}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 66.5% accurate, 7.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := alphax \cdot \left(u0 \cdot alphay\right)\\ \mathbf{if}\;sin2phi \leq 3.0000000340435383 \cdot 10^{-18}:\\ \;\;\;\;\frac{t_0}{\frac{cos2phi \cdot alphay}{alphax}}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_0}{\frac{alphax \cdot sin2phi}{alphay}}\\ \end{array} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (let* ((t_0 (* alphax (* u0 alphay))))
   (if (<= sin2phi 3.0000000340435383e-18)
     (/ t_0 (/ (* cos2phi alphay) alphax))
     (/ t_0 (/ (* alphax sin2phi) alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	float t_0 = alphax * (u0 * alphay);
	float tmp;
	if (sin2phi <= 3.0000000340435383e-18f) {
		tmp = t_0 / ((cos2phi * alphay) / alphax);
	} else {
		tmp = t_0 / ((alphax * sin2phi) / alphay);
	}
	return tmp;
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    real(4) :: t_0
    real(4) :: tmp
    t_0 = alphax * (u0 * alphay)
    if (sin2phi <= 3.0000000340435383e-18) then
        tmp = t_0 / ((cos2phi * alphay) / alphax)
    else
        tmp = t_0 / ((alphax * sin2phi) / alphay)
    end if
    code = tmp
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	t_0 = Float32(alphax * Float32(u0 * alphay))
	tmp = Float32(0.0)
	if (sin2phi <= Float32(3.0000000340435383e-18))
		tmp = Float32(t_0 / Float32(Float32(cos2phi * alphay) / alphax));
	else
		tmp = Float32(t_0 / Float32(Float32(alphax * sin2phi) / alphay));
	end
	return tmp
end
function tmp_2 = code(alphax, alphay, u0, cos2phi, sin2phi)
	t_0 = alphax * (u0 * alphay);
	tmp = single(0.0);
	if (sin2phi <= single(3.0000000340435383e-18))
		tmp = t_0 / ((cos2phi * alphay) / alphax);
	else
		tmp = t_0 / ((alphax * sin2phi) / alphay);
	end
	tmp_2 = tmp;
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := alphax \cdot \left(u0 \cdot alphay\right)\\
\mathbf{if}\;sin2phi \leq 3.0000000340435383 \cdot 10^{-18}:\\
\;\;\;\;\frac{t_0}{\frac{cos2phi \cdot alphay}{alphax}}\\

\mathbf{else}:\\
\;\;\;\;\frac{t_0}{\frac{alphax \cdot sin2phi}{alphay}}\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 75.8% accurate, 8.3× speedup?

\[\begin{array}{l} \\ \frac{u0}{\frac{cos2phi}{alphax \cdot alphax} - \frac{\frac{sin2phi}{alphay}}{-alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/ u0 (- (/ cos2phi (* alphax alphax)) (/ (/ sin2phi alphay) (- alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return u0 / ((cos2phi / (alphax * alphax)) - ((sin2phi / alphay) / -alphay));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = u0 / ((cos2phi / (alphax * alphax)) - ((sin2phi / alphay) / -alphay))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(u0 / Float32(Float32(cos2phi / Float32(alphax * alphax)) - Float32(Float32(sin2phi / alphay) / Float32(-alphay))))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = u0 / ((cos2phi / (alphax * alphax)) - ((sin2phi / alphay) / -alphay));
end
\begin{array}{l}

\\
\frac{u0}{\frac{cos2phi}{alphax \cdot alphax} - \frac{\frac{sin2phi}{alphay}}{-alphay}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 75.8% accurate, 8.9× speedup?

\[\begin{array}{l} \\ \frac{u0}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/ u0 (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return u0 / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = u0 / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(u0 / Float32(Float32(cos2phi / Float32(alphax * alphax)) + Float32(sin2phi / Float32(alphay * alphay))))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = u0 / ((cos2phi / (alphax * alphax)) + (sin2phi / (alphay * alphay)));
end
\begin{array}{l}

\\
\frac{u0}{\frac{cos2phi}{alphax \cdot alphax} + \frac{sin2phi}{alphay \cdot alphay}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 9: 59.0% accurate, 10.5× speedup?

\[\begin{array}{l} \\ \frac{alphax \cdot \left(u0 \cdot alphay\right)}{alphax \cdot \frac{sin2phi}{alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/ (* alphax (* u0 alphay)) (* alphax (/ sin2phi alphay))))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return (alphax * (u0 * alphay)) / (alphax * (sin2phi / alphay));
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = (alphax * (u0 * alphay)) / (alphax * (sin2phi / alphay))
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(alphax * Float32(u0 * alphay)) / Float32(alphax * Float32(sin2phi / alphay)))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = (alphax * (u0 * alphay)) / (alphax * (sin2phi / alphay));
end
\begin{array}{l}

\\
\frac{alphax \cdot \left(u0 \cdot alphay\right)}{alphax \cdot \frac{sin2phi}{alphay}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 10: 59.0% accurate, 10.5× speedup?

\[\begin{array}{l} \\ \frac{alphax \cdot \left(u0 \cdot alphay\right)}{\frac{alphax \cdot sin2phi}{alphay}} \end{array} \]
(FPCore (alphax alphay u0 cos2phi sin2phi)
 :precision binary32
 (/ (* alphax (* u0 alphay)) (/ (* alphax sin2phi) alphay)))
float code(float alphax, float alphay, float u0, float cos2phi, float sin2phi) {
	return (alphax * (u0 * alphay)) / ((alphax * sin2phi) / alphay);
}
real(4) function code(alphax, alphay, u0, cos2phi, sin2phi)
    real(4), intent (in) :: alphax
    real(4), intent (in) :: alphay
    real(4), intent (in) :: u0
    real(4), intent (in) :: cos2phi
    real(4), intent (in) :: sin2phi
    code = (alphax * (u0 * alphay)) / ((alphax * sin2phi) / alphay)
end function
function code(alphax, alphay, u0, cos2phi, sin2phi)
	return Float32(Float32(alphax * Float32(u0 * alphay)) / Float32(Float32(alphax * sin2phi) / alphay))
end
function tmp = code(alphax, alphay, u0, cos2phi, sin2phi)
	tmp = (alphax * (u0 * alphay)) / ((alphax * sin2phi) / alphay);
end
\begin{array}{l}

\\
\frac{alphax \cdot \left(u0 \cdot alphay\right)}{\frac{alphax \cdot sin2phi}{alphay}}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

?
herbie shell --seed 2024006 
(FPCore (alphax alphay u0 cos2phi sin2phi)
  :name "Beckmann Distribution sample, tan2theta, alphax != alphay, u1 <= 0.5"
  :precision binary32
  :pre (and (and (and (and (and (<= 0.0001 alphax) (<= alphax 1.0)) (and (<= 0.0001 alphay) (<= alphay 1.0))) (and (<= 2.328306437e-10 u0) (<= u0 1.0))) (and (<= 0.0 cos2phi) (<= cos2phi 1.0))) (<= 0.0 sin2phi))
  (/ (- (log (- 1.0 u0))) (+ (/ cos2phi (* alphax alphax)) (/ sin2phi (* alphay alphay)))))