(1-cos(x))* sin(x)*exp(x) + sin(x+10)

Percentage Accurate: 99.9% → 100.0%
Time: 5.4s
Alternatives: 5
Speedup: 3.4×

Specification

?
\[-1 \cdot 10^{-9} \leq x \land x \leq 0\]
\[\begin{array}{l} \\ \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (+ (* (* (- 1.0 (cos x)) (sin x)) (exp x)) (sin (+ x 10.0))))
double code(double x) {
	return (((1.0 - cos(x)) * sin(x)) * exp(x)) + sin((x + 10.0));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (((1.0d0 - cos(x)) * sin(x)) * exp(x)) + sin((x + 10.0d0))
end function
public static double code(double x) {
	return (((1.0 - Math.cos(x)) * Math.sin(x)) * Math.exp(x)) + Math.sin((x + 10.0));
}
def code(x):
	return (((1.0 - math.cos(x)) * math.sin(x)) * math.exp(x)) + math.sin((x + 10.0))
function code(x)
	return Float64(Float64(Float64(Float64(1.0 - cos(x)) * sin(x)) * exp(x)) + sin(Float64(x + 10.0)))
end
function tmp = code(x)
	tmp = (((1.0 - cos(x)) * sin(x)) * exp(x)) + sin((x + 10.0));
end
code[x_] := N[(N[(N[(N[(1.0 - N[Cos[x], $MachinePrecision]), $MachinePrecision] * N[Sin[x], $MachinePrecision]), $MachinePrecision] * N[Exp[x], $MachinePrecision]), $MachinePrecision] + N[Sin[N[(x + 10.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right)
\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 5 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.9% accurate, 1.0× speedup?

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

\\
\left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right)
\end{array}

Alternative 1: 100.0% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos 10 \cdot x\\ {\left(\frac{t\_0 - \sin 10}{{t\_0}^{2} - {\sin 10}^{2}}\right)}^{-1} \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (* (cos 10.0) x)))
   (pow (/ (- t_0 (sin 10.0)) (- (pow t_0 2.0) (pow (sin 10.0) 2.0))) -1.0)))
double code(double x) {
	double t_0 = cos(10.0) * x;
	return pow(((t_0 - sin(10.0)) / (pow(t_0, 2.0) - pow(sin(10.0), 2.0))), -1.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    t_0 = cos(10.0d0) * x
    code = ((t_0 - sin(10.0d0)) / ((t_0 ** 2.0d0) - (sin(10.0d0) ** 2.0d0))) ** (-1.0d0)
end function
public static double code(double x) {
	double t_0 = Math.cos(10.0) * x;
	return Math.pow(((t_0 - Math.sin(10.0)) / (Math.pow(t_0, 2.0) - Math.pow(Math.sin(10.0), 2.0))), -1.0);
}
def code(x):
	t_0 = math.cos(10.0) * x
	return math.pow(((t_0 - math.sin(10.0)) / (math.pow(t_0, 2.0) - math.pow(math.sin(10.0), 2.0))), -1.0)
function code(x)
	t_0 = Float64(cos(10.0) * x)
	return Float64(Float64(t_0 - sin(10.0)) / Float64((t_0 ^ 2.0) - (sin(10.0) ^ 2.0))) ^ -1.0
end
function tmp = code(x)
	t_0 = cos(10.0) * x;
	tmp = ((t_0 - sin(10.0)) / ((t_0 ^ 2.0) - (sin(10.0) ^ 2.0))) ^ -1.0;
end
code[x_] := Block[{t$95$0 = N[(N[Cos[10.0], $MachinePrecision] * x), $MachinePrecision]}, N[Power[N[(N[(t$95$0 - N[Sin[10.0], $MachinePrecision]), $MachinePrecision] / N[(N[Power[t$95$0, 2.0], $MachinePrecision] - N[Power[N[Sin[10.0], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos 10 \cdot x\\
{\left(\frac{t\_0 - \sin 10}{{t\_0}^{2} - {\sin 10}^{2}}\right)}^{-1}
\end{array}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\sin 10 + x \cdot \cos 10} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto \color{blue}{x \cdot \cos 10 + \sin 10} \]
    2. *-commutativeN/A

      \[\leadsto \color{blue}{\cos 10 \cdot x} + \sin 10 \]
    3. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\cos 10, x, \sin 10\right)} \]
    4. lower-cos.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\cos 10}, x, \sin 10\right) \]
    5. lower-sin.f64100.0

      \[\leadsto \mathsf{fma}\left(\cos 10, x, \color{blue}{\sin 10}\right) \]
  5. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos 10, x, \sin 10\right)} \]
  6. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \frac{1}{\color{blue}{\frac{\cos 10 \cdot x - \sin 10}{{\left(\cos 10 \cdot x\right)}^{2} - {\sin 10}^{2}}}} \]
    2. Final simplification100.0%

      \[\leadsto {\left(\frac{\cos 10 \cdot x - \sin 10}{{\left(\cos 10 \cdot x\right)}^{2} - {\sin 10}^{2}}\right)}^{-1} \]
    3. Add Preprocessing

    Alternative 2: 100.0% accurate, 1.0× speedup?

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

      \[\left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right) \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \color{blue}{\left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right)} \]
      2. +-commutativeN/A

        \[\leadsto \color{blue}{\sin \left(x + 10\right) + \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x}} \]
      3. lift-sin.f64N/A

        \[\leadsto \color{blue}{\sin \left(x + 10\right)} + \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} \]
      4. lift-+.f64N/A

        \[\leadsto \sin \color{blue}{\left(x + 10\right)} + \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} \]
      5. sin-sumN/A

        \[\leadsto \color{blue}{\left(\sin x \cdot \cos 10 + \cos x \cdot \sin 10\right)} + \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} \]
      6. associate-+l+N/A

        \[\leadsto \color{blue}{\sin x \cdot \cos 10 + \left(\cos x \cdot \sin 10 + \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x}\right)} \]
      7. sin-cos-multN/A

        \[\leadsto \color{blue}{\frac{\sin \left(x - 10\right) + \sin \left(x + 10\right)}{2}} + \left(\cos x \cdot \sin 10 + \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x}\right) \]
      8. div-invN/A

        \[\leadsto \color{blue}{\left(\sin \left(x - 10\right) + \sin \left(x + 10\right)\right) \cdot \frac{1}{2}} + \left(\cos x \cdot \sin 10 + \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x}\right) \]
      9. metadata-evalN/A

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sin \left(x - 10\right) + \sin \left(x + 10\right), \frac{1}{2}, \cos x \cdot \sin 10 + \left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x}\right)} \]
    4. Applied rewrites100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\sin \left(x - 10\right) + \sin \left(10 + x\right), 0.5, \mathsf{fma}\left(e^{x} \cdot \sin x, 1 - \cos x, \sin 10 \cdot \cos x\right)\right)} \]
    5. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\sin 10 + \left(\frac{1}{2} \cdot \left(x \cdot \left(\cos -10 + \cos 10\right)\right) + \frac{1}{2} \cdot \left(\sin -10 + \sin 10\right)\right)} \]
    6. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\left(\frac{1}{2} \cdot \left(x \cdot \left(\cos -10 + \cos 10\right)\right) + \frac{1}{2} \cdot \left(\sin -10 + \sin 10\right)\right) + \sin 10} \]
      2. distribute-lft-inN/A

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

        \[\leadsto \color{blue}{\left(\left(\frac{1}{2} \cdot \left(x \cdot \left(\cos -10 + \cos 10\right)\right) + \frac{1}{2} \cdot \sin -10\right) + \frac{1}{2} \cdot \sin 10\right)} + \sin 10 \]
      4. *-commutativeN/A

        \[\leadsto \left(\left(\frac{1}{2} \cdot \left(x \cdot \left(\cos -10 + \cos 10\right)\right) + \color{blue}{\sin -10 \cdot \frac{1}{2}}\right) + \frac{1}{2} \cdot \sin 10\right) + \sin 10 \]
      5. associate-+l+N/A

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

        \[\leadsto \left(\frac{1}{2} \cdot \left(x \cdot \left(\cos -10 + \cos 10\right)\right) + \color{blue}{\frac{1}{2} \cdot \sin -10}\right) + \left(\frac{1}{2} \cdot \sin 10 + \sin 10\right) \]
      7. distribute-lft-outN/A

        \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(x \cdot \left(\cos -10 + \cos 10\right) + \sin -10\right)} + \left(\frac{1}{2} \cdot \sin 10 + \sin 10\right) \]
      8. +-commutativeN/A

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1}{2}, \sin -10 + x \cdot \left(\cos -10 + \cos 10\right), \frac{1}{2} \cdot \sin 10 + \sin 10\right)} \]
    7. Applied rewrites100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, \mathsf{fma}\left(\cos -10 + \cos 10, x, \sin -10\right), 1.5 \cdot \sin 10\right)} \]
    8. Add Preprocessing

    Alternative 3: 100.0% accurate, 2.0× speedup?

    \[\begin{array}{l} \\ \mathsf{fma}\left(\cos 10, x, \sin 10\right) \end{array} \]
    (FPCore (x) :precision binary64 (fma (cos 10.0) x (sin 10.0)))
    double code(double x) {
    	return fma(cos(10.0), x, sin(10.0));
    }
    
    function code(x)
    	return fma(cos(10.0), x, sin(10.0))
    end
    
    code[x_] := N[(N[Cos[10.0], $MachinePrecision] * x + N[Sin[10.0], $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \mathsf{fma}\left(\cos 10, x, \sin 10\right)
    \end{array}
    
    Derivation
    1. Initial program 100.0%

      \[\left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\sin 10 + x \cdot \cos 10} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{x \cdot \cos 10 + \sin 10} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\cos 10 \cdot x} + \sin 10 \]
      3. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\cos 10, x, \sin 10\right)} \]
      4. lower-cos.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\cos 10}, x, \sin 10\right) \]
      5. lower-sin.f64100.0

        \[\leadsto \mathsf{fma}\left(\cos 10, x, \color{blue}{\sin 10}\right) \]
    5. Applied rewrites100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\cos 10, x, \sin 10\right)} \]
    6. Add Preprocessing

    Alternative 4: 99.9% accurate, 3.4× speedup?

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

      \[\left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

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

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

        \[\leadsto \color{blue}{{x}^{3} \cdot \frac{1}{2}} + \sin \left(x + 10\right) \]
      3. lower-pow.f64100.0

        \[\leadsto \color{blue}{{x}^{3}} \cdot 0.5 + \sin \left(x + 10\right) \]
    5. Applied rewrites100.0%

      \[\leadsto \color{blue}{{x}^{3} \cdot 0.5} + \sin \left(x + 10\right) \]
    6. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto \left(\left(x \cdot x\right) \cdot x\right) \cdot 0.5 + \sin \left(x + 10\right) \]
      2. Add Preprocessing

      Alternative 5: 99.6% accurate, 4.2× speedup?

      \[\begin{array}{l} \\ \sin 10 \end{array} \]
      (FPCore (x) :precision binary64 (sin 10.0))
      double code(double x) {
      	return sin(10.0);
      }
      
      real(8) function code(x)
          real(8), intent (in) :: x
          code = sin(10.0d0)
      end function
      
      public static double code(double x) {
      	return Math.sin(10.0);
      }
      
      def code(x):
      	return math.sin(10.0)
      
      function code(x)
      	return sin(10.0)
      end
      
      function tmp = code(x)
      	tmp = sin(10.0);
      end
      
      code[x_] := N[Sin[10.0], $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \sin 10
      \end{array}
      
      Derivation
      1. Initial program 100.0%

        \[\left(\left(1 - \cos x\right) \cdot \sin x\right) \cdot e^{x} + \sin \left(x + 10\right) \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{\sin 10} \]
      4. Step-by-step derivation
        1. lower-sin.f6499.6

          \[\leadsto \color{blue}{\sin 10} \]
      5. Applied rewrites99.6%

        \[\leadsto \color{blue}{\sin 10} \]
      6. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 1 
      (FPCore (x)
        :name "(1-cos(x))* sin(x)*exp(x) + sin(x+10)"
        :precision binary64
        :pre (and (<= -1e-9 x) (<= x 0.0))
        (+ (* (* (- 1.0 (cos x)) (sin x)) (exp x)) (sin (+ x 10.0))))