log(x - y)

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

Specification

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

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

\\
\log \left(x - y\right)
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
\log \left(x - y\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\log \left(x - y\right) \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 84.0% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.3 \cdot 10^{-216}:\\
\;\;\;\;\log \left(-y\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -2.29999999999999997e-216

    1. Initial program 100.0%

      \[\log \left(x - y\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \log \color{blue}{\left(-1 \cdot y\right)} \]
    4. Step-by-step derivation
      1. neg-mul-1N/A

        \[\leadsto \log \color{blue}{\left(\mathsf{neg}\left(y\right)\right)} \]
      2. lower-neg.f6483.1

        \[\leadsto \log \color{blue}{\left(-y\right)} \]
    5. Applied rewrites83.1%

      \[\leadsto \log \color{blue}{\left(-y\right)} \]

    if -2.29999999999999997e-216 < y

    1. Initial program 100.0%

      \[\log \left(x - y\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{-1 \cdot \log \left(\frac{1}{x}\right)} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(\log \left(\frac{1}{x}\right)\right)} \]
      2. log-recN/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\log x\right)\right)}\right) \]
      3. remove-double-negN/A

        \[\leadsto \color{blue}{\log x} \]
      4. lower-log.f6489.7

        \[\leadsto \color{blue}{\log x} \]
    5. Applied rewrites89.7%

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

Alternative 3: 52.9% accurate, 1.0× speedup?

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

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

    \[\log \left(x - y\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{-1 \cdot \log \left(\frac{1}{x}\right)} \]
  4. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto \color{blue}{\mathsf{neg}\left(\log \left(\frac{1}{x}\right)\right)} \]
    2. log-recN/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\log x\right)\right)}\right) \]
    3. remove-double-negN/A

      \[\leadsto \color{blue}{\log x} \]
    4. lower-log.f6450.7

      \[\leadsto \color{blue}{\log x} \]
  5. Applied rewrites50.7%

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

Reproduce

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