
(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.1%
sqr-neg94.1%
cancel-sign-sub94.1%
fma-def98.0%
Simplified98.0%
Final simplification98.0%
(FPCore (x y) :precision binary64 (if (<= (* y y) 1e+44) (* (- x y) (- x y)) (* y (- y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 1e+44) {
tmp = (x - y) * (x - y);
} else {
tmp = 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 * y) <= 1d+44) then
tmp = (x - y) * (x - y)
else
tmp = y * -y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 1e+44) {
tmp = (x - y) * (x - y);
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 1e+44: tmp = (x - y) * (x - y) else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 1e+44) tmp = Float64(Float64(x - y) * Float64(x - y)); else tmp = Float64(y * Float64(-y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 1e+44) tmp = (x - y) * (x - y); else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 1e+44], N[(N[(x - y), $MachinePrecision] * N[(x - y), $MachinePrecision]), $MachinePrecision], N[(y * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 10^{+44}:\\
\;\;\;\;\left(x - y\right) \cdot \left(x - y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 1.0000000000000001e44Initial program 100.0%
difference-of-squares100.0%
add-sqr-sqrt44.6%
sqrt-prod91.1%
sqr-neg91.1%
sqrt-unprod46.4%
add-sqr-sqrt83.0%
sub-neg83.0%
pow183.0%
pow183.0%
pow-prod-up83.0%
add-sqr-sqrt46.1%
add-sqr-sqrt19.7%
difference-of-squares19.7%
metadata-eval19.7%
unpow-prod-down19.7%
Applied egg-rr19.7%
unpow219.7%
unpow219.7%
unswap-sqr19.7%
difference-of-squares19.7%
unpow1/219.7%
unpow1/219.7%
pow-sqr19.8%
metadata-eval19.8%
unpow119.8%
unpow1/219.8%
unpow1/219.8%
pow-sqr19.8%
metadata-eval19.8%
unpow119.8%
difference-of-squares19.8%
unpow1/219.8%
unpow1/219.8%
pow-sqr36.6%
metadata-eval36.6%
unpow136.6%
Simplified83.0%
if 1.0000000000000001e44 < (*.f64 y y) Initial program 87.9%
Taylor expanded in x around 0 83.5%
mul-1-neg83.5%
Simplified83.5%
unpow283.5%
Applied egg-rr83.5%
Final simplification83.3%
(FPCore (x y) :precision binary64 (if (<= (* y y) 5e+291) (- (* x x) (* y y)) (* y (- y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+291) {
tmp = (x * x) - (y * y);
} else {
tmp = 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 * y) <= 5d+291) then
tmp = (x * x) - (y * y)
else
tmp = y * -y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+291) {
tmp = (x * x) - (y * y);
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 5e+291: tmp = (x * x) - (y * y) else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 5e+291) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(y * Float64(-y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 5e+291) tmp = (x * x) - (y * y); else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 5e+291], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(y * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 5 \cdot 10^{+291}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 5.0000000000000001e291Initial program 100.0%
if 5.0000000000000001e291 < (*.f64 y y) Initial program 78.3%
Taylor expanded in x around 0 92.7%
mul-1-neg92.7%
Simplified92.7%
unpow292.8%
Applied egg-rr92.8%
Final simplification98.0%
(FPCore (x y) :precision binary64 (* y (- y)))
double code(double x, double y) {
return y * -y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = y * -y
end function
public static double code(double x, double y) {
return y * -y;
}
def code(x, y): return y * -y
function code(x, y) return Float64(y * Float64(-y)) end
function tmp = code(x, y) tmp = y * -y; end
code[x_, y_] := N[(y * (-y)), $MachinePrecision]
\begin{array}{l}
\\
y \cdot \left(-y\right)
\end{array}
Initial program 94.1%
Taylor expanded in x around 0 56.3%
mul-1-neg56.3%
Simplified56.3%
unpow256.3%
Applied egg-rr56.3%
Final simplification56.3%
herbie shell --seed 2024020
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))