
(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 (if (<= (* y y) 2e+300) (fma x x (* y (- y))) (* x (* x (- 1.0 (* (/ y x) (/ y x)))))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 2e+300) {
tmp = fma(x, x, (y * -y));
} else {
tmp = x * (x * (1.0 - ((y / x) * (y / x))));
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 2e+300) tmp = fma(x, x, Float64(y * Float64(-y))); else tmp = Float64(x * Float64(x * Float64(1.0 - Float64(Float64(y / x) * Float64(y / x))))); end return tmp end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e+300], N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision], N[(x * N[(x * N[(1.0 - N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{+300}:\\
\;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(x \cdot \left(1 - \frac{y}{x} \cdot \frac{y}{x}\right)\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 2.0000000000000001e300Initial program 100.0%
sqr-neg100.0%
cancel-sign-sub100.0%
fma-define100.0%
Simplified100.0%
if 2.0000000000000001e300 < (*.f64 y y) Initial program 76.0%
Taylor expanded in x around inf 53.5%
mul-1-neg53.5%
unsub-neg53.5%
Simplified53.5%
add-sqr-sqrt1.4%
pow21.4%
sqrt-prod1.4%
sqrt-pow11.4%
metadata-eval1.4%
pow11.4%
add-sqr-sqrt1.4%
pow21.4%
sqrt-div1.4%
sqrt-pow111.3%
metadata-eval11.3%
pow111.3%
sqrt-pow111.3%
metadata-eval11.3%
pow111.3%
Applied egg-rr11.3%
unpow211.3%
*-commutative11.3%
*-commutative11.3%
swap-sqr11.2%
pow211.2%
pow211.2%
add-sqr-sqrt77.4%
associate-*r*100.0%
pow2100.0%
Applied egg-rr100.0%
pow2100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= (* y y) 2e+300) (- (* x x) (* y y)) (* x (* x (- 1.0 (* (/ y x) (/ y x)))))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 2e+300) {
tmp = (x * x) - (y * y);
} else {
tmp = x * (x * (1.0 - ((y / x) * (y / x))));
}
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+300) then
tmp = (x * x) - (y * y)
else
tmp = x * (x * (1.0d0 - ((y / x) * (y / x))))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 2e+300) {
tmp = (x * x) - (y * y);
} else {
tmp = x * (x * (1.0 - ((y / x) * (y / x))));
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 2e+300: tmp = (x * x) - (y * y) else: tmp = x * (x * (1.0 - ((y / x) * (y / x)))) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 2e+300) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(x * Float64(x * Float64(1.0 - Float64(Float64(y / x) * Float64(y / x))))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 2e+300) tmp = (x * x) - (y * y); else tmp = x * (x * (1.0 - ((y / x) * (y / x)))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e+300], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(x * N[(x * N[(1.0 - N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{+300}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(x \cdot \left(1 - \frac{y}{x} \cdot \frac{y}{x}\right)\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 2.0000000000000001e300Initial program 100.0%
if 2.0000000000000001e300 < (*.f64 y y) Initial program 76.0%
Taylor expanded in x around inf 53.5%
mul-1-neg53.5%
unsub-neg53.5%
Simplified53.5%
add-sqr-sqrt1.4%
pow21.4%
sqrt-prod1.4%
sqrt-pow11.4%
metadata-eval1.4%
pow11.4%
add-sqr-sqrt1.4%
pow21.4%
sqrt-div1.4%
sqrt-pow111.3%
metadata-eval11.3%
pow111.3%
sqrt-pow111.3%
metadata-eval11.3%
pow111.3%
Applied egg-rr11.3%
unpow211.3%
*-commutative11.3%
*-commutative11.3%
swap-sqr11.2%
pow211.2%
pow211.2%
add-sqr-sqrt77.4%
associate-*r*100.0%
pow2100.0%
Applied egg-rr100.0%
pow2100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= (* x x) 5e+302) (- (* x x) (* y y)) (* x x)))
double code(double x, double y) {
double tmp;
if ((x * x) <= 5e+302) {
tmp = (x * x) - (y * y);
} else {
tmp = x * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x * x) <= 5d+302) then
tmp = (x * x) - (y * y)
else
tmp = x * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x * x) <= 5e+302) {
tmp = (x * x) - (y * y);
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 5e+302: tmp = (x * x) - (y * y) else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 5e+302) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x * x) <= 5e+302) tmp = (x * x) - (y * y); else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 5e+302], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 5 \cdot 10^{+302}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 5e302Initial program 100.0%
if 5e302 < (*.f64 x x) Initial program 73.8%
Taylor expanded in x around inf 73.8%
mul-1-neg73.8%
unsub-neg73.8%
Simplified73.8%
add-sqr-sqrt73.8%
pow273.8%
sqrt-prod73.8%
sqrt-pow173.8%
metadata-eval73.8%
pow173.8%
add-sqr-sqrt73.8%
pow273.8%
sqrt-div73.8%
sqrt-pow184.6%
metadata-eval84.6%
pow184.6%
sqrt-pow184.6%
metadata-eval84.6%
pow184.6%
Applied egg-rr84.6%
unpow284.6%
*-commutative84.6%
*-commutative84.6%
swap-sqr84.6%
pow284.6%
pow284.6%
add-sqr-sqrt100.0%
associate-*r*100.0%
pow2100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 84.6%
Final simplification96.1%
(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.3%
Taylor expanded in x around inf 66.9%
mul-1-neg66.9%
unsub-neg66.9%
Simplified66.9%
add-sqr-sqrt40.6%
pow240.6%
sqrt-prod40.6%
sqrt-pow140.6%
metadata-eval40.6%
pow140.6%
add-sqr-sqrt40.6%
pow240.6%
sqrt-div40.6%
sqrt-pow143.4%
metadata-eval43.4%
pow143.4%
sqrt-pow145.3%
metadata-eval45.3%
pow145.3%
Applied egg-rr45.3%
unpow245.3%
*-commutative45.3%
*-commutative45.3%
swap-sqr45.3%
pow245.3%
pow245.3%
add-sqr-sqrt78.0%
associate-*r*87.9%
pow287.9%
Applied egg-rr87.9%
Taylor expanded in y around 0 48.0%
Final simplification48.0%
herbie shell --seed 2024075
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))