abs(x)-x

Percentage Accurate: 100.0% → 100.0%
Time: 2.1s
Alternatives: 3
Speedup: 1.0×

Specification

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

\\
\left|x\right| - 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 3 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} \\ \left|x\right| - x \end{array} \]
(FPCore (x) :precision binary64 (- (fabs x) x))
double code(double x) {
	return fabs(x) - x;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = abs(x) - x
end function
public static double code(double x) {
	return Math.abs(x) - x;
}
def code(x):
	return math.fabs(x) - x
function code(x)
	return Float64(abs(x) - x)
end
function tmp = code(x)
	tmp = abs(x) - x;
end
code[x_] := N[(N[Abs[x], $MachinePrecision] - x), $MachinePrecision]
\begin{array}{l}

\\
\left|x\right| - x
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
\left|x\right| - x
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left|x\right| - x \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 59.5% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\left|x\right| - x \leq 0:\\ \;\;\;\;0\\ \mathbf{else}:\\ \;\;\;\;-x\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= (- (fabs x) x) 0.0) 0.0 (- x)))
double code(double x) {
	double tmp;
	if ((fabs(x) - x) <= 0.0) {
		tmp = 0.0;
	} else {
		tmp = -x;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if ((abs(x) - x) <= 0.0d0) then
        tmp = 0.0d0
    else
        tmp = -x
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if ((Math.abs(x) - x) <= 0.0) {
		tmp = 0.0;
	} else {
		tmp = -x;
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (math.fabs(x) - x) <= 0.0:
		tmp = 0.0
	else:
		tmp = -x
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(abs(x) - x) <= 0.0)
		tmp = 0.0;
	else
		tmp = Float64(-x);
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((abs(x) - x) <= 0.0)
		tmp = 0.0;
	else
		tmp = -x;
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(N[Abs[x], $MachinePrecision] - x), $MachinePrecision], 0.0], 0.0, (-x)]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\left|x\right| - x \leq 0:\\
\;\;\;\;0\\

\mathbf{else}:\\
\;\;\;\;-x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (fabs.f64 x) x) < 0.0

    1. Initial program 100.0%

      \[\left|x\right| - x \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift--.f64N/A

        \[\leadsto \color{blue}{\left|x\right| - x} \]
      2. flip--N/A

        \[\leadsto \color{blue}{\frac{\left|x\right| \cdot \left|x\right| - x \cdot x}{\left|x\right| + x}} \]
      3. lift-fabs.f64N/A

        \[\leadsto \frac{\color{blue}{\left|x\right|} \cdot \left|x\right| - x \cdot x}{\left|x\right| + x} \]
      4. lift-fabs.f64N/A

        \[\leadsto \frac{\left|x\right| \cdot \color{blue}{\left|x\right|} - x \cdot x}{\left|x\right| + x} \]
      5. sqr-absN/A

        \[\leadsto \frac{\color{blue}{x \cdot x} - x \cdot x}{\left|x\right| + x} \]
      6. div-subN/A

        \[\leadsto \color{blue}{\frac{x \cdot x}{\left|x\right| + x} - \frac{x \cdot x}{\left|x\right| + x}} \]
      7. +-inverses100.0

        \[\leadsto \color{blue}{0} \]
    4. Applied rewrites100.0%

      \[\leadsto \color{blue}{0} \]

    if 0.0 < (-.f64 (fabs.f64 x) x)

    1. Initial program 100.0%

      \[\left|x\right| - 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.f6418.8

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

      \[\leadsto \color{blue}{-x} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 52.8% accurate, 6.0× speedup?

\[\begin{array}{l} \\ 0 \end{array} \]
(FPCore (x) :precision binary64 0.0)
double code(double x) {
	return 0.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 0.0d0
end function
public static double code(double x) {
	return 0.0;
}
def code(x):
	return 0.0
function code(x)
	return 0.0
end
function tmp = code(x)
	tmp = 0.0;
end
code[x_] := 0.0
\begin{array}{l}

\\
0
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left|x\right| - x \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{\left|x\right| - x} \]
    2. flip--N/A

      \[\leadsto \color{blue}{\frac{\left|x\right| \cdot \left|x\right| - x \cdot x}{\left|x\right| + x}} \]
    3. lift-fabs.f64N/A

      \[\leadsto \frac{\color{blue}{\left|x\right|} \cdot \left|x\right| - x \cdot x}{\left|x\right| + x} \]
    4. lift-fabs.f64N/A

      \[\leadsto \frac{\left|x\right| \cdot \color{blue}{\left|x\right|} - x \cdot x}{\left|x\right| + x} \]
    5. sqr-absN/A

      \[\leadsto \frac{\color{blue}{x \cdot x} - x \cdot x}{\left|x\right| + x} \]
    6. div-subN/A

      \[\leadsto \color{blue}{\frac{x \cdot x}{\left|x\right| + x} - \frac{x \cdot x}{\left|x\right| + x}} \]
    7. +-inverses53.2

      \[\leadsto \color{blue}{0} \]
  4. Applied rewrites53.2%

    \[\leadsto \color{blue}{0} \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 1 
(FPCore (x)
  :name "abs(x)-x"
  :precision binary64
  :pre (and (<= -1000.0 x) (<= x 1000.0))
  (- (fabs x) x))