
(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 91.0%
sqr-neg91.0%
cancel-sign-sub91.0%
fma-def97.3%
Simplified97.3%
Final simplification97.3%
(FPCore (x y)
:precision binary64
(if (or (<= (* y y) 1e-216)
(and (not (<= (* y y) 1e-183)) (<= (* y y) 1e+50)))
(* (- x y) (- x y))
(* y (- y))))
double code(double x, double y) {
double tmp;
if (((y * y) <= 1e-216) || (!((y * y) <= 1e-183) && ((y * y) <= 1e+50))) {
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-216) .or. (.not. ((y * y) <= 1d-183)) .and. ((y * y) <= 1d+50)) 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-216) || (!((y * y) <= 1e-183) && ((y * y) <= 1e+50))) {
tmp = (x - y) * (x - y);
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if ((y * y) <= 1e-216) or (not ((y * y) <= 1e-183) and ((y * y) <= 1e+50)): tmp = (x - y) * (x - y) else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if ((Float64(y * y) <= 1e-216) || (!(Float64(y * y) <= 1e-183) && (Float64(y * y) <= 1e+50))) 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-216) || (~(((y * y) <= 1e-183)) && ((y * y) <= 1e+50))) tmp = (x - y) * (x - y); else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(y * y), $MachinePrecision], 1e-216], And[N[Not[LessEqual[N[(y * y), $MachinePrecision], 1e-183]], $MachinePrecision], LessEqual[N[(y * y), $MachinePrecision], 1e+50]]], 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^{-216} \lor \neg \left(y \cdot y \leq 10^{-183}\right) \land y \cdot y \leq 10^{+50}:\\
\;\;\;\;\left(x - y\right) \cdot \left(x - y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 1e-216 or 1.00000000000000001e-183 < (*.f64 y y) < 1.0000000000000001e50Initial program 100.0%
difference-of-squares100.0%
add-sqr-sqrt49.6%
sqrt-prod92.3%
sqr-neg92.3%
sqrt-unprod42.7%
add-sqr-sqrt85.5%
sub-neg85.5%
pow185.5%
pow185.5%
pow-prod-up85.5%
add-sqr-sqrt42.6%
add-sqr-sqrt22.5%
difference-of-squares22.5%
metadata-eval22.5%
unpow-prod-down22.4%
Applied egg-rr22.4%
unpow222.4%
unpow222.4%
unswap-sqr22.5%
difference-of-squares22.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr22.5%
metadata-eval22.5%
unpow122.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr22.5%
metadata-eval22.5%
unpow122.5%
difference-of-squares22.5%
unpow1/222.5%
unpow1/222.5%
pow-sqr42.8%
metadata-eval42.8%
unpow142.8%
Simplified85.5%
if 1e-216 < (*.f64 y y) < 1.00000000000000001e-183 or 1.0000000000000001e50 < (*.f64 y y) Initial program 81.9%
Taylor expanded in x around 0 81.4%
mul-1-neg81.4%
Simplified81.4%
unpow281.4%
Applied egg-rr81.4%
Final simplification83.5%
(FPCore (x y) :precision binary64 (if (<= (* y y) 2e+292) (- (* x x) (* y y)) (* y (- y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 2e+292) {
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) <= 2d+292) 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) <= 2e+292) {
tmp = (x * x) - (y * y);
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 2e+292: tmp = (x * x) - (y * y) else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 2e+292) 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) <= 2e+292) tmp = (x * x) - (y * y); else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e+292], 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 2 \cdot 10^{+292}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 2e292Initial program 100.0%
if 2e292 < (*.f64 y y) Initial program 66.7%
Taylor expanded in x around 0 89.9%
mul-1-neg89.9%
Simplified89.9%
unpow289.9%
Applied egg-rr89.9%
Final simplification97.3%
(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 91.0%
Taylor expanded in x around 0 56.4%
mul-1-neg56.4%
Simplified56.4%
unpow256.4%
Applied egg-rr56.4%
Final simplification56.4%
herbie shell --seed 2024031
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))