invcot (example 3.9)

Percentage Accurate: 6.3% → 99.5%
Time: 17.1s
Alternatives: 7
Speedup: 21.0×

Specification

?
\[-0.026 < x \land x < 0.026\]
\[\begin{array}{l} \\ \frac{1}{x} - \frac{1}{\tan x} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
double code(double x) {
	return (1.0 / x) - (1.0 / tan(x));
}
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(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
public static double code(double x) {
	return (1.0 / x) - (1.0 / Math.tan(x));
}
def code(x):
	return (1.0 / x) - (1.0 / math.tan(x))
function code(x)
	return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)))
end
function tmp = code(x)
	tmp = (1.0 / x) - (1.0 / tan(x));
end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x} - \frac{1}{\tan x}
\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 7 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: 6.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{x} - \frac{1}{\tan x} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 x) (/ 1.0 (tan x))))
double code(double x) {
	return (1.0 / x) - (1.0 / tan(x));
}
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(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (1.0d0 / x) - (1.0d0 / tan(x))
end function
public static double code(double x) {
	return (1.0 / x) - (1.0 / Math.tan(x));
}
def code(x):
	return (1.0 / x) - (1.0 / math.tan(x))
function code(x)
	return Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)))
end
function tmp = code(x)
	tmp = (1.0 / x) - (1.0 / tan(x));
end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x} - \frac{1}{\tan x}
\end{array}

Alternative 1: 99.5% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \frac{-0.012345679012345678 \cdot x}{\mathsf{fma}\left(0.0004938271604938272, {x}^{4}, 0.1111111111111111\right) \cdot \left(0.022222222222222223 \cdot \left(x \cdot x\right) - 0.3333333333333333\right)} \end{array} \]
(FPCore (x)
 :precision binary64
 (/
  (* -0.012345679012345678 x)
  (*
   (fma 0.0004938271604938272 (pow x 4.0) 0.1111111111111111)
   (- (* 0.022222222222222223 (* x x)) 0.3333333333333333))))
double code(double x) {
	return (-0.012345679012345678 * x) / (fma(0.0004938271604938272, pow(x, 4.0), 0.1111111111111111) * ((0.022222222222222223 * (x * x)) - 0.3333333333333333));
}
function code(x)
	return Float64(Float64(-0.012345679012345678 * x) / Float64(fma(0.0004938271604938272, (x ^ 4.0), 0.1111111111111111) * Float64(Float64(0.022222222222222223 * Float64(x * x)) - 0.3333333333333333)))
