log(sqrt(x*x+y*y))

Percentage Accurate: 77.1% → 100.0%
Time: 2.8s
Alternatives: 3
Speedup: 0.6×

Specification

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

\\
\log \left(\sqrt{x \cdot x + y \cdot 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: 77.1% accurate, 1.0× speedup?

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

\\
\log \left(\sqrt{x \cdot x + y \cdot y}\right)
\end{array}

Alternative 1: 100.0% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \log \left(\mathsf{hypot}\left(y, x\right)\right) \end{array} \]
(FPCore (x y) :precision binary64 (log (hypot y x)))
double code(double x, double y) {
	return log(hypot(y, x));
}
public static double code(double x, double y) {
	return Math.log(Math.hypot(y, x));
}
def code(x, y):
	return math.log(math.hypot(y, x))
function code(x, y)
	return log(hypot(y, x))
end
function tmp = code(x, y)
	tmp = log(hypot(y, x));
end
code[x_, y_] := N[Log[N[Sqrt[y ^ 2 + x ^ 2], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\mathsf{hypot}\left(y, x\right)\right)
\end{array}
Derivation
  1. Initial program 81.6%

    \[\log \left(\sqrt{x \cdot x + y \cdot y}\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-sqrt.f64N/A

      \[\leadsto \log \color{blue}{\left(\sqrt{x \cdot x + y \cdot y}\right)} \]
    2. lift-+.f64N/A

      \[\leadsto \log \left(\sqrt{\color{blue}{x \cdot x + y \cdot y}}\right) \]
    3. +-commutativeN/A

      \[\leadsto \log \left(\sqrt{\color{blue}{y \cdot y + x \cdot x}}\right) \]
    4. lift-*.f64N/A

      \[\leadsto \log \left(\sqrt{\color{blue}{y \cdot y} + x \cdot x}\right) \]
    5. lift-*.f64N/A

      \[\leadsto \log \left(\sqrt{y \cdot y + \color{blue}{x \cdot x}}\right) \]
    6. lower-hypot.f64100.0

      \[\leadsto \log \color{blue}{\left(\mathsf{hypot}\left(y, x\right)\right)} \]
  4. Applied rewrites100.0%

    \[\leadsto \log \color{blue}{\left(\mathsf{hypot}\left(y, x\right)\right)} \]
  5. Add Preprocessing

Alternative 2: 24.9% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \log y \cdot 2\right) \cdot 0.5 \end{array} \]
(FPCore (x y)
 :precision binary64
 (* (fma (/ (/ x y) y) x (* (log y) 2.0)) 0.5))
double code(double x, double y) {
	return fma(((x / y) / y), x, (log(y) * 2.0)) * 0.5;
}
function code(x, y)
	return Float64(fma(Float64(Float64(x / y) / y), x, Float64(log(y) * 2.0)) * 0.5)
end
code[x_, y_] := N[(N[(N[(N[(x / y), $MachinePrecision] / y), $MachinePrecision] * x + N[(N[Log[y], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision] * 0.5), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \log y \cdot 2\right) \cdot 0.5
\end{array}
Derivation
  1. Initial program 81.6%

    \[\log \left(\sqrt{x \cdot x + y \cdot y}\right) \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-log.f64N/A

      \[\leadsto \color{blue}{\log \left(\sqrt{x \cdot x + y \cdot y}\right)} \]
    2. lift-sqrt.f64N/A

      \[\leadsto \log \color{blue}{\left(\sqrt{x \cdot x + y \cdot y}\right)} \]
    3. pow1/2N/A

      \[\leadsto \log \color{blue}{\left({\left(x \cdot x + y \cdot y\right)}^{\frac{1}{2}}\right)} \]
    4. pow-to-expN/A

      \[\leadsto \log \color{blue}{\left(e^{\log \left(x \cdot x + y \cdot y\right) \cdot \frac{1}{2}}\right)} \]
    5. rem-log-expN/A

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

      \[\leadsto \color{blue}{\log \left(x \cdot x + y \cdot y\right) \cdot \frac{1}{2}} \]
    7. lower-log.f6481.6

      \[\leadsto \color{blue}{\log \left(x \cdot x + y \cdot y\right)} \cdot 0.5 \]
    8. lift-+.f64N/A

      \[\leadsto \log \color{blue}{\left(x \cdot x + y \cdot y\right)} \cdot \frac{1}{2} \]
    9. +-commutativeN/A

      \[\leadsto \log \color{blue}{\left(y \cdot y + x \cdot x\right)} \cdot \frac{1}{2} \]
    10. lift-*.f64N/A

      \[\leadsto \log \left(\color{blue}{y \cdot y} + x \cdot x\right) \cdot \frac{1}{2} \]
    11. lower-fma.f6481.6

      \[\leadsto \log \color{blue}{\left(\mathsf{fma}\left(y, y, x \cdot x\right)\right)} \cdot 0.5 \]
  4. Applied rewrites81.6%

    \[\leadsto \color{blue}{\log \left(\mathsf{fma}\left(y, y, x \cdot x\right)\right) \cdot 0.5} \]
  5. Taylor expanded in y around inf

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

      \[\leadsto \color{blue}{\left(\frac{{x}^{2}}{{y}^{2}} + -2 \cdot \log \left(\frac{1}{y}\right)\right)} \cdot \frac{1}{2} \]
    2. unpow2N/A

      \[\leadsto \left(\frac{\color{blue}{x \cdot x}}{{y}^{2}} + -2 \cdot \log \left(\frac{1}{y}\right)\right) \cdot \frac{1}{2} \]
    3. associate-/l*N/A

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

      \[\leadsto \left(\color{blue}{\frac{x}{{y}^{2}} \cdot x} + -2 \cdot \log \left(\frac{1}{y}\right)\right) \cdot \frac{1}{2} \]
    5. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{x}{{y}^{2}}, x, -2 \cdot \log \left(\frac{1}{y}\right)\right)} \cdot \frac{1}{2} \]
    6. unpow2N/A

      \[\leadsto \mathsf{fma}\left(\frac{x}{\color{blue}{y \cdot y}}, x, -2 \cdot \log \left(\frac{1}{y}\right)\right) \cdot \frac{1}{2} \]
    7. associate-/r*N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{x}{y}}{y}}, x, -2 \cdot \log \left(\frac{1}{y}\right)\right) \cdot \frac{1}{2} \]
    8. lower-/.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{\frac{x}{y}}{y}}, x, -2 \cdot \log \left(\frac{1}{y}\right)\right) \cdot \frac{1}{2} \]
    9. lower-/.f64N/A

      \[\leadsto \mathsf{fma}\left(\frac{\color{blue}{\frac{x}{y}}}{y}, x, -2 \cdot \log \left(\frac{1}{y}\right)\right) \cdot \frac{1}{2} \]
    10. remove-double-negN/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \color{blue}{\mathsf{neg}\left(\left(\mathsf{neg}\left(-2 \cdot \log \left(\frac{1}{y}\right)\right)\right)\right)}\right) \cdot \frac{1}{2} \]
    11. *-commutativeN/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \mathsf{neg}\left(\left(\mathsf{neg}\left(\color{blue}{\log \left(\frac{1}{y}\right) \cdot -2}\right)\right)\right)\right) \cdot \frac{1}{2} \]
    12. distribute-lft-neg-outN/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\log \left(\frac{1}{y}\right)\right)\right) \cdot -2}\right)\right) \cdot \frac{1}{2} \]
    13. mul-1-negN/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \mathsf{neg}\left(\color{blue}{\left(-1 \cdot \log \left(\frac{1}{y}\right)\right)} \cdot -2\right)\right) \cdot \frac{1}{2} \]
    14. distribute-rgt-neg-inN/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \color{blue}{\left(-1 \cdot \log \left(\frac{1}{y}\right)\right) \cdot \left(\mathsf{neg}\left(-2\right)\right)}\right) \cdot \frac{1}{2} \]
    15. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \left(-1 \cdot \log \left(\frac{1}{y}\right)\right) \cdot \color{blue}{2}\right) \cdot \frac{1}{2} \]
    16. lower-*.f64N/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \color{blue}{\left(-1 \cdot \log \left(\frac{1}{y}\right)\right) \cdot 2}\right) \cdot \frac{1}{2} \]
    17. mul-1-negN/A

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

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\log y\right)\right)}\right)\right) \cdot 2\right) \cdot \frac{1}{2} \]
    19. remove-double-negN/A

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \color{blue}{\log y} \cdot 2\right) \cdot \frac{1}{2} \]
    20. lower-log.f6421.9

      \[\leadsto \mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \color{blue}{\log y} \cdot 2\right) \cdot 0.5 \]
  7. Applied rewrites21.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{\frac{x}{y}}{y}, x, \log y \cdot 2\right)} \cdot 0.5 \]
  8. Add Preprocessing

Alternative 3: 29.4% accurate, 1.2× speedup?

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

\\
\log y
\end{array}
Derivation
  1. Initial program 81.6%

    \[\log \left(\sqrt{x \cdot x + y \cdot y}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{\log y} \]
  4. Step-by-step derivation
    1. lower-log.f6427.0

      \[\leadsto \color{blue}{\log y} \]
  5. Applied rewrites27.0%

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

Reproduce

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