
(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 5 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) 5e+82) (- (* x x) (* y y)) (* (pow y 2.0) (fma x (* (/ 1.0 y) (/ x y)) -1.0))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+82) {
tmp = (x * x) - (y * y);
} else {
tmp = pow(y, 2.0) * fma(x, ((1.0 / y) * (x / y)), -1.0);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 5e+82) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64((y ^ 2.0) * fma(x, Float64(Float64(1.0 / y) * Float64(x / y)), -1.0)); end return tmp end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 5e+82], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[Power[y, 2.0], $MachinePrecision] * N[(x * N[(N[(1.0 / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 5 \cdot 10^{+82}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;{y}^{2} \cdot \mathsf{fma}\left(x, \frac{1}{y} \cdot \frac{x}{y}, -1\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 5.00000000000000015e82Initial program 100.0%
if 5.00000000000000015e82 < (*.f64 y y) Initial program 83.5%
Taylor expanded in y around inf 83.5%
unpow283.5%
associate-/l*90.4%
fmm-def90.4%
metadata-eval90.4%
Simplified90.4%
*-un-lft-identity90.4%
unpow290.4%
times-frac100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= (* y y) 1e+88) (- (* x x) (* y y)) (* (pow y 2.0) (+ -1.0 (/ (/ x y) (/ y x))))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 1e+88) {
tmp = (x * x) - (y * y);
} else {
tmp = pow(y, 2.0) * (-1.0 + ((x / y) / (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) <= 1d+88) then
tmp = (x * x) - (y * y)
else
tmp = (y ** 2.0d0) * ((-1.0d0) + ((x / y) / (y / x)))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 1e+88) {
tmp = (x * x) - (y * y);
} else {
tmp = Math.pow(y, 2.0) * (-1.0 + ((x / y) / (y / x)));
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 1e+88: tmp = (x * x) - (y * y) else: tmp = math.pow(y, 2.0) * (-1.0 + ((x / y) / (y / x))) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 1e+88) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64((y ^ 2.0) * Float64(-1.0 + Float64(Float64(x / y) / Float64(y / x)))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 1e+88) tmp = (x * x) - (y * y); else tmp = (y ^ 2.0) * (-1.0 + ((x / y) / (y / x))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 1e+88], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[Power[y, 2.0], $MachinePrecision] * N[(-1.0 + N[(N[(x / y), $MachinePrecision] / N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 10^{+88}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;{y}^{2} \cdot \left(-1 + \frac{\frac{x}{y}}{\frac{y}{x}}\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 9.99999999999999959e87Initial program 100.0%
if 9.99999999999999959e87 < (*.f64 y y) Initial program 83.3%
Taylor expanded in y around inf 83.3%
unpow283.3%
associate-/l*90.4%
fmm-def90.4%
metadata-eval90.4%
Simplified90.4%
fma-undefine90.4%
div-inv90.4%
associate-*r*83.3%
pow283.3%
pow-flip83.3%
metadata-eval83.3%
Applied egg-rr83.3%
metadata-eval83.3%
pow-sqr83.3%
inv-pow83.3%
inv-pow83.3%
unpow283.3%
swap-sqr100.0%
div-inv100.0%
div-inv100.0%
clear-num100.0%
un-div-inv100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x 1e+199) (fma x x (* y (- y))) (* (+ y x) (+ y x))))
double code(double x, double y) {
double tmp;
if (x <= 1e+199) {
tmp = fma(x, x, (y * -y));
} else {
tmp = (y + x) * (y + x);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= 1e+199) tmp = fma(x, x, Float64(y * Float64(-y))); else tmp = Float64(Float64(y + x) * Float64(y + x)); end return tmp end
code[x_, y_] := If[LessEqual[x, 1e+199], N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision], N[(N[(y + x), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 10^{+199}:\\
\;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y + x\right) \cdot \left(y + x\right)\\
\end{array}
\end{array}
if x < 1.0000000000000001e199Initial program 93.9%
sqr-neg93.9%
cancel-sign-sub93.9%
fma-define96.9%
Simplified96.9%
if 1.0000000000000001e199 < x Initial program 81.5%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt59.3%
sqrt-unprod100.0%
sqr-neg100.0%
sqrt-prod40.7%
add-sqr-sqrt96.3%
Applied egg-rr96.3%
Final simplification96.9%
(FPCore (x y) :precision binary64 (if (<= x 1e+149) (- (* x x) (* y y)) (* (+ y x) (+ y x))))
double code(double x, double y) {
double tmp;
if (x <= 1e+149) {
tmp = (x * x) - (y * y);
} else {
tmp = (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 (x <= 1d+149) then
tmp = (x * x) - (y * y)
else
tmp = (y + x) * (y + x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 1e+149) {
tmp = (x * x) - (y * y);
} else {
tmp = (y + x) * (y + x);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 1e+149: tmp = (x * x) - (y * y) else: tmp = (y + x) * (y + x) return tmp
function code(x, y) tmp = 0.0 if (x <= 1e+149) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(Float64(y + x) * Float64(y + x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 1e+149) tmp = (x * x) - (y * y); else tmp = (y + x) * (y + x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 1e+149], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[(y + x), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 10^{+149}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(y + x\right) \cdot \left(y + x\right)\\
\end{array}
\end{array}
if x < 1.00000000000000005e149Initial program 95.4%
if 1.00000000000000005e149 < x Initial program 75.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt64.9%
sqrt-unprod97.3%
sqr-neg97.3%
sqrt-prod32.4%
add-sqr-sqrt89.2%
Applied egg-rr89.2%
Final simplification94.5%
(FPCore (x y) :precision binary64 (* (+ y x) (+ y x)))
double code(double x, double y) {
return (y + x) * (y + x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y + x) * (y + x)
end function
public static double code(double x, double y) {
return (y + x) * (y + x);
}
def code(x, y): return (y + x) * (y + x)
function code(x, y) return Float64(Float64(y + x) * Float64(y + x)) end
function tmp = code(x, y) tmp = (y + x) * (y + x); end
code[x_, y_] := N[(N[(y + x), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(y + x\right) \cdot \left(y + x\right)
\end{array}
Initial program 92.6%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt49.9%
sqrt-unprod73.1%
sqr-neg73.1%
sqrt-prod25.5%
add-sqr-sqrt54.1%
Applied egg-rr54.1%
Final simplification54.1%
herbie shell --seed 2024115
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))