end
code[x_] := N[(N[(-0.012345679012345678 * x), $MachinePrecision] / N[(N[(0.0004938271604938272 * N[Power[x, 4.0], $MachinePrecision] + 0.1111111111111111), $MachinePrecision] * N[(N[(0.022222222222222223 * N[(x * x), $MachinePrecision]), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{-0.012345679012345678 \cdot x}{\mathsf{fma}\left(0.0004938271604938272, {x}^{4}, 0.1111111111111111\right) \cdot \left(0.022222222222222223 \cdot \left(x \cdot x\right) - 0.3333333333333333\right)}
\end{array}
Derivation
  1. Initial program 7.2%

    \[\frac{1}{x} - \frac{1}{\tan x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
    2. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
    3. fp-cancel-sign-sub-invN/A

      \[\leadsto \color{blue}{\left(\frac{1}{3} - \left(\mathsf{neg}\left(\frac{1}{45}\right)\right) \cdot {x}^{2}\right)} \cdot x \]
    4. metadata-evalN/A

      \[\leadsto \left(\frac{1}{3} - \color{blue}{\frac{-1}{45}} \cdot {x}^{2}\right) \cdot x \]
    5. *-commutativeN/A

      \[\leadsto \left(\frac{1}{3} - \color{blue}{{x}^{2} \cdot \frac{-1}{45}}\right) \cdot x \]
    6. fp-cancel-sub-sign-invN/A

      \[\leadsto \color{blue}{\left(\frac{1}{3} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45}\right)} \cdot x \]
    7. +-commutativeN/A

      \[\leadsto \color{blue}{\left(\left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45} + \frac{1}{3}\right)} \cdot x \]
    8. *-commutativeN/A

      \[\leadsto \left(\color{blue}{\frac{-1}{45} \cdot \left(\mathsf{neg}\left({x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
    9. distribute-rgt-neg-inN/A

      \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45} \cdot {x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
    10. distribute-lft-neg-inN/A

      \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45}\right)\right) \cdot {x}^{2}} + \frac{1}{3}\right) \cdot x \]
    11. metadata-evalN/A

      \[\leadsto \left(\color{blue}{\frac{1}{45}} \cdot {x}^{2} + \frac{1}{3}\right) \cdot x \]
    12. *-commutativeN/A

      \[\leadsto \left(\color{blue}{{x}^{2} \cdot \frac{1}{45}} + \frac{1}{3}\right) \cdot x \]
    13. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45}, \frac{1}{3}\right)} \cdot x \]
    14. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45}, \frac{1}{3}\right) \cdot x \]
    15. lower-*.f6499.4

      \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.022222222222222223, 0.3333333333333333\right) \cdot x \]
  5. Applied rewrites99.4%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, 0.022222222222222223, 0.3333333333333333\right) \cdot x} \]
  6. Step-by-step derivation
    1. Applied rewrites99.4%

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{0.3333333333333333}, \left(0.022222222222222223 \cdot \left(x \cdot x\right)\right) \cdot x\right) \]
    2. Applied rewrites99.6%

      \[\leadsto \frac{\left({x}^{8} \cdot 2.438652644413961 \cdot 10^{-7} - 0.012345679012345678\right) \cdot x}{\color{blue}{\mathsf{fma}\left(0.0004938271604938272, {x}^{4}, 0.1111111111111111\right) \cdot \left(0.022222222222222223 \cdot \left(x \cdot x\right) - 0.3333333333333333\right)}} \]
    3. Taylor expanded in x around 0

      \[\leadsto \frac{\frac{-1}{81} \cdot x}{\mathsf{fma}\left(\color{blue}{\frac{1}{2025}}, {x}^{4}, \frac{1}{9}\right) \cdot \left(\frac{1}{45} \cdot \left(x \cdot x\right) - \frac{1}{3}\right)} \]
    4. Step-by-step derivation
      1. Applied rewrites99.6%

        \[\leadsto \frac{-0.012345679012345678 \cdot x}{\mathsf{fma}\left(\color{blue}{0.0004938271604938272}, {x}^{4}, 0.1111111111111111\right) \cdot \left(0.022222222222222223 \cdot \left(x \cdot x\right) - 0.3333333333333333\right)} \]
      2. Add Preprocessing

      Alternative 2: 99.6% accurate, 4.2× speedup?

      \[\begin{array}{l} \\ \frac{x}{\left(x \cdot x\right) \cdot 0.022222222222222223 - 0.3333333333333333} \cdot -0.1111111111111111 \end{array} \]
      (FPCore (x)
       :precision binary64
       (*
        (/ x (- (* (* x x) 0.022222222222222223) 0.3333333333333333))
        -0.1111111111111111))
      double code(double x) {
      	return (x / (((x * x) * 0.022222222222222223) - 0.3333333333333333)) * -0.1111111111111111;
      }
      
      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(x)
      use fmin_fmax_functions
          real(8), intent (in) :: x
          code = (x / (((x * x) * 0.022222222222222223d0) - 0.3333333333333333d0)) * (-0.1111111111111111d0)
      end function
      
      public static double code(double x) {
      	return (x / (((x * x) * 0.022222222222222223) - 0.3333333333333333)) * -0.1111111111111111;
      }
      
      def code(x):
      	return (x / (((x * x) * 0.022222222222222223) - 0.3333333333333333)) * -0.1111111111111111
      
      function code(x)
      	return Float64(Float64(x / Float64(Float64(Float64(x * x) * 0.022222222222222223) - 0.3333333333333333)) * -0.1111111111111111)
      end
      
      function tmp = code(x)
      	tmp = (x / (((x * x) * 0.022222222222222223) - 0.3333333333333333)) * -0.1111111111111111;
      end
      
      code[x_] := N[(N[(x / N[(N[(N[(x * x), $MachinePrecision] * 0.022222222222222223), $MachinePrecision] - 0.3333333333333333), $MachinePrecision]), $MachinePrecision] * -0.1111111111111111), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \frac{x}{\left(x \cdot x\right) \cdot 0.022222222222222223 - 0.3333333333333333} \cdot -0.1111111111111111
      \end{array}
      
      Derivation
      1. Initial program 7.2%

        \[\frac{1}{x} - \frac{1}{\tan x} \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
        2. lower-*.f64N/A

          \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
        3. fp-cancel-sign-sub-invN/A

          \[\leadsto \color{blue}{\left(\frac{1}{3} - \left(\mathsf{neg}\left(\frac{1}{45}\right)\right) \cdot {x}^{2}\right)} \cdot x \]
        4. metadata-evalN/A

          \[\leadsto \left(\frac{1}{3} - \color{blue}{\frac{-1}{45}} \cdot {x}^{2}\right) \cdot x \]
        5. *-commutativeN/A

          \[\leadsto \left(\frac{1}{3} - \color{blue}{{x}^{2} \cdot \frac{-1}{45}}\right) \cdot x \]
        6. fp-cancel-sub-sign-invN/A

          \[\leadsto \color{blue}{\left(\frac{1}{3} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45}\right)} \cdot x \]
        7. +-commutativeN/A

          \[\leadsto \color{blue}{\left(\left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45} + \frac{1}{3}\right)} \cdot x \]
        8. *-commutativeN/A

          \[\leadsto \left(\color{blue}{\frac{-1}{45} \cdot \left(\mathsf{neg}\left({x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
        9. distribute-rgt-neg-inN/A

          \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45} \cdot {x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
        10. distribute-lft-neg-inN/A

          \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45}\right)\right) \cdot {x}^{2}} + \frac{1}{3}\right) \cdot x \]
        11. metadata-evalN/A

          \[\leadsto \left(\color{blue}{\frac{1}{45}} \cdot {x}^{2} + \frac{1}{3}\right) \cdot x \]
        12. *-commutativeN/A

          \[\leadsto \left(\color{blue}{{x}^{2} \cdot \frac{1}{45}} + \frac{1}{3}\right) \cdot x \]
        13. lower-fma.f64N/A

          \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45}, \frac{1}{3}\right)} \cdot x \]
        14. unpow2N/A

          \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45}, \frac{1}{3}\right) \cdot x \]
        15. lower-*.f6499.4

          \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.022222222222222223, 0.3333333333333333\right) \cdot x \]
      5. Applied rewrites99.4%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, 0.022222222222222223, 0.3333333333333333\right) \cdot x} \]
      6. Step-by-step derivation
        1. Applied rewrites99.5%

          \[\leadsto \frac{\left({x}^{4} \cdot 0.0004938271604938272 - 0.1111111111111111\right) \cdot x}{\color{blue}{0.022222222222222223 \cdot \left(x \cdot x\right) - 0.3333333333333333}} \]
        2. Taylor expanded in x around 0

          \[\leadsto \frac{\frac{-1}{9} \cdot x}{\color{blue}{\frac{1}{45}} \cdot \left(x \cdot x\right) - \frac{1}{3}} \]
        3. Step-by-step derivation
          1. Applied rewrites99.5%

            \[\leadsto \frac{-0.1111111111111111 \cdot x}{\color{blue}{0.022222222222222223} \cdot \left(x \cdot x\right) - 0.3333333333333333} \]
          2. Step-by-step derivation
            1. Applied rewrites99.5%

              \[\leadsto \frac{x}{\left(x \cdot x\right) \cdot 0.022222222222222223 - 0.3333333333333333} \cdot \color{blue}{-0.1111111111111111} \]
            2. Add Preprocessing

            Alternative 3: 99.5% accurate, 4.5× speedup?

            \[\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(0.0021164021164021165, x \cdot x, 0.022222222222222223\right), x \cdot x, 0.3333333333333333\right) \cdot x \end{array} \]
            (FPCore (x)
             :precision binary64
             (*
              (fma
               (fma 0.0021164021164021165 (* x x) 0.022222222222222223)
               (* x x)
               0.3333333333333333)
              x))
            double code(double x) {
            	return fma(fma(0.0021164021164021165, (x * x), 0.022222222222222223), (x * x), 0.3333333333333333) * x;
            }
            
            function code(x)
            	return Float64(fma(fma(0.0021164021164021165, Float64(x * x), 0.022222222222222223), Float64(x * x), 0.3333333333333333) * x)
            end
            
            code[x_] := N[(N[(N[(0.0021164021164021165 * N[(x * x), $MachinePrecision] + 0.022222222222222223), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.3333333333333333), $MachinePrecision] * x), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            \mathsf{fma}\left(\mathsf{fma}\left(0.0021164021164021165, x \cdot x, 0.022222222222222223\right), x \cdot x, 0.3333333333333333\right) \cdot x
            \end{array}
            
            Derivation
            1. Initial program 7.2%

              \[\frac{1}{x} - \frac{1}{\tan x} \]
            2. Add Preprocessing
            3. Taylor expanded in x around 0

              \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{45} + \frac{2}{945} \cdot {x}^{2}\right)\right)} \]
            4. Applied rewrites99.5%

              \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(0.0021164021164021165, x \cdot x, 0.022222222222222223\right), x \cdot x, 0.3333333333333333\right) \cdot x} \]
            5. Add Preprocessing

            Alternative 4: 99.4% accurate, 5.7× speedup?

            \[\begin{array}{l} \\ \mathsf{fma}\left(0.022222222222222223 \cdot \left(x \cdot x\right), x, 0.3333333333333333 \cdot x\right) \end{array} \]
            (FPCore (x)
             :precision binary64
             (fma (* 0.022222222222222223 (* x x)) x (* 0.3333333333333333 x)))
            double code(double x) {
            	return fma((0.022222222222222223 * (x * x)), x, (0.3333333333333333 * x));
            }
            
            function code(x)
            	return fma(Float64(0.022222222222222223 * Float64(x * x)), x, Float64(0.3333333333333333 * x))
            end
            
            code[x_] := N[(N[(0.022222222222222223 * N[(x * x), $MachinePrecision]), $MachinePrecision] * x + N[(0.3333333333333333 * x), $MachinePrecision]), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            \mathsf{fma}\left(0.022222222222222223 \cdot \left(x \cdot x\right), x, 0.3333333333333333 \cdot x\right)
            \end{array}
            
            Derivation
            1. Initial program 7.2%

              \[\frac{1}{x} - \frac{1}{\tan x} \]
            2. Add Preprocessing
            3. Taylor expanded in x around 0

              \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
            4. Step-by-step derivation
              1. *-commutativeN/A

                \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
              2. lower-*.f64N/A

                \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
              3. fp-cancel-sign-sub-invN/A

                \[\leadsto \color{blue}{\left(\frac{1}{3} - \left(\mathsf{neg}\left(\frac{1}{45}\right)\right) \cdot {x}^{2}\right)} \cdot x \]
              4. metadata-evalN/A

                \[\leadsto \left(\frac{1}{3} - \color{blue}{\frac{-1}{45}} \cdot {x}^{2}\right) \cdot x \]
              5. *-commutativeN/A

                \[\leadsto \left(\frac{1}{3} - \color{blue}{{x}^{2} \cdot \frac{-1}{45}}\right) \cdot x \]
              6. fp-cancel-sub-sign-invN/A

                \[\leadsto \color{blue}{\left(\frac{1}{3} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45}\right)} \cdot x \]
              7. +-commutativeN/A

                \[\leadsto \color{blue}{\left(\left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45} + \frac{1}{3}\right)} \cdot x \]
              8. *-commutativeN/A

                \[\leadsto \left(\color{blue}{\frac{-1}{45} \cdot \left(\mathsf{neg}\left({x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
              9. distribute-rgt-neg-inN/A

                \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45} \cdot {x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
              10. distribute-lft-neg-inN/A

                \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45}\right)\right) \cdot {x}^{2}} + \frac{1}{3}\right) \cdot x \]
              11. metadata-evalN/A

                \[\leadsto \left(\color{blue}{\frac{1}{45}} \cdot {x}^{2} + \frac{1}{3}\right) \cdot x \]
              12. *-commutativeN/A

                \[\leadsto \left(\color{blue}{{x}^{2} \cdot \frac{1}{45}} + \frac{1}{3}\right) \cdot x \]
              13. lower-fma.f64N/A

                \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45}, \frac{1}{3}\right)} \cdot x \]
              14. unpow2N/A

                \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45}, \frac{1}{3}\right) \cdot x \]
              15. lower-*.f6499.4

                \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.022222222222222223, 0.3333333333333333\right) \cdot x \]
            5. Applied rewrites99.4%

              \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, 0.022222222222222223, 0.3333333333333333\right) \cdot x} \]
            6. Step-by-step derivation
              1. Applied rewrites99.4%

                \[\leadsto \mathsf{fma}\left(0.022222222222222223 \cdot \left(x \cdot x\right), \color{blue}{x}, 0.3333333333333333 \cdot x\right) \]
              2. Add Preprocessing

              Alternative 5: 99.4% accurate, 5.7× speedup?

              \[\begin{array}{l} \\ \mathsf{fma}\left(x, 0.3333333333333333, \left(0.022222222222222223 \cdot \left(x \cdot x\right)\right) \cdot x\right) \end{array} \]
              (FPCore (x)
               :precision binary64
               (fma x 0.3333333333333333 (* (* 0.022222222222222223 (* x x)) x)))
              double code(double x) {
              	return fma(x, 0.3333333333333333, ((0.022222222222222223 * (x * x)) * x));
              }
              
              function code(x)
              	return fma(x, 0.3333333333333333, Float64(Float64(0.022222222222222223 * Float64(x * x)) * x))
              end
              
              code[x_] := N[(x * 0.3333333333333333 + N[(N[(0.022222222222222223 * N[(x * x), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
              
              \begin{array}{l}
              
              \\
              \mathsf{fma}\left(x, 0.3333333333333333, \left(0.022222222222222223 \cdot \left(x \cdot x\right)\right) \cdot x\right)
              \end{array}
              
              Derivation
              1. Initial program 7.2%

                \[\frac{1}{x} - \frac{1}{\tan x} \]
              2. Add Preprocessing
              3. Taylor expanded in x around 0

                \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
              4. Step-by-step derivation
                1. *-commutativeN/A

                  \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
                2. lower-*.f64N/A

                  \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
                3. fp-cancel-sign-sub-invN/A

                  \[\leadsto \color{blue}{\left(\frac{1}{3} - \left(\mathsf{neg}\left(\frac{1}{45}\right)\right) \cdot {x}^{2}\right)} \cdot x \]
                4. metadata-evalN/A

                  \[\leadsto \left(\frac{1}{3} - \color{blue}{\frac{-1}{45}} \cdot {x}^{2}\right) \cdot x \]
                5. *-commutativeN/A

                  \[\leadsto \left(\frac{1}{3} - \color{blue}{{x}^{2} \cdot \frac{-1}{45}}\right) \cdot x \]
                6. fp-cancel-sub-sign-invN/A

                  \[\leadsto \color{blue}{\left(\frac{1}{3} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45}\right)} \cdot x \]
                7. +-commutativeN/A

                  \[\leadsto \color{blue}{\left(\left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45} + \frac{1}{3}\right)} \cdot x \]
                8. *-commutativeN/A

                  \[\leadsto \left(\color{blue}{\frac{-1}{45} \cdot \left(\mathsf{neg}\left({x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
                9. distribute-rgt-neg-inN/A

                  \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45} \cdot {x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
                10. distribute-lft-neg-inN/A

                  \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45}\right)\right) \cdot {x}^{2}} + \frac{1}{3}\right) \cdot x \]
                11. metadata-evalN/A

                  \[\leadsto \left(\color{blue}{\frac{1}{45}} \cdot {x}^{2} + \frac{1}{3}\right) \cdot x \]
                12. *-commutativeN/A

                  \[\leadsto \left(\color{blue}{{x}^{2} \cdot \frac{1}{45}} + \frac{1}{3}\right) \cdot x \]
                13. lower-fma.f64N/A

                  \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45}, \frac{1}{3}\right)} \cdot x \]
                14. unpow2N/A

                  \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45}, \frac{1}{3}\right) \cdot x \]
                15. lower-*.f6499.4

                  \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.022222222222222223, 0.3333333333333333\right) \cdot x \]
              5. Applied rewrites99.4%

                \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, 0.022222222222222223, 0.3333333333333333\right) \cdot x} \]
              6. Step-by-step derivation
                1. Applied rewrites99.4%

                  \[\leadsto \mathsf{fma}\left(x, \color{blue}{0.3333333333333333}, \left(0.022222222222222223 \cdot \left(x \cdot x\right)\right) \cdot x\right) \]
                2. Add Preprocessing

                Alternative 6: 99.4% accurate, 7.4× speedup?

                \[\begin{array}{l} \\ \mathsf{fma}\left(x \cdot x, 0.022222222222222223, 0.3333333333333333\right) \cdot x \end{array} \]
                (FPCore (x)
                 :precision binary64
                 (* (fma (* x x) 0.022222222222222223 0.3333333333333333) x))
                double code(double x) {
                	return fma((x * x), 0.022222222222222223, 0.3333333333333333) * x;
                }
                
                function code(x)
                	return Float64(fma(Float64(x * x), 0.022222222222222223, 0.3333333333333333) * x)
                end
                
                code[x_] := N[(N[(N[(x * x), $MachinePrecision] * 0.022222222222222223 + 0.3333333333333333), $MachinePrecision] * x), $MachinePrecision]
                
                \begin{array}{l}
                
                \\
                \mathsf{fma}\left(x \cdot x, 0.022222222222222223, 0.3333333333333333\right) \cdot x
                \end{array}
                
                Derivation
                1. Initial program 7.2%

                  \[\frac{1}{x} - \frac{1}{\tan x} \]
                2. Add Preprocessing
                3. Taylor expanded in x around 0

                  \[\leadsto \color{blue}{x \cdot \left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right)} \]
                4. Step-by-step derivation
                  1. *-commutativeN/A

                    \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
                  2. lower-*.f64N/A

                    \[\leadsto \color{blue}{\left(\frac{1}{3} + \frac{1}{45} \cdot {x}^{2}\right) \cdot x} \]
                  3. fp-cancel-sign-sub-invN/A

                    \[\leadsto \color{blue}{\left(\frac{1}{3} - \left(\mathsf{neg}\left(\frac{1}{45}\right)\right) \cdot {x}^{2}\right)} \cdot x \]
                  4. metadata-evalN/A

                    \[\leadsto \left(\frac{1}{3} - \color{blue}{\frac{-1}{45}} \cdot {x}^{2}\right) \cdot x \]
                  5. *-commutativeN/A

                    \[\leadsto \left(\frac{1}{3} - \color{blue}{{x}^{2} \cdot \frac{-1}{45}}\right) \cdot x \]
                  6. fp-cancel-sub-sign-invN/A

                    \[\leadsto \color{blue}{\left(\frac{1}{3} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45}\right)} \cdot x \]
                  7. +-commutativeN/A

                    \[\leadsto \color{blue}{\left(\left(\mathsf{neg}\left({x}^{2}\right)\right) \cdot \frac{-1}{45} + \frac{1}{3}\right)} \cdot x \]
                  8. *-commutativeN/A

                    \[\leadsto \left(\color{blue}{\frac{-1}{45} \cdot \left(\mathsf{neg}\left({x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
                  9. distribute-rgt-neg-inN/A

                    \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45} \cdot {x}^{2}\right)\right)} + \frac{1}{3}\right) \cdot x \]
                  10. distribute-lft-neg-inN/A

                    \[\leadsto \left(\color{blue}{\left(\mathsf{neg}\left(\frac{-1}{45}\right)\right) \cdot {x}^{2}} + \frac{1}{3}\right) \cdot x \]
                  11. metadata-evalN/A

                    \[\leadsto \left(\color{blue}{\frac{1}{45}} \cdot {x}^{2} + \frac{1}{3}\right) \cdot x \]
                  12. *-commutativeN/A

                    \[\leadsto \left(\color{blue}{{x}^{2} \cdot \frac{1}{45}} + \frac{1}{3}\right) \cdot x \]
                  13. lower-fma.f64N/A

                    \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{2}, \frac{1}{45}, \frac{1}{3}\right)} \cdot x \]
                  14. unpow2N/A

                    \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, \frac{1}{45}, \frac{1}{3}\right) \cdot x \]
                  15. lower-*.f6499.4

                    \[\leadsto \mathsf{fma}\left(\color{blue}{x \cdot x}, 0.022222222222222223, 0.3333333333333333\right) \cdot x \]
                5. Applied rewrites99.4%

                  \[\leadsto \color{blue}{\mathsf{fma}\left(x \cdot x, 0.022222222222222223, 0.3333333333333333\right) \cdot x} \]
                6. Add Preprocessing

                Alternative 7: 99.0% accurate, 21.0× speedup?

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

                  \[\frac{1}{x} - \frac{1}{\tan x} \]
                2. Add Preprocessing
                3. Taylor expanded in x around 0

                  \[\leadsto \color{blue}{\frac{1}{3} \cdot x} \]
                4. Step-by-step derivation
                  1. lower-*.f6498.6

                    \[\leadsto \color{blue}{0.3333333333333333 \cdot x} \]
                5. Applied rewrites98.6%

                  \[\leadsto \color{blue}{0.3333333333333333 \cdot x} \]
                6. Add Preprocessing

                Developer Target 1: 99.9% accurate, 0.9× speedup?

                \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|x\right| < 0.026:\\ \;\;\;\;\frac{x}{3} \cdot \left(1 + \frac{x \cdot x}{15}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{x} - \frac{1}{\tan x}\\ \end{array} \end{array} \]
                (FPCore (x)
                 :precision binary64
                 (if (< (fabs x) 0.026)
                   (* (/ x 3.0) (+ 1.0 (/ (* x x) 15.0)))
                   (- (/ 1.0 x) (/ 1.0 (tan x)))))
                double code(double x) {
                	double tmp;
                	if (fabs(x) < 0.026) {
                		tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
                	} else {
                		tmp = (1.0 / x) - (1.0 / tan(x));
                	}
                	return tmp;
                }
                
                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(x)
                use fmin_fmax_functions
                    real(8), intent (in) :: x
                    real(8) :: tmp
                    if (abs(x) < 0.026d0) then
                        tmp = (x / 3.0d0) * (1.0d0 + ((x * x) / 15.0d0))
                    else
                        tmp = (1.0d0 / x) - (1.0d0 / tan(x))
                    end if
                    code = tmp
                end function
                
                public static double code(double x) {
                	double tmp;
                	if (Math.abs(x) < 0.026) {
                		tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
                	} else {
                		tmp = (1.0 / x) - (1.0 / Math.tan(x));
                	}
                	return tmp;
                }
                
                def code(x):
                	tmp = 0
                	if math.fabs(x) < 0.026:
                		tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0))
                	else:
                		tmp = (1.0 / x) - (1.0 / math.tan(x))
                	return tmp
                
                function code(x)
                	tmp = 0.0
                	if (abs(x) < 0.026)
                		tmp = Float64(Float64(x / 3.0) * Float64(1.0 + Float64(Float64(x * x) / 15.0)));
                	else
                		tmp = Float64(Float64(1.0 / x) - Float64(1.0 / tan(x)));
                	end
                	return tmp
                end
                
                function tmp_2 = code(x)
                	tmp = 0.0;
                	if (abs(x) < 0.026)
                		tmp = (x / 3.0) * (1.0 + ((x * x) / 15.0));
                	else
                		tmp = (1.0 / x) - (1.0 / tan(x));
                	end
                	tmp_2 = tmp;
                end
                
                code[x_] := If[Less[N[Abs[x], $MachinePrecision], 0.026], N[(N[(x / 3.0), $MachinePrecision] * N[(1.0 + N[(N[(x * x), $MachinePrecision] / 15.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] - N[(1.0 / N[Tan[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
                
                \begin{array}{l}
                
                \\
                \begin{array}{l}
                \mathbf{if}\;\left|x\right| < 0.026:\\
                \;\;\;\;\frac{x}{3} \cdot \left(1 + \frac{x \cdot x}{15}\right)\\
                
                \mathbf{else}:\\
                \;\;\;\;\frac{1}{x} - \frac{1}{\tan x}\\
                
                
                \end{array}
                \end{array}
                

                Reproduce

                ?
                herbie shell --seed 2024356 
                (FPCore (x)
                  :name "invcot (example 3.9)"
                  :precision binary64
                  :pre (and (< -0.026 x) (< x 0.026))
                
                  :alt
                  (! :herbie-platform default (if (< (fabs x) 13/500) (* (/ x 3) (+ 1 (/ (* x x) 15))) (- (/ 1 x) (/ 1 (tan x)))))
                
                  (- (/ 1.0 x) (/ 1.0 (tan x))))