ab-angle->ABCF A

Percentage Accurate: 80.4% → 80.4%
Time: 4.3s
Alternatives: 4
Speedup: N/A×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{angle}{180} \cdot \mathsf{PI}\left(\right)\\ {\left(a \cdot \sin t\_0\right)}^{2} + {\left(b \cdot \cos t\_0\right)}^{2} \end{array} \end{array} \]
(FPCore (a b angle)
 :precision binary64
 (let* ((t_0 (* (/ angle 180.0) (PI))))
   (+ (pow (* a (sin t_0)) 2.0) (pow (* b (cos t_0)) 2.0))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{angle}{180} \cdot \mathsf{PI}\left(\right)\\
{\left(a \cdot \sin t\_0\right)}^{2} + {\left(b \cdot \cos t\_0\right)}^{2}
\end{array}
\end{array}

Sampling outcomes in binary64 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: 80.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{angle}{180} \cdot \mathsf{PI}\left(\right)\\ {\left(a \cdot \sin t\_0\right)}^{2} + {\left(b \cdot \cos t\_0\right)}^{2} \end{array} \end{array} \]
(FPCore (a b angle)
 :precision binary64
 (let* ((t_0 (* (/ angle 180.0) (PI))))
   (+ (pow (* a (sin t_0)) 2.0) (pow (* b (cos t_0)) 2.0))))
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{angle}{180} \cdot \mathsf{PI}\left(\right)\\
{\left(a \cdot \sin t\_0\right)}^{2} + {\left(b \cdot \cos t\_0\right)}^{2}
\end{array}
\end{array}

Alternative 1: 80.4% accurate, 2.0× speedup?

\[\begin{array}{l} \\ {\left(a \cdot \sin \left(\left(0.005555555555555556 \cdot angle\right) \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + b \cdot b \end{array} \]
(FPCore (a b angle)
 :precision binary64
 (+ (pow (* a (sin (* (* 0.005555555555555556 angle) (PI)))) 2.0) (* b b)))
\begin{array}{l}

\\
{\left(a \cdot \sin \left(\left(0.005555555555555556 \cdot angle\right) \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + b \cdot b
\end{array}
Derivation
  1. Initial program 78.8%

    \[{\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + {\left(b \cdot \cos \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} \]
  2. Add Preprocessing
  3. Taylor expanded in angle around 0

    \[\leadsto {\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + \color{blue}{{b}^{2}} \]
  4. Step-by-step derivation
    1. Applied rewrites79.1%

      \[\leadsto {\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + \color{blue}{b \cdot b} \]
    2. Taylor expanded in angle around 0

      \[\leadsto {\left(a \cdot \sin \left(\color{blue}{\left(\frac{1}{180} \cdot angle\right)} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + b \cdot b \]
    3. Step-by-step derivation
      1. Applied rewrites79.1%

        \[\leadsto {\left(a \cdot \sin \left(\color{blue}{\left(0.005555555555555556 \cdot angle\right)} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + b \cdot b \]
      2. Add Preprocessing

      Alternative 2: 67.7% accurate, 9.3× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a \leq 4 \cdot 10^{-62}:\\ \;\;\;\;b \cdot b\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\left(\left(\mathsf{PI}\left(\right) \cdot a\right) \cdot 0.005555555555555556\right) \cdot angle, \left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right), b \cdot b\right)\\ \end{array} \end{array} \]
      (FPCore (a b angle)
       :precision binary64
       (if (<= a 4e-62)
         (* b b)
         (fma
          (* (* (* (PI) a) 0.005555555555555556) angle)
          (* (* 0.005555555555555556 a) (* (PI) angle))
          (* b b))))
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;a \leq 4 \cdot 10^{-62}:\\
      \;\;\;\;b \cdot b\\
      
      \mathbf{else}:\\
      \;\;\;\;\mathsf{fma}\left(\left(\left(\mathsf{PI}\left(\right) \cdot a\right) \cdot 0.005555555555555556\right) \cdot angle, \left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right), b \cdot b\right)\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if a < 4.0000000000000002e-62

        1. Initial program 78.9%

          \[{\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + {\left(b \cdot \cos \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} \]
        2. Add Preprocessing
        3. Taylor expanded in angle around 0

          \[\leadsto \color{blue}{{b}^{2}} \]
        4. Step-by-step derivation
          1. Applied rewrites60.7%

            \[\leadsto \color{blue}{b \cdot b} \]

          if 4.0000000000000002e-62 < a

          1. Initial program 78.6%

            \[{\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + {\left(b \cdot \cos \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} \]
          2. Add Preprocessing
          3. Taylor expanded in angle around 0

            \[\leadsto {\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + \color{blue}{{b}^{2}} \]
          4. Step-by-step derivation
            1. Applied rewrites78.4%

              \[\leadsto {\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + \color{blue}{b \cdot b} \]
            2. Taylor expanded in angle around 0

              \[\leadsto {\color{blue}{\left(\frac{1}{180} \cdot \left(a \cdot \left(angle \cdot \mathsf{PI}\left(\right)\right)\right)\right)}}^{2} + b \cdot b \]
            3. Step-by-step derivation
              1. Applied rewrites72.9%

                \[\leadsto {\color{blue}{\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot 0.005555555555555556\right)}}^{2} + b \cdot b \]
              2. Step-by-step derivation
                1. lift-+.f64N/A

                  \[\leadsto \color{blue}{{\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot \frac{1}{180}\right)}^{2} + b \cdot b} \]
                2. lift-pow.f64N/A

                  \[\leadsto \color{blue}{{\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot \frac{1}{180}\right)}^{2}} + b \cdot b \]
                3. unpow2N/A

                  \[\leadsto \color{blue}{\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot \frac{1}{180}\right) \cdot \left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot \frac{1}{180}\right)} + b \cdot b \]
                4. lower-fma.f6472.9

                  \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot 0.005555555555555556, \left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot 0.005555555555555556, b \cdot b\right)} \]
              3. Applied rewrites72.9%

                \[\leadsto \color{blue}{\mathsf{fma}\left(\left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right), \left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right), b \cdot b\right)} \]
              4. Step-by-step derivation
                1. Applied rewrites72.9%

                  \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\left(\mathsf{PI}\left(\right) \cdot a\right) \cdot 0.005555555555555556\right) \cdot angle}, \left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right), b \cdot b\right) \]
              5. Recombined 2 regimes into one program.
              6. Add Preprocessing

              Alternative 3: 67.7% accurate, 9.3× speedup?

              \[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right)\\ \mathbf{if}\;a \leq 4 \cdot 10^{-62}:\\ \;\;\;\;b \cdot b\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t\_0, t\_0, b \cdot b\right)\\ \end{array} \end{array} \]
              (FPCore (a b angle)
               :precision binary64
               (let* ((t_0 (* (* 0.005555555555555556 a) (* (PI) angle))))
                 (if (<= a 4e-62) (* b b) (fma t_0 t_0 (* b b)))))
              \begin{array}{l}
              
              \\
              \begin{array}{l}
              t_0 := \left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right)\\
              \mathbf{if}\;a \leq 4 \cdot 10^{-62}:\\
              \;\;\;\;b \cdot b\\
              
              \mathbf{else}:\\
              \;\;\;\;\mathsf{fma}\left(t\_0, t\_0, b \cdot b\right)\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 2 regimes
              2. if a < 4.0000000000000002e-62

                1. Initial program 78.9%

                  \[{\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + {\left(b \cdot \cos \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} \]
                2. Add Preprocessing
                3. Taylor expanded in angle around 0

                  \[\leadsto \color{blue}{{b}^{2}} \]
                4. Step-by-step derivation
                  1. Applied rewrites60.7%

                    \[\leadsto \color{blue}{b \cdot b} \]

                  if 4.0000000000000002e-62 < a

                  1. Initial program 78.6%

                    \[{\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + {\left(b \cdot \cos \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} \]
                  2. Add Preprocessing
                  3. Taylor expanded in angle around 0

                    \[\leadsto {\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + \color{blue}{{b}^{2}} \]
                  4. Step-by-step derivation
                    1. Applied rewrites78.4%

                      \[\leadsto {\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + \color{blue}{b \cdot b} \]
                    2. Taylor expanded in angle around 0

                      \[\leadsto {\color{blue}{\left(\frac{1}{180} \cdot \left(a \cdot \left(angle \cdot \mathsf{PI}\left(\right)\right)\right)\right)}}^{2} + b \cdot b \]
                    3. Step-by-step derivation
                      1. Applied rewrites72.9%

                        \[\leadsto {\color{blue}{\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot 0.005555555555555556\right)}}^{2} + b \cdot b \]
                      2. Step-by-step derivation
                        1. lift-+.f64N/A

                          \[\leadsto \color{blue}{{\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot \frac{1}{180}\right)}^{2} + b \cdot b} \]
                        2. lift-pow.f64N/A

                          \[\leadsto \color{blue}{{\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot \frac{1}{180}\right)}^{2}} + b \cdot b \]
                        3. unpow2N/A

                          \[\leadsto \color{blue}{\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot \frac{1}{180}\right) \cdot \left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot \frac{1}{180}\right)} + b \cdot b \]
                        4. lower-fma.f6472.9

                          \[\leadsto \color{blue}{\mathsf{fma}\left(\left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot 0.005555555555555556, \left(\left(\mathsf{PI}\left(\right) \cdot angle\right) \cdot a\right) \cdot 0.005555555555555556, b \cdot b\right)} \]
                      3. Applied rewrites72.9%

                        \[\leadsto \color{blue}{\mathsf{fma}\left(\left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right), \left(0.005555555555555556 \cdot a\right) \cdot \left(\mathsf{PI}\left(\right) \cdot angle\right), b \cdot b\right)} \]
                    4. Recombined 2 regimes into one program.
                    5. Add Preprocessing

                    Alternative 4: 57.7% accurate, 74.7× speedup?

                    \[\begin{array}{l} \\ b \cdot b \end{array} \]
                    (FPCore (a b angle) :precision binary64 (* b b))
                    double code(double a, double b, double angle) {
                    	return b * b;
                    }
                    
                    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(8) function code(a, b, angle)
                    use fmin_fmax_functions
                        real(8), intent (in) :: a
                        real(8), intent (in) :: b
                        real(8), intent (in) :: angle
                        code = b * b
                    end function
                    
                    public static double code(double a, double b, double angle) {
                    	return b * b;
                    }
                    
                    def code(a, b, angle):
                    	return b * b
                    
                    function code(a, b, angle)
                    	return Float64(b * b)
                    end
                    
                    function tmp = code(a, b, angle)
                    	tmp = b * b;
                    end
                    
                    code[a_, b_, angle_] := N[(b * b), $MachinePrecision]
                    
                    \begin{array}{l}
                    
                    \\
                    b \cdot b
                    \end{array}
                    
                    Derivation
                    1. Initial program 78.8%

                      \[{\left(a \cdot \sin \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} + {\left(b \cdot \cos \left(\frac{angle}{180} \cdot \mathsf{PI}\left(\right)\right)\right)}^{2} \]
                    2. Add Preprocessing
                    3. Taylor expanded in angle around 0

                      \[\leadsto \color{blue}{{b}^{2}} \]
                    4. Step-by-step derivation
                      1. Applied rewrites53.9%

                        \[\leadsto \color{blue}{b \cdot b} \]
                      2. Add Preprocessing

                      Reproduce

                      ?
                      herbie shell --seed 2025025 
                      (FPCore (a b angle)
                        :name "ab-angle->ABCF A"
                        :precision binary64
                        (+ (pow (* a (sin (* (/ angle 180.0) (PI)))) 2.0) (pow (* b (cos (* (/ angle 180.0) (PI)))) 2.0)))