bug500 (missed optimization)

Percentage Accurate: 69.1% → 98.8%
Time: 4.8s
Alternatives: 4
Speedup: 6.5×

Specification

?
\[-1000 < x \land x < 1000\]
\[\begin{array}{l} \\ \sin x - x \end{array} \]
(FPCore (x) :precision binary64 (- (sin x) x))
double code(double x) {
	return sin(x) - x;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = sin(x) - x
end function
public static double code(double x) {
	return Math.sin(x) - x;
}
def code(x):
	return math.sin(x) - x
function code(x)
	return Float64(sin(x) - x)
end
function tmp = code(x)
	tmp = sin(x) - x;
end
code[x_] := N[(N[Sin[x], $MachinePrecision] - x), $MachinePrecision]
\begin{array}{l}

\\
\sin x - 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 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: 69.1% accurate, 1.0× speedup?

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

\\
\sin x - x
\end{array}

Alternative 1: 98.8% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \left(\left(\left(x \cdot x\right) \cdot \mathsf{fma}\left(\left(\left(x \cdot x\right) \cdot 2.7557319223985893 \cdot 10^{-6} - 0.0001984126984126984\right) \cdot x, x, 0.008333333333333333\right) - 0.16666666666666666\right) \cdot \left(x \cdot x\right)\right) \cdot x \end{array} \]
(FPCore (x)
 :precision binary64
 (*
  (*
   (-
    (*
     (* x x)
     (fma
      (* (- (* (* x x) 2.7557319223985893e-6) 0.0001984126984126984) x)
      x
      0.008333333333333333))
    0.16666666666666666)
   (* x x))
  x))
double code(double x) {
	return ((((x * x) * fma(((((x * x) * 2.7557319223985893e-6) - 0.0001984126984126984) * x), x, 0.008333333333333333)) - 0.16666666666666666) * (x * x)) * x;
}
function code(x)
	return Float64(Float64(Float64(Float64(Float64(x * x) * fma(Float64(Float64(Float64(Float64(x * x) * 2.7557319223985893e-6) - 0.0001984126984126984) * x), x, 0.008333333333333333)) - 0.16666666666666666) * Float64(x * x)) * x)
