sin(x+1e-6)-sin(x)

Percentage Accurate: 60.4% → 89.2%
Time: 3.8s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[1.56 \leq x \land x \leq 1.58\]
\[\begin{array}{l} \\ \sin \left(x + 10^{-6}\right) - \sin x \end{array} \]
(FPCore (x) :precision binary64 (- (sin (+ x 1e-6)) (sin x)))
double code(double x) {
	return sin((x + 1e-6)) - sin(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = sin((x + 1d-6)) - sin(x)
end function
public static double code(double x) {
	return Math.sin((x + 1e-6)) - Math.sin(x);
}
def code(x):
	return math.sin((x + 1e-6)) - math.sin(x)
function code(x)
	return Float64(sin(Float64(x + 1e-6)) - sin(x))
end
function tmp = code(x)
	tmp = sin((x + 1e-6)) - sin(x);
end
code[x_] := N[(N[Sin[N[(x + 1e-6), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\sin \left(x + 10^{-6}\right) - \sin 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: 60.4% accurate, 1.0× speedup?

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

\\
\sin \left(x + 10^{-6}\right) - \sin x
\end{array}

Alternative 1: 89.2% accurate, 1.0× speedup?

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

\\
\left(\sin \left( 5 \cdot 10^{-7} \right) \cdot 2\right) \cdot \cos \left(-5 \cdot 10^{-7} - x\right)
\end{array}
Derivation
  1. Initial program 60.2%

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

      \[\leadsto \color{blue}{\sin \left(x + \frac{4722366482869645}{4722366482869645213696}\right) - \sin x} \]
    2. lift-sin.f64N/A

      \[\leadsto \color{blue}{\sin \left(x + \frac{4722366482869645}{4722366482869645213696}\right)} - \sin x \]
    3. lift-sin.f64N/A

      \[\leadsto \sin \left(x + \frac{4722366482869645}{4722366482869645213696}\right) - \color{blue}{\sin x} \]
    4. diff-sinN/A

      \[\leadsto \color{blue}{2 \cdot \left(\sin \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) - x}{2}\right) \cdot \cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{2}\right)\right)} \]
    5. associate-*r*N/A

      \[\leadsto \color{blue}{\left(2 \cdot \sin \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) - x}{2}\right)\right) \cdot \cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{2}\right)} \]
    6. lower-*.f64N/A

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

      \[\leadsto \color{blue}{\left(\sin \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) - x}{2}\right) \cdot 2\right)} \cdot \cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{2}\right) \]
    8. lower-*.f64N/A

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

      \[\leadsto \left(\color{blue}{\sin \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) - x}{2}\right)} \cdot 2\right) \cdot \cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{2}\right) \]
    10. lift-+.f64N/A

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

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

      \[\leadsto \left(\sin \left(\frac{\color{blue}{\frac{4722366482869645}{4722366482869645213696} + \left(x - x\right)}}{2}\right) \cdot 2\right) \cdot \cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{2}\right) \]
    13. +-inversesN/A

      \[\leadsto \left(\sin \left(\frac{\frac{4722366482869645}{4722366482869645213696} + \color{blue}{0}}{2}\right) \cdot 2\right) \cdot \cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{2}\right) \]
    14. metadata-evalN/A

      \[\leadsto \left(\sin \left(\frac{\color{blue}{\frac{4722366482869645}{4722366482869645213696}}}{2}\right) \cdot 2\right) \cdot \cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{2}\right) \]
    15. metadata-evalN/A

      \[\leadsto \left(\sin \color{blue}{\frac{4722366482869645}{9444732965739290427392}} \cdot 2\right) \cdot \cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{2}\right) \]
    16. frac-2negN/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \color{blue}{\left(\frac{\mathsf{neg}\left(\left(\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x\right)\right)}{\mathsf{neg}\left(2\right)}\right)} \]
    17. distribute-frac-negN/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{\mathsf{neg}\left(2\right)}\right)\right)} \]
    18. cos-negN/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \color{blue}{\cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{\mathsf{neg}\left(2\right)}\right)} \]
    19. lower-cos.f64N/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \color{blue}{\cos \left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{\mathsf{neg}\left(2\right)}\right)} \]
    20. lower-/.f64N/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \color{blue}{\left(\frac{\left(x + \frac{4722366482869645}{4722366482869645213696}\right) + x}{\mathsf{neg}\left(2\right)}\right)} \]
  4. Applied rewrites89.1%

    \[\leadsto \color{blue}{\left(\sin \left( 5 \cdot 10^{-7} \right) \cdot 2\right) \cdot \cos \left(\frac{\mathsf{fma}\left(2, x, 10^{-6}\right)}{-2}\right)} \]
  5. Taylor expanded in x around 0

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

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \left(-1 \cdot x - \color{blue}{\frac{4722366482869645}{9444732965739290427392} \cdot 1}\right) \]
    2. lft-mult-inverseN/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \left(-1 \cdot x - \frac{4722366482869645}{9444732965739290427392} \cdot \color{blue}{\left(\frac{1}{x} \cdot x\right)}\right) \]
    3. associate-*l*N/A

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

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \left(-1 \cdot x - \color{blue}{x \cdot \left(\frac{4722366482869645}{9444732965739290427392} \cdot \frac{1}{x}\right)}\right) \]
    5. unsub-negN/A

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

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \color{blue}{\left(\left(\mathsf{neg}\left(x \cdot \left(\frac{4722366482869645}{9444732965739290427392} \cdot \frac{1}{x}\right)\right)\right) + -1 \cdot x\right)} \]
    7. mul-1-negN/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \left(\left(\mathsf{neg}\left(x \cdot \left(\frac{4722366482869645}{9444732965739290427392} \cdot \frac{1}{x}\right)\right)\right) + \color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right) \]
    8. sub-negN/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \color{blue}{\left(\left(\mathsf{neg}\left(x \cdot \left(\frac{4722366482869645}{9444732965739290427392} \cdot \frac{1}{x}\right)\right)\right) - x\right)} \]
    9. lower--.f64N/A

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

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{\left(\frac{4722366482869645}{9444732965739290427392} \cdot \frac{1}{x}\right) \cdot x}\right)\right) - x\right) \]
    11. associate-*l*N/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{\frac{4722366482869645}{9444732965739290427392} \cdot \left(\frac{1}{x} \cdot x\right)}\right)\right) - x\right) \]
    12. lft-mult-inverseN/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \left(\left(\mathsf{neg}\left(\frac{4722366482869645}{9444732965739290427392} \cdot \color{blue}{1}\right)\right) - x\right) \]
    13. metadata-evalN/A

      \[\leadsto \left(\sin \frac{4722366482869645}{9444732965739290427392} \cdot 2\right) \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{\frac{4722366482869645}{9444732965739290427392}}\right)\right) - x\right) \]
    14. metadata-eval89.1

      \[\leadsto \left(\sin \left( 5 \cdot 10^{-7} \right) \cdot 2\right) \cdot \cos \left(\color{blue}{-5 \cdot 10^{-7}} - x\right) \]
  7. Applied rewrites89.1%

    \[\leadsto \left(\sin \left( 5 \cdot 10^{-7} \right) \cdot 2\right) \cdot \cos \color{blue}{\left(-5 \cdot 10^{-7} - x\right)} \]
  8. Add Preprocessing

