
(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 (<= (* x x) 1e+142) (- (* x x) (* y y)) (* (pow x 2.0) (- 1.0 (* (/ y x) (/ y x))))))
double code(double x, double y) {
double tmp;
if ((x * x) <= 1e+142) {
tmp = (x * x) - (y * y);
} else {
tmp = pow(x, 2.0) * (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 ((x * x) <= 1d+142) then
tmp = (x * x) - (y * y)
else
tmp = (x ** 2.0d0) * (1.0d0 - ((y / x) * (y / x)))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x * x) <= 1e+142) {
tmp = (x * x) - (y * y);
} else {
tmp = Math.pow(x, 2.0) * (1.0 - ((y / x) * (y / x)));
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 1e+142: tmp = (x * x) - (y * y) else: tmp = math.pow(x, 2.0) * (1.0 - ((y / x) * (y / x))) return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 1e+142) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64((x ^ 2.0) * Float64(1.0 - Float64(Float64(y / x) * Float64(y / x)))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x * x) <= 1e+142) tmp = (x * x) - (y * y); else tmp = (x ^ 2.0) * (1.0 - ((y / x) * (y / x))); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 1e+142], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[Power[x, 2.0], $MachinePrecision] * N[(1.0 - N[(N[(y / x), $MachinePrecision] * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 10^{+142}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;{x}^{2} \cdot \left(1 - \frac{y}{x} \cdot \frac{y}{x}\right)\\
\end{array}
\end{array}
if (*.f64 x x) < 1.00000000000000005e142Initial program 100.0%
if 1.00000000000000005e142 < (*.f64 x x) Initial program 84.9%
Taylor expanded in x around inf 84.9%
mul-1-neg84.9%
unsub-neg84.9%
Simplified84.9%
unpow284.9%
unpow284.9%
times-frac100.0%
Applied egg-rr100.0%
(FPCore (x y) :precision binary64 (if (<= x 1e+198) (fma x x (* y (- y))) (* (+ x y) (+ x y))))
double code(double x, double y) {
double tmp;
if (x <= 1e+198) {
tmp = fma(x, x, (y * -y));
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= 1e+198) tmp = fma(x, x, Float64(y * Float64(-y))); else tmp = Float64(Float64(x + y) * Float64(x + y)); end return tmp end
code[x_, y_] := If[LessEqual[x, 1e+198], N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision], N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 10^{+198}:\\
\;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\left(x + y\right) \cdot \left(x + y\right)\\
\end{array}
\end{array}
if x < 1.00000000000000002e198Initial program 96.1%
sqr-neg96.1%
cancel-sign-sub96.1%
fma-define98.3%
Simplified98.3%
if 1.00000000000000002e198 < x Initial program 78.3%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt60.9%
sqrt-unprod95.7%
sqr-neg95.7%
sqrt-prod34.8%
add-sqr-sqrt95.7%
Applied egg-rr95.7%
(FPCore (x y) :precision binary64 (if (<= x 5.8e+150) (- (* x x) (* y y)) (* (+ x y) (+ x y))))
double code(double x, double y) {
double tmp;
if (x <= 5.8e+150) {
tmp = (x * x) - (y * y);
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 5.8d+150) then
tmp = (x * x) - (y * y)
else
tmp = (x + y) * (x + y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 5.8e+150) {
tmp = (x * x) - (y * y);
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 5.8e+150: tmp = (x * x) - (y * y) else: tmp = (x + y) * (x + y) return tmp
function code(x, y) tmp = 0.0 if (x <= 5.8e+150) tmp = Float64(Float64(x * x) - Float64(y * y)); else tmp = Float64(Float64(x + y) * Float64(x + y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 5.8e+150) tmp = (x * x) - (y * y); else tmp = (x + y) * (x + y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 5.8e+150], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.8 \cdot 10^{+150}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(x + y\right) \cdot \left(x + y\right)\\
\end{array}
\end{array}
if x < 5.80000000000000022e150Initial program 97.3%
if 5.80000000000000022e150 < x Initial program 75.8%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt57.6%
sqrt-unprod90.9%
sqr-neg90.9%
sqrt-prod33.3%
add-sqr-sqrt87.9%
Applied egg-rr87.9%
(FPCore (x y) :precision binary64 (* (+ x y) (+ x y)))
double code(double x, double y) {
return (x + y) * (x + y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) * (x + y)
end function
public static double code(double x, double y) {
return (x + y) * (x + y);
}
def code(x, y): return (x + y) * (x + y)
function code(x, y) return Float64(Float64(x + y) * Float64(x + y)) end
function tmp = code(x, y) tmp = (x + y) * (x + y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] * N[(x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + y\right) \cdot \left(x + y\right)
\end{array}
Initial program 94.5%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt51.9%
sqrt-unprod74.5%
sqr-neg74.5%
sqrt-prod24.1%
add-sqr-sqrt52.8%
Applied egg-rr52.8%
herbie shell --seed 2024100
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))