
(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 6 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+307) (- (* 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+307) {
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+307) 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+307], 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^{+307}:\\
\;\;\;\;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) < 5e307Initial program 100.0%
if 5e307 < (*.f64 y y) Initial program 71.7%
Taylor expanded in y around inf 71.7%
unpow271.7%
associate-/l*81.7%
fma-neg81.7%
metadata-eval81.7%
Simplified81.7%
*-un-lft-identity81.7%
unpow281.7%
times-frac100.0%
Applied egg-rr100.0%
(FPCore (x y) :precision binary64 (if (<= (* y y) 5e+307) (- (* x x) (* y y)) (* (pow y 2.0) (+ -1.0 (pow (/ x y) 2.0)))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+307) {
tmp = (x * x) - (y * y);
} else {
tmp = pow(y, 2.0) * (-1.0 + pow((x / y), 2.0));
}
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+307) then
tmp = (x * x) - (y * y)
else
tmp = (y ** 2.0d0) * ((-1.0d0) + ((x / y) ** 2.0d0))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 5e+307) {
tmp = (x * x) - (y * y);
} else {
tmp = Math.pow(y, 2.0) * (-1.0 + Math.pow((x / y), 2.0));
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 5e+307: tmp = (x * x) - (y * y) else: tmp = math.pow(y, 2.0) * (-1.0 + math.pow((x / y), 2.0)) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 5e+307) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64((y ^ 2.0) * Float64(-1.0 + (Float64(x / y) ^ 2.0))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 5e+307) tmp = (x * x) - (y * y); else tmp = (y ^ 2.0) * (-1.0 + ((x / y) ^ 2.0)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 5e+307], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[Power[y, 2.0], $MachinePrecision] * N[(-1.0 + N[Power[N[(x / y), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 5 \cdot 10^{+307}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;{y}^{2} \cdot \left(-1 + {\left(\frac{x}{y}\right)}^{2}\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 5e307Initial program 100.0%
if 5e307 < (*.f64 y y) Initial program 71.7%
Taylor expanded in y around inf 71.7%
unpow271.7%
associate-/l*81.7%
fma-neg81.7%
metadata-eval81.7%
Simplified81.7%
*-un-lft-identity81.7%
unpow281.7%
times-frac100.0%
Applied egg-rr100.0%
fma-undefine100.0%
associate-*r*100.0%
div-inv100.0%
pow2100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= (* y y) 5e+307) (- (* 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) <= 5e+307) {
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) <= 5d+307) 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) <= 5e+307) {
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) <= 5e+307: 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) <= 5e+307) 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) <= 5e+307) 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], 5e+307], 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 5 \cdot 10^{+307}:\\
\;\;\;\;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) < 5e307Initial program 100.0%
if 5e307 < (*.f64 y y) Initial program 71.7%
Taylor expanded in y around inf 71.7%
unpow271.7%
associate-/l*81.7%
fma-neg81.7%
metadata-eval81.7%
Simplified81.7%
*-un-lft-identity81.7%
unpow281.7%
times-frac100.0%
Applied egg-rr100.0%
fma-undefine100.0%
associate-*r*100.0%
div-inv100.0%
pow2100.0%
Applied egg-rr100.0%
unpow2100.0%
clear-num100.0%
un-div-inv100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x 3.2e+208) (fma x x (* y (- y))) (* (+ y x) (+ y x))))
double code(double x, double y) {
double tmp;
if (x <= 3.2e+208) {
tmp = fma(x, x, (y * -y));
} else {
tmp = (y + x) * (y + x);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= 3.2e+208) 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, 3.2e+208], 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 3.2 \cdot 10^{+208}:\\
\;\;\;\;\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 < 3.2000000000000001e208Initial program 94.5%
sqr-neg94.5%
cancel-sign-sub94.5%
fma-define96.6%
Simplified96.6%
if 3.2000000000000001e208 < x Initial program 77.8%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt44.4%
sqrt-unprod94.4%
sqr-neg94.4%
sqrt-prod50.0%
add-sqr-sqrt94.4%
Applied egg-rr94.4%
Final simplification96.5%
(FPCore (x y) :precision binary64 (if (<= x 7.5e+161) (- (* x x) (* y y)) (* (+ y x) (+ y x))))
double code(double x, double y) {
double tmp;
if (x <= 7.5e+161) {
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 <= 7.5d+161) 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 <= 7.5e+161) {
tmp = (x * x) - (y * y);
} else {
tmp = (y + x) * (y + x);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 7.5e+161: tmp = (x * x) - (y * y) else: tmp = (y + x) * (y + x) return tmp
function code(x, y) tmp = 0.0 if (x <= 7.5e+161) 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 <= 7.5e+161) tmp = (x * x) - (y * y); else tmp = (y + x) * (y + x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 7.5e+161], 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 7.5 \cdot 10^{+161}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(y + x\right) \cdot \left(y + x\right)\\
\end{array}
\end{array}
if x < 7.4999999999999995e161Initial program 94.4%
if 7.4999999999999995e161 < x Initial program 82.6%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt43.5%
sqrt-unprod95.7%
sqr-neg95.7%
sqrt-prod52.2%
add-sqr-sqrt95.7%
Applied egg-rr95.7%
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 93.4%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt49.5%
sqrt-unprod71.9%
sqr-neg71.9%
sqrt-prod25.4%
add-sqr-sqrt54.3%
Applied egg-rr54.3%
Final simplification54.3%
herbie shell --seed 2024103
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))