Alternative 2: 12.7% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\sin \left(x + 10^{-6}\right) - \sin x \leq -2 \cdot 10^{-10}:\\ \;\;\;\;\sin \left( 10^{-6} \right) - \sin x\\ \mathbf{else}:\\ \;\;\;\;\sin \left( 10^{-6} \right)\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= (- (sin (+ x 1e-6)) (sin x)) -2e-10)
   (- (sin 1e-6) (sin x))
   (sin 1e-6)))
double code(double x) {
	double tmp;
	if ((sin((x + 1e-6)) - sin(x)) <= -2e-10) {
		tmp = sin(1e-6) - sin(x);
	} else {
		tmp = sin(1e-6);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((sin((x + 1d-6)) - sin(x)) <= (-2d-10)) then
        tmp = sin(1d-6) - sin(x)
    else
        tmp = sin(1d-6)
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((Math.sin((x + 1e-6)) - Math.sin(x)) <= -2e-10) {
		tmp = Math.sin(1e-6) - Math.sin(x);
	} else {
		tmp = Math.sin(1e-6);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (math.sin((x + 1e-6)) - math.sin(x)) <= -2e-10:
		tmp = math.sin(1e-6) - math.sin(x)
	else:
		tmp = math.sin(1e-6)
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(sin(Float64(x + 1e-6)) - sin(x)) <= -2e-10)
		tmp = Float64(sin(1e-6) - sin(x));
	else
		tmp = sin(1e-6);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((sin((x + 1e-6)) - sin(x)) <= -2e-10)
		tmp = sin(1e-6) - sin(x);
	else
		tmp = sin(1e-6);
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(N[Sin[N[(x + 1e-6), $MachinePrecision]], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], -2e-10], N[(N[Sin[1e-6], $MachinePrecision] - N[Sin[x], $MachinePrecision]), $MachinePrecision], N[Sin[1e-6], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\sin \left(x + 10^{-6}\right) - \sin x \leq -2 \cdot 10^{-10}:\\
\;\;\;\;\sin \left( 10^{-6} \right) - \sin x\\

\mathbf{else}:\\
\;\;\;\;\sin \left( 10^{-6} \right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (sin.f64 (+.f64 x #s(literal 4722366482869645/4722366482869645213696 binary64))) (sin.f64 x)) < -2.00000000000000007e-10

    1. Initial program 60.2%

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

      \[\leadsto \sin \color{blue}{\frac{4722366482869645}{4722366482869645213696}} - \sin x \]
    4. Step-by-step derivation
      1. Applied rewrites11.2%

        \[\leadsto \sin \color{blue}{\left( 10^{-6} \right)} - \sin x \]

      if -2.00000000000000007e-10 < (-.f64 (sin.f64 (+.f64 x #s(literal 4722366482869645/4722366482869645213696 binary64))) (sin.f64 x))

      1. Initial program 60.3%

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

        \[\leadsto \color{blue}{\sin \frac{4722366482869645}{4722366482869645213696}} \]
      4. Step-by-step derivation
        1. lower-sin.f6414.0

          \[\leadsto \color{blue}{\sin \left( 10^{-6} \right)} \]
      5. Applied rewrites14.0%

        \[\leadsto \color{blue}{\sin \left( 10^{-6} \right)} \]
    5. Recombined 2 regimes into one program.
    6. Add Preprocessing

    Alternative 3: 60.4% accurate, 1.0× speedup?

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

      \[\sin \left(x + 10^{-6}\right) - \sin x \]
    2. Add Preprocessing
    3. Add Preprocessing

    Alternative 4: 8.4% accurate, 2.0× speedup?

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

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

      \[\leadsto \color{blue}{\sin \frac{4722366482869645}{4722366482869645213696}} \]
    4. Step-by-step derivation
      1. lower-sin.f648.5

        \[\leadsto \color{blue}{\sin \left( 10^{-6} \right)} \]
    5. Applied rewrites8.5%

      \[\leadsto \color{blue}{\sin \left( 10^{-6} \right)} \]
    6. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 1 
    (FPCore (x)
      :name "sin(x+1e-6)-sin(x)"
      :precision binary64
      :pre (and (<= 1.56 x) (<= x 1.58))
      (- (sin (+ x 1e-6)) (sin x)))