
(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 95.7%
sqr-neg95.7%
cancel-sign-sub95.7%
fma-define98.4%
Simplified98.4%
(FPCore (x y) :precision binary64 (if (<= (* y y) 1e+306) (- (* x x) (* y y)) (* y (- y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 1e+306) {
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) <= 1d+306) 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) <= 1e+306) {
tmp = (x * x) - (y * y);
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 1e+306: tmp = (x * x) - (y * y) else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 1e+306) 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) <= 1e+306) tmp = (x * x) - (y * y); else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 1e+306], 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 10^{+306}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 1.00000000000000002e306Initial program 100.0%
if 1.00000000000000002e306 < (*.f64 y y) Initial program 83.3%
Taylor expanded in x around 0 93.9%
neg-mul-193.9%
Simplified93.9%
unpow293.9%
distribute-lft-neg-in93.9%
Applied egg-rr93.9%
Final simplification98.4%
(FPCore (x y) :precision binary64 (if (<= (* y y) 2e-6) (* x x) (* y (- y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 2e-6) {
tmp = x * x;
} 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) <= 2d-6) then
tmp = x * x
else
tmp = y * -y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 2e-6) {
tmp = x * x;
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 2e-6: tmp = x * x else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 2e-6) tmp = Float64(x * x); else tmp = Float64(y * Float64(-y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 2e-6) tmp = x * x; else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e-6], N[(x * x), $MachinePrecision], N[(y * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{-6}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 1.99999999999999991e-6Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt46.7%
sqrt-unprod92.3%
sqr-neg92.3%
sqrt-prod45.5%
add-sqr-sqrt81.9%
Applied egg-rr81.9%
Taylor expanded in x around inf 82.3%
Taylor expanded in x around inf 82.5%
if 1.99999999999999991e-6 < (*.f64 y y) Initial program 91.7%
Taylor expanded in x around 0 79.0%
neg-mul-179.0%
Simplified79.0%
unpow279.0%
distribute-lft-neg-in79.0%
Applied egg-rr79.0%
Final simplification80.7%
(FPCore (x y) :precision binary64 (* x x))
double code(double x, double y) {
return x * x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * x
end function
public static double code(double x, double y) {
return x * x;
}
def code(x, y): return x * x
function code(x, y) return Float64(x * x) end
function tmp = code(x, y) tmp = x * x; end
code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x
\end{array}
Initial program 95.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt48.7%
sqrt-unprod75.2%
sqr-neg75.2%
sqrt-prod27.2%
add-sqr-sqrt50.3%
Applied egg-rr50.3%
Taylor expanded in x around inf 54.5%
Taylor expanded in x around inf 51.0%
herbie shell --seed 2024135
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))