
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
return (x * x) - (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
return (x * x) - (y * y);
}
def code(x, y): return (x * x) - (y * y)
function code(x, y) return Float64(Float64(x * x) - Float64(y * y)) end
function tmp = code(x, y) tmp = (x * x) - (y * y); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x - y \cdot y
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
return (x * x) - (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
return (x * x) - (y * y);
}
def code(x, y): return (x * x) - (y * y)
function code(x, y) return Float64(Float64(x * x) - Float64(y * y)) end
function tmp = code(x, y) tmp = (x * x) - (y * y); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x - y \cdot y
\end{array}
(FPCore (x y) :precision binary64 (fma x x (* y (- y))))
double code(double x, double y) {
return fma(x, x, (y * -y));
}
function code(x, y) return fma(x, x, Float64(y * Float64(-y))) end
code[x_, y_] := N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)
\end{array}
Initial program 94.9%
sqr-neg94.9%
cancel-sign-sub94.9%
fma-define98.8%
Simplified98.8%
(FPCore (x y) :precision binary64 (if (<= (* y y) INFINITY) (- (* x x) (* y y)) (- (pow y 2.0))))
double code(double x, double y) {
double tmp;
if ((y * y) <= ((double) INFINITY)) {
tmp = (x * x) - (y * y);
} else {
tmp = -pow(y, 2.0);
}
return tmp;
}
public static double code(double x, double y) {
double tmp;
if ((y * y) <= Double.POSITIVE_INFINITY) {
tmp = (x * x) - (y * y);
} else {
tmp = -Math.pow(y, 2.0);
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= math.inf: tmp = (x * x) - (y * y) else: tmp = -math.pow(y, 2.0) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= Inf) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(-(y ^ 2.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= Inf) tmp = (x * x) - (y * y); else tmp = -(y ^ 2.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], Infinity], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], (-N[Power[y, 2.0], $MachinePrecision])]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq \infty:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;-{y}^{2}\\
\end{array}
\end{array}
if (*.f64 y y) < +inf.0Initial program 94.9%
if +inf.0 < (*.f64 y y) Initial program 94.9%
Taylor expanded in x around 0 52.5%
mul-1-neg52.5%
Simplified52.5%
(FPCore (x y) :precision binary64 (- (* x x) (* y y)))
double code(double x, double y) {
return (x * x) - (y * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) - (y * y)
end function
public static double code(double x, double y) {
return (x * x) - (y * y);
}
def code(x, y): return (x * x) - (y * y)
function code(x, y) return Float64(Float64(x * x) - Float64(y * y)) end
function tmp = code(x, y) tmp = (x * x) - (y * y); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x - y \cdot y
\end{array}
Initial program 94.9%
(FPCore (x y) :precision binary64 (* (+ x y) (+ x y)))
double code(double x, double y) {
return (x + y) * (x + y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) * (x + y)
end function
public static double code(double x, double y) {
return (x + y) * (x + y);
}
def code(x, y): return (x + y) * (x + y)
function code(x, y) return Float64(Float64(x + y) * Float64(x + y)) end
function tmp = code(x, y) tmp = (x + y) * (x + y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) \cdot \left(x + y\right)
\end{array}
Initial program 94.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt53.4%
sqrt-unprod80.0%
sqr-neg80.0%
sqrt-prod26.9%
add-sqr-sqrt53.9%
Applied egg-rr53.9%
herbie shell --seed 2024086
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))