x-0.00001x

Percentage Accurate: 100.0% → 100.0%
Time: 1.6s
Alternatives: 2
Speedup: 1.3×

Specification

?
\[-1 \leq x \land x \leq 1\]
\[\begin{array}{l} \\ x - 10^{-5} \cdot x \end{array} \]
(FPCore (x) :precision binary64 (- x (* 1e-5 x)))
double code(double x) {
	return x - (1e-5 * x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - (1d-5 * x)
end function
public static double code(double x) {
	return x - (1e-5 * x);
}
def code(x):
	return x - (1e-5 * x)
function code(x)
	return Float64(x - Float64(1e-5 * x))
end
function tmp = code(x)
	tmp = x - (1e-5 * x);
end
code[x_] := N[(x - N[(1e-5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - 10^{-5} \cdot 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 2 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x - 10^{-5} \cdot x \end{array} \]
(FPCore (x) :precision binary64 (- x (* 1e-5 x)))
double code(double x) {
	return x - (1e-5 * x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x - (1d-5 * x)
end function
public static double code(double x) {
	return x - (1e-5 * x);
}
def code(x):
	return x - (1e-5 * x)
function code(x)
	return Float64(x - Float64(1e-5 * x))
end
function tmp = code(x)
	tmp = x - (1e-5 * x);
end
code[x_] := N[(x - N[(1e-5 * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - 10^{-5} \cdot x
\end{array}

Alternative 1: 100.0% accurate, 1.3× speedup?

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

\\
\mathsf{fma}\left(x, -1 \cdot 10^{-5}, x\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - 10^{-5} \cdot x \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{x - \frac{5902958103587057}{590295810358705651712} \cdot x} \]
    2. sub-negN/A

      \[\leadsto \color{blue}{x + \left(\mathsf{neg}\left(\frac{5902958103587057}{590295810358705651712} \cdot x\right)\right)} \]
    3. +-commutativeN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(\frac{5902958103587057}{590295810358705651712} \cdot x\right)\right) + x} \]
    4. lift-*.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\frac{5902958103587057}{590295810358705651712} \cdot x}\right)\right) + x \]
    5. *-commutativeN/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{x \cdot \frac{5902958103587057}{590295810358705651712}}\right)\right) + x \]
    6. distribute-rgt-neg-inN/A

      \[\leadsto \color{blue}{x \cdot \left(\mathsf{neg}\left(\frac{5902958103587057}{590295810358705651712}\right)\right)} + x \]
    7. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{neg}\left(\frac{5902958103587057}{590295810358705651712}\right), x\right)} \]
    8. metadata-eval100.0

      \[\leadsto \mathsf{fma}\left(x, \color{blue}{-1 \cdot 10^{-5}}, x\right) \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(x, -1 \cdot 10^{-5}, x\right)} \]
  5. Add Preprocessing

Alternative 2: 99.5% accurate, 1.5× speedup?

\[\begin{array}{l} \\ x \cdot 0.99999 \end{array} \]
(FPCore (x) :precision binary64 (* x 0.99999))
double code(double x) {
	return x * 0.99999;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x * 0.99999d0
end function
public static double code(double x) {
	return x * 0.99999;
}
def code(x):
	return x * 0.99999
function code(x)
	return Float64(x * 0.99999)
end
function tmp = code(x)
	tmp = x * 0.99999;
end
code[x_] := N[(x * 0.99999), $MachinePrecision]
\begin{array}{l}

\\
x \cdot 0.99999
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - 10^{-5} \cdot x \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{x - \frac{5902958103587057}{590295810358705651712} \cdot x} \]
    2. lift-*.f64N/A

      \[\leadsto x - \color{blue}{\frac{5902958103587057}{590295810358705651712} \cdot x} \]
    3. cancel-sign-sub-invN/A

      \[\leadsto \color{blue}{x + \left(\mathsf{neg}\left(\frac{5902958103587057}{590295810358705651712}\right)\right) \cdot x} \]
    4. distribute-rgt1-inN/A

      \[\leadsto \color{blue}{\left(\left(\mathsf{neg}\left(\frac{5902958103587057}{590295810358705651712}\right)\right) + 1\right) \cdot x} \]
    5. *-commutativeN/A

      \[\leadsto \color{blue}{x \cdot \left(\left(\mathsf{neg}\left(\frac{5902958103587057}{590295810358705651712}\right)\right) + 1\right)} \]
    6. lower-*.f64N/A

      \[\leadsto \color{blue}{x \cdot \left(\left(\mathsf{neg}\left(\frac{5902958103587057}{590295810358705651712}\right)\right) + 1\right)} \]
    7. metadata-evalN/A

      \[\leadsto x \cdot \left(\color{blue}{\frac{-5902958103587057}{590295810358705651712}} + 1\right) \]
    8. metadata-eval99.6

      \[\leadsto x \cdot \color{blue}{0.99999} \]
  4. Applied rewrites99.6%

    \[\leadsto \color{blue}{x \cdot 0.99999} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 1 
(FPCore (x)
  :name "x-0.00001x"
  :precision binary64
  :pre (and (<= -1.0 x) (<= x 1.0))
  (- x (* 1e-5 x)))