
(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 3 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 (if (<= (* x x) 2e+291) (- (* x x) (* y y)) (* (+ x y) (+ x y))))
double code(double x, double y) {
double tmp;
if ((x * x) <= 2e+291) {
tmp = (x * x) - (y * y);
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x * x) <= 2d+291) then
tmp = (x * x) - (y * y)
else
tmp = (x + y) * (x + y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x * x) <= 2e+291) {
tmp = (x * x) - (y * y);
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 2e+291: tmp = (x * x) - (y * y) else: tmp = (x + y) * (x + y) return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 2e+291) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(Float64(x + y) * Float64(x + y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x * x) <= 2e+291) tmp = (x * x) - (y * y); else tmp = (x + y) * (x + y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 2e+291], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 2 \cdot 10^{+291}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(x + y\right) \cdot \left(x + y\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 1.9999999999999999e291Initial program 100.0%
if 1.9999999999999999e291 < (*.f64 x x) Initial program 83.6%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt52.2%
sqrt-unprod91.0%
sqr-neg91.0%
sqrt-prod43.3%
add-sqr-sqrt92.5%
Applied egg-rr92.5%
Final simplification98.0%
(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 95.7%
sqr-neg95.7%
cancel-sign-sub95.7%
fma-define97.7%
Simplified97.7%
Final simplification97.7%
(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 95.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt43.3%
sqrt-unprod69.8%
sqr-neg69.8%
sqrt-prod27.6%
add-sqr-sqrt53.0%
Applied egg-rr53.0%
Final simplification53.0%
herbie shell --seed 2024044
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))