Trowbridge-Reitz Sample, near normal, slope_x

Percentage Accurate: 99.0% → 99.0%
Time: 6.0s
Alternatives: 4
Speedup: N/A×

Specification

?
\[\left(\left(cosTheta\_i > 0.9999 \land cosTheta\_i \leq 1\right) \land \left(2.328306437 \cdot 10^{-10} \leq u1 \land u1 \leq 1\right)\right) \land \left(2.328306437 \cdot 10^{-10} \leq u2 \land u2 \leq 1\right)\]
\[\begin{array}{l} \\ \sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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_i, u1, u2)
use fmin_fmax_functions
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 / (1.0e0 - u1))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2));
end
\begin{array}{l}

\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\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 4 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: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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_i, u1, u2)
use fmin_fmax_functions
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 / (1.0e0 - u1))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2));
end
\begin{array}{l}

\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}

Alternative 1: 99.0% accurate, N/A× speedup?

\[\begin{array}{l} \\ \sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf((u1 / (1.0f - u1))) * cosf((6.28318530718f * u2));
}
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_i, u1, u2)
use fmin_fmax_functions
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    code = sqrt((u1 / (1.0e0 - u1))) * cos((6.28318530718e0 * u2))
end function
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(u1 / Float32(Float32(1.0) - u1))) * cos(Float32(Float32(6.28318530718) * u2)))
end
function tmp = code(cosTheta_i, u1, u2)
	tmp = sqrt((u1 / (single(1.0) - u1))) * cos((single(6.28318530718) * u2));
end
\begin{array}{l}

\\
\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 98.7% accurate, N/A× speedup?

\[\begin{array}{l} \\ \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \sin \left(\mathsf{fma}\left(6.28318530718, u2, \frac{\pi}{2}\right)\right) \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (*
  (sqrt (/ (* u1 (+ 1.0 (+ u1 (* u1 u1)))) (- 1.0 (pow u1 3.0))))
  (sin (fma 6.28318530718 u2 (/ PI 2.0)))))
float code(float cosTheta_i, float u1, float u2) {
	return sqrtf(((u1 * (1.0f + (u1 + (u1 * u1)))) / (1.0f - powf(u1, 3.0f)))) * sinf(fmaf(6.28318530718f, u2, (((float) M_PI) / 2.0f)));
}
function code(cosTheta_i, u1, u2)
	return Float32(sqrt(Float32(Float32(u1 * Float32(Float32(1.0) + Float32(u1 + Float32(u1 * u1)))) / Float32(Float32(1.0) - (u1 ^ Float32(3.0))))) * sin(fma(Float32(6.28318530718), u2, Float32(Float32(pi) / Float32(2.0)))))
end
\begin{array}{l}