end
code[x_] := N[(N[(N[(N[(N[(x * x), $MachinePrecision] * N[(N[(N[(N[(N[(x * x), $MachinePrecision] * 2.7557319223985893e-6), $MachinePrecision] - 0.0001984126984126984), $MachinePrecision] * x), $MachinePrecision] * x + 0.008333333333333333), $MachinePrecision]), $MachinePrecision] - 0.16666666666666666), $MachinePrecision] * N[(x * x), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(\left(x \cdot x\right) \cdot \mathsf{fma}\left(\left(\left(x \cdot x\right) \cdot 2.7557319223985893 \cdot 10^{-6} - 0.0001984126984126984\right) \cdot x, x, 0.008333333333333333\right) - 0.16666666666666666\right) \cdot \left(x \cdot x\right)\right) \cdot x
\end{array}
Derivation
  1. Initial program 70.6%

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

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

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

      \[\leadsto \color{blue}{\left({x}^{2} \cdot \left(\frac{1}{120} + {x}^{2} \cdot \left(\frac{1}{362880} \cdot {x}^{2} - \frac{1}{5040}\right)\right) - \frac{1}{6}\right) \cdot {x}^{3}} \]
  5. Applied rewrites99.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{4}, 2.7557319223985893 \cdot 10^{-6} \cdot \left(x \cdot x\right) - 0.0001984126984126984, \left(x \cdot x\right) \cdot 0.008333333333333333 - 0.16666666666666666\right) \cdot {x}^{3}} \]
  6. Step-by-step derivation
    1. Applied rewrites99.3%

      \[\leadsto \left(\mathsf{fma}\left(\left(x \cdot x\right) \cdot 2.7557319223985893 \cdot 10^{-6} - 0.0001984126984126984, {x}^{4}, 0.008333333333333333 \cdot \left(x \cdot x\right) - 0.16666666666666666\right) \cdot \left(x \cdot x\right)\right) \cdot \color{blue}{x} \]
    2. Step-by-step derivation
      1. Applied rewrites99.3%

        \[\leadsto \left(\mathsf{fma}\left(\left(2.7557319223985893 \cdot 10^{-6} \cdot \left(x \cdot x\right) - 0.0001984126984126984\right) \cdot \left(x \cdot x\right), x \cdot x, 0.008333333333333333 \cdot \left(x \cdot x\right) - 0.16666666666666666\right) \cdot \left(x \cdot x\right)\right) \cdot x \]
      2. Step-by-step derivation
        1. Applied rewrites99.3%

          \[\leadsto \left(\left(\left(x \cdot x\right) \cdot \mathsf{fma}\left(\left(\left(x \cdot x\right) \cdot 2.7557319223985893 \cdot 10^{-6} - 0.0001984126984126984\right) \cdot x, x, 0.008333333333333333\right) - 0.16666666666666666\right) \cdot \left(x \cdot x\right)\right) \cdot x \]
        2. Add Preprocessing

        Alternative 2: 98.6% accurate, 3.9× speedup?

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

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

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

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

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

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

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

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

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

            \[\leadsto \left(\color{blue}{\left(x \cdot x\right)} \cdot \frac{1}{120} - \frac{1}{6}\right) \cdot {x}^{3} \]
          8. lower-pow.f6499.0

            \[\leadsto \left(\left(x \cdot x\right) \cdot 0.008333333333333333 - 0.16666666666666666\right) \cdot \color{blue}{{x}^{3}} \]
        5. Applied rewrites99.0%

          \[\leadsto \color{blue}{\left(\left(x \cdot x\right) \cdot 0.008333333333333333 - 0.16666666666666666\right) \cdot {x}^{3}} \]
        6. Step-by-step derivation
          1. Applied rewrites99.0%

            \[\leadsto \left(\left(x \cdot x\right) \cdot 0.008333333333333333 - 0.16666666666666666\right) \cdot \left(\left(x \cdot x\right) \cdot \color{blue}{x}\right) \]
          2. Taylor expanded in x around inf

            \[\leadsto \left({x}^{2} \cdot \left(\frac{1}{120} - \frac{1}{6} \cdot \frac{1}{{x}^{2}}\right)\right) \cdot \left(\color{blue}{\left(x \cdot x\right)} \cdot x\right) \]
          3. Step-by-step derivation
            1. Applied rewrites99.0%

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

            Alternative 3: 98.2% accurate, 6.5× speedup?

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

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

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

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

                \[\leadsto \color{blue}{{x}^{3} \cdot \frac{-1}{6}} \]
              3. lower-pow.f6498.4

                \[\leadsto \color{blue}{{x}^{3}} \cdot -0.16666666666666666 \]
            5. Applied rewrites98.4%

              \[\leadsto \color{blue}{{x}^{3} \cdot -0.16666666666666666} \]
            6. Step-by-step derivation
              1. Applied rewrites98.4%

                \[\leadsto \left(-0.16666666666666666 \cdot \left(x \cdot x\right)\right) \cdot \color{blue}{x} \]
              2. Add Preprocessing

              Alternative 4: 6.5% accurate, 34.7× speedup?

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

                \[\sin x - x \]
              2. Add Preprocessing
              3. Taylor expanded in x around inf

                \[\leadsto \color{blue}{-1 \cdot x} \]
              4. Step-by-step derivation
                1. mul-1-negN/A

                  \[\leadsto \color{blue}{\mathsf{neg}\left(x\right)} \]
                2. lower-neg.f646.5

                  \[\leadsto \color{blue}{-x} \]
              5. Applied rewrites6.5%

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

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

              \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|x\right| < 0.07:\\ \;\;\;\;-\left(\left(\frac{{x}^{3}}{6} - \frac{{x}^{5}}{120}\right) + \frac{{x}^{7}}{5040}\right)\\ \mathbf{else}:\\ \;\;\;\;\sin x - x\\ \end{array} \end{array} \]
              (FPCore (x)
               :precision binary64
               (if (< (fabs x) 0.07)
                 (- (+ (- (/ (pow x 3.0) 6.0) (/ (pow x 5.0) 120.0)) (/ (pow x 7.0) 5040.0)))
                 (- (sin x) x)))
              double code(double x) {
              	double tmp;
              	if (fabs(x) < 0.07) {
              		tmp = -(((pow(x, 3.0) / 6.0) - (pow(x, 5.0) / 120.0)) + (pow(x, 7.0) / 5040.0));
              	} else {
              		tmp = sin(x) - x;
              	}
              	return tmp;
              }
              
              real(8) function code(x)
                  real(8), intent (in) :: x
                  real(8) :: tmp
                  if (abs(x) < 0.07d0) then
                      tmp = -((((x ** 3.0d0) / 6.0d0) - ((x ** 5.0d0) / 120.0d0)) + ((x ** 7.0d0) / 5040.0d0))
                  else
                      tmp = sin(x) - x
                  end if
                  code = tmp
              end function
              
              public static double code(double x) {
              	double tmp;
              	if (Math.abs(x) < 0.07) {
              		tmp = -(((Math.pow(x, 3.0) / 6.0) - (Math.pow(x, 5.0) / 120.0)) + (Math.pow(x, 7.0) / 5040.0));
              	} else {
              		tmp = Math.sin(x) - x;
              	}
              	return tmp;
              }
              
              def code(x):
              	tmp = 0
              	if math.fabs(x) < 0.07:
              		tmp = -(((math.pow(x, 3.0) / 6.0) - (math.pow(x, 5.0) / 120.0)) + (math.pow(x, 7.0) / 5040.0))
              	else:
              		tmp = math.sin(x) - x
              	return tmp
              
              function code(x)
              	tmp = 0.0
              	if (abs(x) < 0.07)
              		tmp = Float64(-Float64(Float64(Float64((x ^ 3.0) / 6.0) - Float64((x ^ 5.0) / 120.0)) + Float64((x ^ 7.0) / 5040.0)));
              	else
              		tmp = Float64(sin(x) - x);
              	end
              	return tmp
              end
              
              function tmp_2 = code(x)
              	tmp = 0.0;
              	if (abs(x) < 0.07)
              		tmp = -((((x ^ 3.0) / 6.0) - ((x ^ 5.0) / 120.0)) + ((x ^ 7.0) / 5040.0));
              	else
              		tmp = sin(x) - x;
              	end
              	tmp_2 = tmp;
              end
              
              code[x_] := If[Less[N[Abs[x], $MachinePrecision], 0.07], (-N[(N[(N[(N[Power[x, 3.0], $MachinePrecision] / 6.0), $MachinePrecision] - N[(N[Power[x, 5.0], $MachinePrecision] / 120.0), $MachinePrecision]), $MachinePrecision] + N[(N[Power[x, 7.0], $MachinePrecision] / 5040.0), $MachinePrecision]), $MachinePrecision]), N[(N[Sin[x], $MachinePrecision] - x), $MachinePrecision]]
              
              \begin{array}{l}
              
              \\
              \begin{array}{l}
              \mathbf{if}\;\left|x\right| < 0.07:\\
              \;\;\;\;-\left(\left(\frac{{x}^{3}}{6} - \frac{{x}^{5}}{120}\right) + \frac{{x}^{7}}{5040}\right)\\
              
              \mathbf{else}:\\
              \;\;\;\;\sin x - x\\
              
              
              \end{array}
              \end{array}
              

              Reproduce

              ?
              herbie shell --seed 2024326 
              (FPCore (x)
                :name "bug500 (missed optimization)"
                :precision binary64
                :pre (and (< -1000.0 x) (< x 1000.0))
              
                :alt
                (! :herbie-platform default (if (< (fabs x) 7/100) (- (+ (- (/ (pow x 3) 6) (/ (pow x 5) 120)) (/ (pow x 7) 5040))) (- (sin x) x)))
              
                (- (sin x) x))