sqrt(x*x-y*y)

Percentage Accurate: 53.2% → 99.5%
Time: 4.4s
Alternatives: 2
Speedup: 24.0×

Specification

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

\\
\sqrt{x \cdot x - y \cdot y}
\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: 53.2% accurate, 1.0× speedup?

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

\\
\sqrt{x \cdot x - y \cdot y}
\end{array}

Alternative 1: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \mathsf{fma}\left(\frac{-0.5}{x\_m} \cdot y, y, x\_m\right) \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y) :precision binary64 (fma (* (/ -0.5 x_m) y) y x_m))
x_m = fabs(x);
double code(double x_m, double y) {
	return fma(((-0.5 / x_m) * y), y, x_m);
}
x_m = abs(x)
function code(x_m, y)
	return fma(Float64(Float64(-0.5 / x_m) * y), y, x_m)
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := N[(N[(N[(-0.5 / x$95$m), $MachinePrecision] * y), $MachinePrecision] * y + x$95$m), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|

\\
\mathsf{fma}\left(\frac{-0.5}{x\_m} \cdot y, y, x\_m\right)
\end{array}
Derivation
  1. Initial program 52.4%

    \[\sqrt{x \cdot x - y \cdot y} \]
  2. Add Preprocessing
  3. Taylor expanded in y around 0

    \[\leadsto \color{blue}{x + \frac{-1}{2} \cdot \frac{{y}^{2}}{x}} \]
  4. Step-by-step derivation
    1. +-commutativeN/A

      \[\leadsto \color{blue}{\frac{-1}{2} \cdot \frac{{y}^{2}}{x} + x} \]
    2. associate-*r/N/A

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

      \[\leadsto \color{blue}{\frac{\frac{-1}{2}}{x} \cdot {y}^{2}} + x \]
    4. metadata-evalN/A

      \[\leadsto \frac{\color{blue}{\mathsf{neg}\left(\frac{1}{2}\right)}}{x} \cdot {y}^{2} + x \]
    5. distribute-neg-fracN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(\frac{\frac{1}{2}}{x}\right)\right)} \cdot {y}^{2} + x \]
    6. metadata-evalN/A

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

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

      \[\leadsto \left(\mathsf{neg}\left(\frac{1}{2} \cdot \frac{1}{x}\right)\right) \cdot \color{blue}{\left(y \cdot y\right)} + x \]
    9. associate-*r*N/A

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{-0.5}{x}} \cdot y, y, x\right) \]
  5. Applied rewrites49.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{-0.5}{x} \cdot y, y, x\right)} \]
  6. Final simplification49.0%

    \[\leadsto \mathsf{fma}\left(\frac{-0.5}{x} \cdot y, y, x\right) \]
  7. Add Preprocessing

Alternative 2: 99.0% accurate, 24.0× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ x\_m \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y) :precision binary64 x_m)
x_m = fabs(x);
double code(double x_m, double y) {
	return x_m;
}
x_m = abs(x)
real(8) function code(x_m, y)
    real(8), intent (in) :: x_m
    real(8), intent (in) :: y
    code = x_m
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
	return x_m;
}
x_m = math.fabs(x)
def code(x_m, y):
	return x_m
x_m = abs(x)
function code(x_m, y)
	return x_m
end
x_m = abs(x);
function tmp = code(x_m, y)
	tmp = x_m;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := x$95$m
\begin{array}{l}
x_m = \left|x\right|

\\
x\_m
\end{array}
Derivation
  1. Initial program 52.4%

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

      \[\leadsto \sqrt{\color{blue}{x \cdot x - y \cdot y}} \]
    2. lift-*.f64N/A

      \[\leadsto \sqrt{\color{blue}{x \cdot x} - y \cdot y} \]
    3. lift-*.f64N/A

      \[\leadsto \sqrt{x \cdot x - \color{blue}{y \cdot y}} \]
    4. difference-of-squaresN/A

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

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

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

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

      \[\leadsto \sqrt{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}} \]
    9. lower-+.f6453.0

      \[\leadsto \sqrt{\left(x - y\right) \cdot \color{blue}{\left(y + x\right)}} \]
  4. Applied rewrites53.0%

    \[\leadsto \color{blue}{\sqrt{\left(x - y\right) \cdot \left(y + x\right)}} \]
  5. Taylor expanded in x around inf

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

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

      \[\leadsto x \cdot \left(\color{blue}{\frac{\frac{1}{2} \cdot \left(y + -1 \cdot y\right)}{x}} + 1\right) \]
    3. distribute-rgt1-inN/A

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

      \[\leadsto x \cdot \left(\frac{\frac{1}{2} \cdot \left(\color{blue}{0} \cdot y\right)}{x} + 1\right) \]
    5. mul0-lftN/A

      \[\leadsto x \cdot \left(\frac{\frac{1}{2} \cdot \color{blue}{0}}{x} + 1\right) \]
    6. metadata-evalN/A

      \[\leadsto x \cdot \left(\frac{\color{blue}{0}}{x} + 1\right) \]
    7. mul0-lftN/A

      \[\leadsto x \cdot \left(\frac{\color{blue}{0 \cdot y}}{x} + 1\right) \]
    8. associate-*r/N/A

      \[\leadsto x \cdot \left(\color{blue}{0 \cdot \frac{y}{x}} + 1\right) \]
    9. mul0-lftN/A

      \[\leadsto x \cdot \left(\color{blue}{0} + 1\right) \]
    10. metadata-evalN/A

      \[\leadsto x \cdot \color{blue}{1} \]
    11. *-rgt-identity48.9

      \[\leadsto \color{blue}{x} \]
  7. Applied rewrites48.9%

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

Reproduce

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