\\
\sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \sin \left(\mathsf{fma}\left(6.28318530718, u2, \frac{\pi}{2}\right)\right)
\end{array}
Derivation
  1. Initial program 99.1%

    \[\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f32N/A

      \[\leadsto \sqrt{\frac{u1}{\color{blue}{1 - u1}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    2. flip3--N/A

      \[\leadsto \sqrt{\frac{u1}{\color{blue}{\frac{{1}^{3} - {u1}^{3}}{1 \cdot 1 + \left(u1 \cdot u1 + 1 \cdot u1\right)}}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    3. lower-/.f32N/A

      \[\leadsto \sqrt{\frac{u1}{\color{blue}{\frac{{1}^{3} - {u1}^{3}}{1 \cdot 1 + \left(u1 \cdot u1 + 1 \cdot u1\right)}}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    4. metadata-evalN/A

      \[\leadsto \sqrt{\frac{u1}{\frac{\color{blue}{1} - {u1}^{3}}{1 \cdot 1 + \left(u1 \cdot u1 + 1 \cdot u1\right)}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    5. lower--.f32N/A

      \[\leadsto \sqrt{\frac{u1}{\frac{\color{blue}{1 - {u1}^{3}}}{1 \cdot 1 + \left(u1 \cdot u1 + 1 \cdot u1\right)}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    6. lower-pow.f32N/A

      \[\leadsto \sqrt{\frac{u1}{\frac{1 - \color{blue}{{u1}^{3}}}{1 \cdot 1 + \left(u1 \cdot u1 + 1 \cdot u1\right)}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    7. metadata-evalN/A

      \[\leadsto \sqrt{\frac{u1}{\frac{1 - {u1}^{3}}{\color{blue}{1} + \left(u1 \cdot u1 + 1 \cdot u1\right)}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    8. lower-+.f32N/A

      \[\leadsto \sqrt{\frac{u1}{\frac{1 - {u1}^{3}}{\color{blue}{1 + \left(u1 \cdot u1 + 1 \cdot u1\right)}}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    9. lower-fma.f32N/A

      \[\leadsto \sqrt{\frac{u1}{\frac{1 - {u1}^{3}}{1 + \color{blue}{\mathsf{fma}\left(u1, u1, 1 \cdot u1\right)}}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    10. lower-*.f3299.0

      \[\leadsto \sqrt{\frac{u1}{\frac{1 - {u1}^{3}}{1 + \mathsf{fma}\left(u1, u1, \color{blue}{1 \cdot u1}\right)}}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  4. Applied rewrites99.0%

    \[\leadsto \sqrt{\frac{u1}{\color{blue}{\frac{1 - {u1}^{3}}{1 + \mathsf{fma}\left(u1, u1, 1 \cdot u1\right)}}}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  5. Taylor expanded in u2 around inf

    \[\leadsto \color{blue}{\sqrt{\frac{u1 \cdot \left(1 + \left(u1 + {u1}^{2}\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right)} \]
  6. Step-by-step derivation
    1. lower-*.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + {u1}^{2}\right)\right)}{1 - {u1}^{3}}} \cdot \color{blue}{\cos \left(\frac{314159265359}{50000000000} \cdot u2\right)} \]
    2. lower-sqrt.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + {u1}^{2}\right)\right)}{1 - {u1}^{3}}} \cdot \cos \color{blue}{\left(\frac{314159265359}{50000000000} \cdot u2\right)} \]
    3. lower-/.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + {u1}^{2}\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\color{blue}{\frac{314159265359}{50000000000}} \cdot u2\right) \]
    4. lower-*.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + {u1}^{2}\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    5. lower-+.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + {u1}^{2}\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    6. lower-+.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + {u1}^{2}\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    7. pow2N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    8. lift-*.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    9. lift-pow.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    10. lift--.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \cos \left(\frac{314159265359}{50000000000} \cdot u2\right) \]
    11. sin-+PI/2-revN/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \sin \left(\frac{314159265359}{50000000000} \cdot u2 + \frac{\mathsf{PI}\left(\right)}{2}\right) \]
    12. lower-sin.f32N/A

      \[\leadsto \sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \sin \left(\frac{314159265359}{50000000000} \cdot u2 + \frac{\mathsf{PI}\left(\right)}{2}\right) \]
  7. Applied rewrites98.9%

    \[\leadsto \color{blue}{\sqrt{\frac{u1 \cdot \left(1 + \left(u1 + u1 \cdot u1\right)\right)}{1 - {u1}^{3}}} \cdot \sin \left(\mathsf{fma}\left(6.28318530718, u2, \frac{\pi}{2}\right)\right)} \]
  8. Add Preprocessing

Alternative 3: 93.3% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := {\left(\frac{u1}{1 - u1}\right)}^{0.5}\\ \mathsf{fma}\left(\mathsf{fma}\left(\left(-85.45681720672748 \cdot t\_0\right) \cdot \left(u2 \cdot u2\right) - -64.93939402268539 \cdot t\_0, u2 \cdot u2, -19.739208802181317 \cdot t\_0\right), u2 \cdot u2, t\_0\right) \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (pow (/ u1 (- 1.0 u1)) 0.5)))
   (fma
    (fma
     (- (* (* -85.45681720672748 t_0) (* u2 u2)) (* -64.93939402268539 t_0))
     (* u2 u2)
     (* -19.739208802181317 t_0))
    (* u2 u2)
    t_0)))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = powf((u1 / (1.0f - u1)), 0.5f);
	return fmaf(fmaf((((-85.45681720672748f * t_0) * (u2 * u2)) - (-64.93939402268539f * t_0)), (u2 * u2), (-19.739208802181317f * t_0)), (u2 * u2), t_0);
}
function code(cosTheta_i, u1, u2)
	t_0 = Float32(u1 / Float32(Float32(1.0) - u1)) ^ Float32(0.5)
	return fma(fma(Float32(Float32(Float32(Float32(-85.45681720672748) * t_0) * Float32(u2 * u2)) - Float32(Float32(-64.93939402268539) * t_0)), Float32(u2 * u2), Float32(Float32(-19.739208802181317) * t_0)), Float32(u2 * u2), t_0)
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := {\left(\frac{u1}{1 - u1}\right)}^{0.5}\\
\mathsf{fma}\left(\mathsf{fma}\left(\left(-85.45681720672748 \cdot t\_0\right) \cdot \left(u2 \cdot u2\right) - -64.93939402268539 \cdot t\_0, u2 \cdot u2, -19.739208802181317 \cdot t\_0\right), u2 \cdot u2, t\_0\right)
\end{array}
\end{array}
Derivation
  1. Initial program 99.1%

    \[\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  2. Add Preprocessing
  3. Taylor expanded in u2 around 0

    \[\leadsto \color{blue}{\sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right)\right)} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto {u2}^{2} \cdot \left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right)\right) + \color{blue}{\sqrt{\frac{u1}{1 - u1}}} \]
    2. *-commutativeN/A

      \[\leadsto \left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right)\right) \cdot {u2}^{2} + \sqrt{\color{blue}{\frac{u1}{1 - u1}}} \]
    3. lower-fma.f32N/A

      \[\leadsto \mathsf{fma}\left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right), \color{blue}{{u2}^{2}}, \sqrt{\frac{u1}{1 - u1}}\right) \]
  5. Applied rewrites94.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\left(-85.45681720672748 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) \cdot \left(u2 \cdot u2\right) - -64.93939402268539 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}, u2 \cdot u2, -19.739208802181317 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right), u2 \cdot u2, {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right)} \]
  6. Add Preprocessing

Alternative 4: 92.8% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := {\left(\frac{u1}{1 - u1}\right)}^{0.5}\\ t_1 := \left(u2 \cdot u2\right) \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(\left(-85.45681720672748 \cdot t\_0\right) \cdot u2\right) \cdot u2 - -64.93939402268539 \cdot t\_0\right) - 19.739208802181317 \cdot t\_0\right)\\ \frac{{t\_0}^{3} + {t\_1}^{3}}{t\_0 \cdot t\_0 + \left(t\_1 \cdot t\_1 - t\_0 \cdot t\_1\right)} \end{array} \end{array} \]
(FPCore (cosTheta_i u1 u2)
 :precision binary32
 (let* ((t_0 (pow (/ u1 (- 1.0 u1)) 0.5))
        (t_1
         (*
          (* u2 u2)
          (-
           (*
            (* u2 u2)
            (-
             (* (* (* -85.45681720672748 t_0) u2) u2)
             (* -64.93939402268539 t_0)))
           (* 19.739208802181317 t_0)))))
   (/
    (+ (pow t_0 3.0) (pow t_1 3.0))
    (+ (* t_0 t_0) (- (* t_1 t_1) (* t_0 t_1))))))
float code(float cosTheta_i, float u1, float u2) {
	float t_0 = powf((u1 / (1.0f - u1)), 0.5f);
	float t_1 = (u2 * u2) * (((u2 * u2) * ((((-85.45681720672748f * t_0) * u2) * u2) - (-64.93939402268539f * t_0))) - (19.739208802181317f * t_0));
	return (powf(t_0, 3.0f) + powf(t_1, 3.0f)) / ((t_0 * t_0) + ((t_1 * t_1) - (t_0 * t_1)));
}
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_i, u1, u2)
use fmin_fmax_functions
    real(4), intent (in) :: costheta_i
    real(4), intent (in) :: u1
    real(4), intent (in) :: u2
    real(4) :: t_0
    real(4) :: t_1
    t_0 = (u1 / (1.0e0 - u1)) ** 0.5e0
    t_1 = (u2 * u2) * (((u2 * u2) * (((((-85.45681720672748e0) * t_0) * u2) * u2) - ((-64.93939402268539e0) * t_0))) - (19.739208802181317e0 * t_0))
    code = ((t_0 ** 3.0e0) + (t_1 ** 3.0e0)) / ((t_0 * t_0) + ((t_1 * t_1) - (t_0 * t_1)))
end function
function code(cosTheta_i, u1, u2)
	t_0 = Float32(u1 / Float32(Float32(1.0) - u1)) ^ Float32(0.5)
	t_1 = Float32(Float32(u2 * u2) * Float32(Float32(Float32(u2 * u2) * Float32(Float32(Float32(Float32(Float32(-85.45681720672748) * t_0) * u2) * u2) - Float32(Float32(-64.93939402268539) * t_0))) - Float32(Float32(19.739208802181317) * t_0)))
	return Float32(Float32((t_0 ^ Float32(3.0)) + (t_1 ^ Float32(3.0))) / Float32(Float32(t_0 * t_0) + Float32(Float32(t_1 * t_1) - Float32(t_0 * t_1))))
end
function tmp = code(cosTheta_i, u1, u2)
	t_0 = (u1 / (single(1.0) - u1)) ^ single(0.5);
	t_1 = (u2 * u2) * (((u2 * u2) * ((((single(-85.45681720672748) * t_0) * u2) * u2) - (single(-64.93939402268539) * t_0))) - (single(19.739208802181317) * t_0));
	tmp = ((t_0 ^ single(3.0)) + (t_1 ^ single(3.0))) / ((t_0 * t_0) + ((t_1 * t_1) - (t_0 * t_1)));
end
\begin{array}{l}

\\
\begin{array}{l}
t_0 := {\left(\frac{u1}{1 - u1}\right)}^{0.5}\\
t_1 := \left(u2 \cdot u2\right) \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(\left(-85.45681720672748 \cdot t\_0\right) \cdot u2\right) \cdot u2 - -64.93939402268539 \cdot t\_0\right) - 19.739208802181317 \cdot t\_0\right)\\
\frac{{t\_0}^{3} + {t\_1}^{3}}{t\_0 \cdot t\_0 + \left(t\_1 \cdot t\_1 - t\_0 \cdot t\_1\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 99.1%

    \[\sqrt{\frac{u1}{1 - u1}} \cdot \cos \left(6.28318530718 \cdot u2\right) \]
  2. Add Preprocessing
  3. Taylor expanded in u2 around 0

    \[\leadsto \color{blue}{\sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right)\right)} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto {u2}^{2} \cdot \left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right)\right) + \color{blue}{\sqrt{\frac{u1}{1 - u1}}} \]
    2. *-commutativeN/A

      \[\leadsto \left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right)\right) \cdot {u2}^{2} + \sqrt{\color{blue}{\frac{u1}{1 - u1}}} \]
    3. lower-fma.f32N/A

      \[\leadsto \mathsf{fma}\left(\frac{-98696044010906577398881}{5000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}} + {u2}^{2} \cdot \left(\frac{-961389193575684075633145058384385882649239799132134631991269883031841}{11250000000000000000000000000000000000000000000000000000000000000000} \cdot \left(\sqrt{\frac{u1}{1 - u1}} \cdot {u2}^{2}\right) + \frac{9740909103402808085817682884085781839780052161}{150000000000000000000000000000000000000000000} \cdot \sqrt{\frac{u1}{1 - u1}}\right), \color{blue}{{u2}^{2}}, \sqrt{\frac{u1}{1 - u1}}\right) \]
  5. Applied rewrites94.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\left(-85.45681720672748 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) \cdot \left(u2 \cdot u2\right) - -64.93939402268539 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}, u2 \cdot u2, -19.739208802181317 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right), u2 \cdot u2, {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right)} \]
  6. Applied rewrites93.6%

    \[\leadsto \frac{{\left({\left(\frac{u1}{1 - u1}\right)}^{0.5}\right)}^{3} + {\left(\left(u2 \cdot u2\right) \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(\left(-85.45681720672748 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) \cdot u2\right) \cdot u2 - -64.93939402268539 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) - 19.739208802181317 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right)\right)}^{3}}{\color{blue}{{\left(\frac{u1}{1 - u1}\right)}^{0.5} \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5} + \left(\left(\left(u2 \cdot u2\right) \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(\left(-85.45681720672748 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) \cdot u2\right) \cdot u2 - -64.93939402268539 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) - 19.739208802181317 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right)\right) \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(\left(-85.45681720672748 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) \cdot u2\right) \cdot u2 - -64.93939402268539 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) - 19.739208802181317 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right)\right) - {\left(\frac{u1}{1 - u1}\right)}^{0.5} \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(u2 \cdot u2\right) \cdot \left(\left(\left(-85.45681720672748 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) \cdot u2\right) \cdot u2 - -64.93939402268539 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right) - 19.739208802181317 \cdot {\left(\frac{u1}{1 - u1}\right)}^{0.5}\right)\right)\right)}} \]
  7. Add Preprocessing

Reproduce

?
herbie shell --seed 2025066 
(FPCore (cosTheta_i u1 u2)
  :name "Trowbridge-Reitz Sample, near normal, slope_x"
  :precision binary32
  :pre (and (and (and (> cosTheta_i 0.9999) (<= cosTheta_i 1.0)) (and (<= 2.328306437e-10 u1) (<= u1 1.0))) (and (<= 2.328306437e-10 u2) (<= u2 1.0)))
  (* (sqrt (/ u1 (- 1.0 u1))) (cos (* 6.28318530718 u2))))