x - sqrt(pow(y, 2) - x)

Percentage Accurate: 100.0% → 100.0%
Time: 2.8s
Alternatives: 6
Speedup: 5.4×

Specification

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

\\
x - \sqrt{{y}^{2} - 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 6 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 - \sqrt{{y}^{2} - x} \end{array} \]
(FPCore (x y) :precision binary64 (- x (sqrt (- (pow y 2.0) x))))
double code(double x, double y) {
	return x - sqrt((pow(y, 2.0) - x));
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x - sqrt(((y ** 2.0d0) - x))
end function
public static double code(double x, double y) {
	return x - Math.sqrt((Math.pow(y, 2.0) - x));
}
def code(x, y):
	return x - math.sqrt((math.pow(y, 2.0) - x))
function code(x, y)
	return Float64(x - sqrt(Float64((y ^ 2.0) - x)))
end
function tmp = code(x, y)
	tmp = x - sqrt(((y ^ 2.0) - x));
end
code[x_, y_] := N[(x - N[Sqrt[N[(N[Power[y, 2.0], $MachinePrecision] - x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \sqrt{{y}^{2} - x}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

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

\\
x - \sqrt{{y}^{2} - x}
\end{array}
Derivation
  1. Initial program 100.0%

    \[x - \sqrt{{y}^{2} - x} \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 68.3% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;{y}^{2} \leq 10^{-223}:\\ \;\;\;\;x - \sqrt{-x}\\ \mathbf{else}:\\ \;\;\;\;x - \mathsf{fma}\left(\frac{x}{y}, -0.5, y\right)\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (pow y 2.0) 1e-223) (- x (sqrt (- x))) (- x (fma (/ x y) -0.5 y))))
double code(double x, double y) {
	double tmp;
	if (pow(y, 2.0) <= 1e-223) {
		tmp = x - sqrt(-x);
	} else {
		tmp = x - fma((x / y), -0.5, y);
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if ((y ^ 2.0) <= 1e-223)
		tmp = Float64(x - sqrt(Float64(-x)));
	else
		tmp = Float64(x - fma(Float64(x / y), -0.5, y));
	end
	return tmp
end
code[x_, y_] := If[LessEqual[N[Power[y, 2.0], $MachinePrecision], 1e-223], N[(x - N[Sqrt[(-x)], $MachinePrecision]), $MachinePrecision], N[(x - N[(N[(x / y), $MachinePrecision] * -0.5 + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;{y}^{2} \leq 10^{-223}:\\
\;\;\;\;x - \sqrt{-x}\\

\mathbf{else}:\\
\;\;\;\;x - \mathsf{fma}\left(\frac{x}{y}, -0.5, y\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 y #s(literal 2 binary64)) < 9.9999999999999997e-224

    1. Initial program 100.0%

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

      \[\leadsto x - \sqrt{\color{blue}{-1 \cdot x}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto x - \sqrt{\color{blue}{\mathsf{neg}\left(x\right)}} \]
      2. lower-neg.f6494.1

        \[\leadsto x - \sqrt{\color{blue}{-x}} \]
    5. Applied rewrites94.1%

      \[\leadsto x - \sqrt{\color{blue}{-x}} \]

    if 9.9999999999999997e-224 < (pow.f64 y #s(literal 2 binary64))

    1. Initial program 100.0%

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

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

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

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

        \[\leadsto x - \color{blue}{\mathsf{fma}\left(\frac{x}{y}, \frac{-1}{2}, y\right)} \]
      4. lower-/.f6441.6

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

      \[\leadsto x - \color{blue}{\mathsf{fma}\left(\frac{x}{y}, -0.5, y\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 86.3% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;{y}^{2} \leq 10^{-223}:\\ \;\;\;\;x - \sqrt{-x}\\ \mathbf{else}:\\ \;\;\;\;x - \sqrt{y \cdot y}\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (pow y 2.0) 1e-223) (- x (sqrt (- x))) (- x (sqrt (* y y)))))
double code(double x, double y) {
	double tmp;
	if (pow(y, 2.0) <= 1e-223) {
		tmp = x - sqrt(-x);
	} else {
		tmp = x - sqrt((y * y));
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y ** 2.0d0) <= 1d-223) then
        tmp = x - sqrt(-x)
    else
        tmp = x - sqrt((y * y))
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (Math.pow(y, 2.0) <= 1e-223) {
		tmp = x - Math.sqrt(-x);
	} else {
		tmp = x - Math.sqrt((y * y));
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if math.pow(y, 2.0) <= 1e-223:
		tmp = x - math.sqrt(-x)
	else:
		tmp = x - math.sqrt((y * y))
	return tmp
function code(x, y)
	tmp = 0.0
	if ((y ^ 2.0) <= 1e-223)
		tmp = Float64(x - sqrt(Float64(-x)));
	else
		tmp = Float64(x - sqrt(Float64(y * y)));
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y ^ 2.0) <= 1e-223)
		tmp = x - sqrt(-x);
	else
		tmp = x - sqrt((y * y));
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[Power[y, 2.0], $MachinePrecision], 1e-223], N[(x - N[Sqrt[(-x)], $MachinePrecision]), $MachinePrecision], N[(x - N[Sqrt[N[(y * y), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;{y}^{2} \leq 10^{-223}:\\
\;\;\;\;x - \sqrt{-x}\\

\mathbf{else}:\\
\;\;\;\;x - \sqrt{y \cdot y}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 y #s(literal 2 binary64)) < 9.9999999999999997e-224

    1. Initial program 100.0%

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

      \[\leadsto x - \sqrt{\color{blue}{-1 \cdot x}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto x - \sqrt{\color{blue}{\mathsf{neg}\left(x\right)}} \]
      2. lower-neg.f6494.1

        \[\leadsto x - \sqrt{\color{blue}{-x}} \]
    5. Applied rewrites94.1%

      \[\leadsto x - \sqrt{\color{blue}{-x}} \]

    if 9.9999999999999997e-224 < (pow.f64 y #s(literal 2 binary64))

    1. Initial program 100.0%

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

      \[\leadsto x - \sqrt{\color{blue}{{y}^{2}}} \]
    4. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto x - \sqrt{\color{blue}{y \cdot y}} \]
      2. lower-*.f6481.5

        \[\leadsto x - \sqrt{\color{blue}{y \cdot y}} \]
    5. Applied rewrites81.5%

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

Alternative 4: 68.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;{y}^{2} \leq 10^{-223}:\\ \;\;\;\;x - \sqrt{-x}\\ \mathbf{else}:\\ \;\;\;\;-y\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= (pow y 2.0) 1e-223) (- x (sqrt (- x))) (- y)))
double code(double x, double y) {
	double tmp;
	if (pow(y, 2.0) <= 1e-223) {
		tmp = x - sqrt(-x);
	} else {
		tmp = -y;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if ((y ** 2.0d0) <= 1d-223) then
        tmp = x - sqrt(-x)
    else
        tmp = -y
    end if
    code = tmp
end function
public static double code(double x, double y) {
	double tmp;
	if (Math.pow(y, 2.0) <= 1e-223) {
		tmp = x - Math.sqrt(-x);
	} else {
		tmp = -y;
	}
	return tmp;
}
def code(x, y):
	tmp = 0
	if math.pow(y, 2.0) <= 1e-223:
		tmp = x - math.sqrt(-x)
	else:
		tmp = -y
	return tmp
function code(x, y)
	tmp = 0.0
	if ((y ^ 2.0) <= 1e-223)
		tmp = Float64(x - sqrt(Float64(-x)));
	else
		tmp = Float64(-y);
	end
	return tmp
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((y ^ 2.0) <= 1e-223)
		tmp = x - sqrt(-x);
	else
		tmp = -y;
	end
	tmp_2 = tmp;
end
code[x_, y_] := If[LessEqual[N[Power[y, 2.0], $MachinePrecision], 1e-223], N[(x - N[Sqrt[(-x)], $MachinePrecision]), $MachinePrecision], (-y)]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;{y}^{2} \leq 10^{-223}:\\
\;\;\;\;x - \sqrt{-x}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (pow.f64 y #s(literal 2 binary64)) < 9.9999999999999997e-224

    1. Initial program 100.0%

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

      \[\leadsto x - \sqrt{\color{blue}{-1 \cdot x}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto x - \sqrt{\color{blue}{\mathsf{neg}\left(x\right)}} \]
      2. lower-neg.f6494.1

        \[\leadsto x - \sqrt{\color{blue}{-x}} \]
    5. Applied rewrites94.1%

      \[\leadsto x - \sqrt{\color{blue}{-x}} \]

    if 9.9999999999999997e-224 < (pow.f64 y #s(literal 2 binary64))

    1. Initial program 100.0%

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

      \[\leadsto \color{blue}{-1 \cdot y} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(y\right)} \]
      2. lower-neg.f6440.9

        \[\leadsto \color{blue}{-y} \]
    5. Applied rewrites40.9%

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

Alternative 5: 100.0% accurate, 5.4× speedup?

\[\begin{array}{l} \\ x - \sqrt{\mathsf{fma}\left(y, y, -x\right)} \end{array} \]
(FPCore (x y) :precision binary64 (- x (sqrt (fma y y (- x)))))
double code(double x, double y) {
	return x - sqrt(fma(y, y, -x));
}
function code(x, y)
	return Float64(x - sqrt(fma(y, y, Float64(-x))))
end
code[x_, y_] := N[(x - N[Sqrt[N[(y * y + (-x)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x - \sqrt{\mathsf{fma}\left(y, y, -x\right)}
\end{array}
Derivation
  1. Initial program 100.0%

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

      \[\leadsto x - \sqrt{\color{blue}{{y}^{2} - x}} \]
    2. sub-negN/A

      \[\leadsto x - \sqrt{\color{blue}{{y}^{2} + \left(\mathsf{neg}\left(x\right)\right)}} \]
    3. lift-pow.f64N/A

      \[\leadsto x - \sqrt{\color{blue}{{y}^{2}} + \left(\mathsf{neg}\left(x\right)\right)} \]
    4. unpow2N/A

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

      \[\leadsto x - \sqrt{\color{blue}{\mathsf{fma}\left(y, y, \mathsf{neg}\left(x\right)\right)}} \]
    6. lower-neg.f6499.9

      \[\leadsto x - \sqrt{\mathsf{fma}\left(y, y, \color{blue}{-x}\right)} \]
  4. Applied rewrites99.9%

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

Alternative 6: 21.3% accurate, 39.3× speedup?

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

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

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

    \[\leadsto \color{blue}{-1 \cdot y} \]
  4. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto \color{blue}{\mathsf{neg}\left(y\right)} \]
    2. lower-neg.f6424.0

      \[\leadsto \color{blue}{-y} \]
  5. Applied rewrites24.0%

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

Reproduce

?
herbie shell --seed 1 
(FPCore (x y)
  :name "x - sqrt(pow(y, 2) - x)"
  :precision binary64
  :pre (and (and (<= -1000.0 x) (<= x 1000.0)) (and (<= -1000.0 y) (<= y 1000.0)))
  (- x (sqrt (- (pow y 2.0) x))))