
(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 93.4%
sqr-neg93.4%
cancel-sign-sub93.4%
fma-define96.9%
Simplified96.9%
(FPCore (x y) :precision binary64 (if (or (<= (* y y) 5e-58) (and (not (<= (* y y) 2e+65)) (<= (* y y) 2e+238))) (* x x) (* y (- y))))
double code(double x, double y) {
double tmp;
if (((y * y) <= 5e-58) || (!((y * y) <= 2e+65) && ((y * y) <= 2e+238))) {
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) <= 5d-58) .or. (.not. ((y * y) <= 2d+65)) .and. ((y * y) <= 2d+238)) 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) <= 5e-58) || (!((y * y) <= 2e+65) && ((y * y) <= 2e+238))) {
tmp = x * x;
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if ((y * y) <= 5e-58) or (not ((y * y) <= 2e+65) and ((y * y) <= 2e+238)): tmp = x * x else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if ((Float64(y * y) <= 5e-58) || (!(Float64(y * y) <= 2e+65) && (Float64(y * y) <= 2e+238))) 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) <= 5e-58) || (~(((y * y) <= 2e+65)) && ((y * y) <= 2e+238))) tmp = x * x; else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(y * y), $MachinePrecision], 5e-58], And[N[Not[LessEqual[N[(y * y), $MachinePrecision], 2e+65]], $MachinePrecision], LessEqual[N[(y * y), $MachinePrecision], 2e+238]]], N[(x * x), $MachinePrecision], N[(y * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 5 \cdot 10^{-58} \lor \neg \left(y \cdot y \leq 2 \cdot 10^{+65}\right) \land y \cdot y \leq 2 \cdot 10^{+238}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 4.99999999999999977e-58 or 2e65 < (*.f64 y y) < 2.0000000000000001e238Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.8%
sqrt-unprod89.4%
sqr-neg89.4%
sqrt-prod43.5%
add-sqr-sqrt80.7%
Applied egg-rr80.7%
Taylor expanded in x around inf 81.1%
Taylor expanded in x around inf 81.3%
if 4.99999999999999977e-58 < (*.f64 y y) < 2e65 or 2.0000000000000001e238 < (*.f64 y y) Initial program 86.2%
Taylor expanded in x around 0 83.4%
neg-mul-183.4%
Simplified83.4%
unpow283.4%
distribute-lft-neg-in83.4%
Applied egg-rr83.4%
Final simplification82.3%
(FPCore (x y) :precision binary64 (if (<= (* y y) 4e+304) (- (* x x) (* y y)) (* y (- y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 4e+304) {
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) <= 4d+304) 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) <= 4e+304) {
tmp = (x * x) - (y * y);
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 4e+304: tmp = (x * x) - (y * y) else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 4e+304) 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) <= 4e+304) tmp = (x * x) - (y * y); else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 4e+304], 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 4 \cdot 10^{+304}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 3.9999999999999998e304Initial program 100.0%
if 3.9999999999999998e304 < (*.f64 y y) Initial program 76.4%
Taylor expanded in x around 0 88.9%
neg-mul-188.9%
Simplified88.9%
unpow288.9%
distribute-lft-neg-in88.9%
Applied egg-rr88.9%
Final simplification96.9%
(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 93.4%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.6%
sqrt-unprod71.0%
sqr-neg71.0%
sqrt-prod27.2%
add-sqr-sqrt49.7%
Applied egg-rr49.7%
Taylor expanded in x around inf 54.2%
Taylor expanded in x around inf 50.6%
herbie shell --seed 2024170
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))