ENA, Section 1.4, Mentioned, A

Percentage Accurate: 52.9% → 100.0%
Time: 9.9s
Alternatives: 7
Speedup: 9.5×

Specification

?
\[-0.01 \leq x \land x \leq 0.01\]
\[\begin{array}{l} \\ 1 - \cos x \end{array} \]
(FPCore (x) :precision binary64 (- 1.0 (cos x)))
double code(double x) {
	return 1.0 - cos(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0 - cos(x)
end function
public static double code(double x) {
	return 1.0 - Math.cos(x);
}
def code(x):
	return 1.0 - math.cos(x)
function code(x)
	return Float64(1.0 - cos(x))
end
function tmp = code(x)
	tmp = 1.0 - cos(x);
end
code[x_] := N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
1 - \cos 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: 52.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 1 - \cos x \end{array} \]
(FPCore (x) :precision binary64 (- 1.0 (cos x)))
double code(double x) {
	return 1.0 - cos(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0 - cos(x)
end function
public static double code(double x) {
	return 1.0 - Math.cos(x);
}
def code(x):
	return 1.0 - math.cos(x)
function code(x)
	return Float64(1.0 - cos(x))
end
function tmp = code(x)
	tmp = 1.0 - cos(x);
end
code[x_] := N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
1 - \cos x
\end{array}

Alternative 1: 100.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \tan \left(\frac{x}{2}\right) \cdot \sin x \end{array} \]
(FPCore (x) :precision binary64 (* (tan (/ x 2.0)) (sin x)))
double code(double x) {
	return tan((x / 2.0)) * sin(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = tan((x / 2.0d0)) * sin(x)
end function
public static double code(double x) {
	return Math.tan((x / 2.0)) * Math.sin(x);
}
def code(x):
	return math.tan((x / 2.0)) * math.sin(x)
function code(x)
	return Float64(tan(Float64(x / 2.0)) * sin(x))
end
function tmp = code(x)
	tmp = tan((x / 2.0)) * sin(x);
end
code[x_] := N[(N[Tan[N[(x / 2.0), $MachinePrecision]], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\tan \left(\frac{x}{2}\right) \cdot \sin x
\end{array}
Derivation
  1. Initial program 50.5%

    \[1 - \cos x \]
  2. Add Preprocessing
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\sin x \cdot \tan \left(\frac{x}{2}\right)} \]
  4. Final simplification100.0%

    \[\leadsto \tan \left(\frac{x}{2}\right) \cdot \sin x \]
  5. Add Preprocessing

Alternative 2: 100.0% accurate, 3.2× speedup?

\[\begin{array}{l} \\ \left(\mathsf{fma}\left(\mathsf{fma}\left(0.001388888888888889, x \cdot x, -0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x\right) \cdot x \end{array} \]
(FPCore (x)
 :precision binary64
 (*
  (*
   (fma (fma 0.001388888888888889 (* x x) -0.041666666666666664) (* x x) 0.5)
   x)
  x))
double code(double x) {
	return (fma(fma(0.001388888888888889, (x * x), -0.041666666666666664), (x * x), 0.5) * x) * x;
}
function code(x)
	return Float64(Float64(fma(fma(0.001388888888888889, Float64(x * x), -0.041666666666666664), Float64(x * x), 0.5) * x) * x)
end
code[x_] := N[(N[(N[(N[(0.001388888888888889 * N[(x * x), $MachinePrecision] + -0.041666666666666664), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.5), $MachinePrecision] * x), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}

\\
\left(\mathsf{fma}\left(\mathsf{fma}\left(0.001388888888888889, x \cdot x, -0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x\right) \cdot x
\end{array}
Derivation
  1. Initial program 50.5%

    \[1 - \cos x \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

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

      \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \left(\frac{1}{2} + {x}^{2} \cdot \left(\frac{1}{720} \cdot {x}^{2} - \frac{1}{24}\right)\right) \]
    2. associate-*l*N/A

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

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

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

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

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

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

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

      \[\leadsto \left(\color{blue}{\mathsf{fma}\left(\frac{1}{720} \cdot {x}^{2} - \frac{1}{24}, {x}^{2}, \frac{1}{2}\right)} \cdot x\right) \cdot x \]
    10. sub-negN/A

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

      \[\leadsto \left(\mathsf{fma}\left(\frac{1}{720} \cdot {x}^{2} + \color{blue}{\frac{-1}{24}}, {x}^{2}, \frac{1}{2}\right) \cdot x\right) \cdot x \]
    12. lower-fma.f64N/A

      \[\leadsto \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\frac{1}{720}, {x}^{2}, \frac{-1}{24}\right)}, {x}^{2}, \frac{1}{2}\right) \cdot x\right) \cdot x \]
    13. unpow2N/A

      \[\leadsto \left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{720}, \color{blue}{x \cdot x}, \frac{-1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x\right) \cdot x \]
    14. lower-*.f64N/A

      \[\leadsto \left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{720}, \color{blue}{x \cdot x}, \frac{-1}{24}\right), {x}^{2}, \frac{1}{2}\right) \cdot x\right) \cdot x \]
    15. unpow2N/A

      \[\leadsto \left(\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{720}, x \cdot x, \frac{-1}{24}\right), \color{blue}{x \cdot x}, \frac{1}{2}\right) \cdot x\right) \cdot x \]
    16. lower-*.f64100.0

      \[\leadsto \left(\mathsf{fma}\left(\mathsf{fma}\left(0.001388888888888889, x \cdot x, -0.041666666666666664\right), \color{blue}{x \cdot x}, 0.5\right) \cdot x\right) \cdot x \]
  5. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(\mathsf{fma}\left(\mathsf{fma}\left(0.001388888888888889, x \cdot x, -0.041666666666666664\right), x \cdot x, 0.5\right) \cdot x\right) \cdot x} \]
  6. Add Preprocessing

Alternative 3: 99.9% accurate, 3.3× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(0.5 \cdot x, x, \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right) \cdot -0.041666666666666664\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (fma (* 0.5 x) x (* (* (* x x) (* x x)) -0.041666666666666664)))
double code(double x) {
	return fma((0.5 * x), x, (((x * x) * (x * x)) * -0.041666666666666664));
}
function code(x)
	return fma(Float64(0.5 * x), x, Float64(Float64(Float64(x * x) * Float64(x * x)) * -0.041666666666666664))
end
code[x_] := N[(N[(0.5 * x), $MachinePrecision] * x + N[(N[(N[(x * x), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision] * -0.041666666666666664), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(0.5 \cdot x, x, \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right) \cdot -0.041666666666666664\right)
\end{array}
Derivation
  1. Initial program 50.5%

    \[1 - \cos x \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

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

      \[\leadsto \color{blue}{\left(\frac{1}{2} + \frac{-1}{24} \cdot {x}^{2}\right) \cdot {x}^{2}} \]
    2. unpow2N/A

      \[\leadsto \left(\frac{1}{2} + \frac{-1}{24} \cdot {x}^{2}\right) \cdot \color{blue}{\left(x \cdot x\right)} \]
    3. associate-*r*N/A

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

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

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

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

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

      \[\leadsto \left(\mathsf{fma}\left(\frac{-1}{24}, \color{blue}{x \cdot x}, \frac{1}{2}\right) \cdot x\right) \cdot x \]
    9. lower-*.f6499.9

      \[\leadsto \left(\mathsf{fma}\left(-0.041666666666666664, \color{blue}{x \cdot x}, 0.5\right) \cdot x\right) \cdot x \]
  5. Applied rewrites99.9%

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

      \[\leadsto \mathsf{fma}\left(0.5 \cdot x, \color{blue}{x}, {x}^{4} \cdot -0.041666666666666664\right) \]
    2. Step-by-step derivation
      1. Applied rewrites99.9%

        \[\leadsto \mathsf{fma}\left(0.5 \cdot x, x, \left(\left(x \cdot x\right) \cdot \left(x \cdot x\right)\right) \cdot -0.041666666666666664\right) \]
      2. Add Preprocessing

      Alternative 4: 99.9% accurate, 4.7× speedup?

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

        \[1 - \cos x \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

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

          \[\leadsto \color{blue}{\left(\frac{1}{2} + \frac{-1}{24} \cdot {x}^{2}\right) \cdot {x}^{2}} \]
        2. unpow2N/A

          \[\leadsto \left(\frac{1}{2} + \frac{-1}{24} \cdot {x}^{2}\right) \cdot \color{blue}{\left(x \cdot x\right)} \]
        3. associate-*r*N/A

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

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

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

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

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

          \[\leadsto \left(\mathsf{fma}\left(\frac{-1}{24}, \color{blue}{x \cdot x}, \frac{1}{2}\right) \cdot x\right) \cdot x \]
        9. lower-*.f6499.9

          \[\leadsto \left(\mathsf{fma}\left(-0.041666666666666664, \color{blue}{x \cdot x}, 0.5\right) \cdot x\right) \cdot x \]
      5. Applied rewrites99.9%

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

      Alternative 5: 99.5% accurate, 9.5× speedup?

      \[\begin{array}{l} \\ 0.5 \cdot \left(x \cdot x\right) \end{array} \]
      (FPCore (x) :precision binary64 (* 0.5 (* x x)))
      double code(double x) {
      	return 0.5 * (x * x);
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = 0.5d0 * (x * x)
      end function
      
      public static double code(double x) {
      	return 0.5 * (x * x);
      }
      
      def code(x):
      	return 0.5 * (x * x)
      
      function code(x)
      	return Float64(0.5 * Float64(x * x))
      end
      
      function tmp = code(x)
      	tmp = 0.5 * (x * x);
      end
      
      code[x_] := N[(0.5 * N[(x * x), $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      0.5 \cdot \left(x \cdot x\right)
      \end{array}
      
      Derivation
      1. Initial program 50.5%

        \[1 - \cos x \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\frac{1}{2} \cdot {x}^{2}} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{{x}^{2} \cdot \frac{1}{2}} \]
        2. lower-*.f64N/A

          \[\leadsto \color{blue}{{x}^{2} \cdot \frac{1}{2}} \]
        3. unpow2N/A

          \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot \frac{1}{2} \]
        4. lower-*.f6499.5

          \[\leadsto \color{blue}{\left(x \cdot x\right)} \cdot 0.5 \]
      5. Applied rewrites99.5%

        \[\leadsto \color{blue}{\left(x \cdot x\right) \cdot 0.5} \]
      6. Final simplification99.5%

        \[\leadsto 0.5 \cdot \left(x \cdot x\right) \]
      7. Add Preprocessing

      Alternative 6: 52.0% accurate, 26.0× speedup?

      \[\begin{array}{l} \\ 1 - 1 \end{array} \]
      (FPCore (x) :precision binary64 (- 1.0 1.0))
      double code(double x) {
      	return 1.0 - 1.0;
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = 1.0d0 - 1.0d0
      end function
      
      public static double code(double x) {
      	return 1.0 - 1.0;
      }
      
      def code(x):
      	return 1.0 - 1.0
      
      function code(x)
      	return Float64(1.0 - 1.0)
      end
      
      function tmp = code(x)
      	tmp = 1.0 - 1.0;
      end
      
      code[x_] := N[(1.0 - 1.0), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      1 - 1
      \end{array}
      
      Derivation
      1. Initial program 50.5%

        \[1 - \cos x \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto 1 - \color{blue}{1} \]
      4. Step-by-step derivation
        1. Applied rewrites49.6%

          \[\leadsto 1 - \color{blue}{1} \]
        2. Add Preprocessing

        Alternative 7: 4.2% accurate, 104.0× speedup?

        \[\begin{array}{l} \\ 2 \end{array} \]
        (FPCore (x) :precision binary64 2.0)
        double code(double x) {
        	return 2.0;
        }
        
        real(8) function code(x)
            real(8), intent (in) :: x
            code = 2.0d0
        end function
        
        public static double code(double x) {
        	return 2.0;
        }
        
        def code(x):
        	return 2.0
        
        function code(x)
        	return 2.0
        end
        
        function tmp = code(x)
        	tmp = 2.0;
        end
        
        code[x_] := 2.0
        
        \begin{array}{l}
        
        \\
        2
        \end{array}
        
        Derivation
        1. Initial program 50.5%

          \[1 - \cos x \]
        2. Add Preprocessing
        3. Taylor expanded in x around 0

          \[\leadsto 1 - \color{blue}{1} \]
        4. Step-by-step derivation
          1. Applied rewrites49.6%

            \[\leadsto 1 - \color{blue}{1} \]
          2. Applied rewrites4.2%

            \[\leadsto \color{blue}{1 + 1} \]
          3. Taylor expanded in x around 0

            \[\leadsto \color{blue}{2} \]
          4. Step-by-step derivation
            1. Applied rewrites4.2%

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

            Developer Target 1: 100.0% accurate, 0.3× speedup?

            \[\begin{array}{l} \\ \frac{\sin x \cdot \sin x}{1 + \cos x} \end{array} \]
            (FPCore (x) :precision binary64 (/ (* (sin x) (sin x)) (+ 1.0 (cos x))))
            double code(double x) {
            	return (sin(x) * sin(x)) / (1.0 + cos(x));
            }
            
            real(8) function code(x)
                real(8), intent (in) :: x
                code = (sin(x) * sin(x)) / (1.0d0 + cos(x))
            end function
            
            public static double code(double x) {
            	return (Math.sin(x) * Math.sin(x)) / (1.0 + Math.cos(x));
            }
            
            def code(x):
            	return (math.sin(x) * math.sin(x)) / (1.0 + math.cos(x))
            
            function code(x)
            	return Float64(Float64(sin(x) * sin(x)) / Float64(1.0 + cos(x)))
            end
            
            function tmp = code(x)
            	tmp = (sin(x) * sin(x)) / (1.0 + cos(x));
            end
            
            code[x_] := N[(N[(N[Sin[x], $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision] / N[(1.0 + N[Cos[x], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            \frac{\sin x \cdot \sin x}{1 + \cos x}
            \end{array}
            

            Reproduce

            ?
            herbie shell --seed 2024331 
            (FPCore (x)
              :name "ENA, Section 1.4, Mentioned, A"
              :precision binary64
              :pre (and (<= -0.01 x) (<= x 0.01))
            
              :alt
              (! :herbie-platform default (/ (* (sin x) (sin x)) (+ 1 (cos x))))
            
              (- 1.0 (cos x)))