
(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) (* y y)) -2e-239) (fma 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) - (y * y)) <= -2e-239) {
tmp = fma(x, x, (y * -y));
} else {
tmp = pow(x, 2.0) * (1.0 - ((y / x) * (y / x)));
}
return tmp;
}
function code(x, y) tmp = 0.0 if (Float64(Float64(x * x) - Float64(y * y)) <= -2e-239) tmp = fma(x, x, Float64(y * Float64(-y))); else tmp = Float64((x ^ 2.0) * Float64(1.0 - Float64(Float64(y / x) * Float64(y / x)))); end return tmp end
code[x_, y_] := If[LessEqual[N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], -2e-239], N[(x * x + 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 - y \cdot y \leq -2 \cdot 10^{-239}:\\
\;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)\\
\mathbf{else}:\\
\;\;\;\;{x}^{2} \cdot \left(1 - \frac{y}{x} \cdot \frac{y}{x}\right)\\
\end{array}
\end{array}
if (-.f64 (*.f64 x x) (*.f64 y y)) < -2.0000000000000002e-239Initial program 100.0%
sqr-neg100.0%
cancel-sign-sub100.0%
fma-define100.0%
Simplified100.0%
if -2.0000000000000002e-239 < (-.f64 (*.f64 x x) (*.f64 y y)) Initial program 83.2%
Taylor expanded in x around inf 73.8%
mul-1-neg73.8%
unsub-neg73.8%
Simplified73.8%
unpow273.8%
unpow273.8%
times-frac100.0%
Applied egg-rr100.0%
(FPCore (x y) :precision binary64 (if (<= x 4.1e+250) (fma x x (* y (- y))) (* (+ x y) (+ x y))))
double code(double x, double y) {
double tmp;
if (x <= 4.1e+250) {
tmp = fma(x, x, (y * -y));
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= 4.1e+250) 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, 4.1e+250], 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 4.1 \cdot 10^{+250}:\\
\;\;\;\;\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 < 4.09999999999999999e250Initial program 91.7%
sqr-neg91.7%
cancel-sign-sub91.7%
fma-define96.3%
Simplified96.3%
if 4.09999999999999999e250 < x Initial program 64.3%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt50.0%
sqrt-unprod92.9%
sqr-neg92.9%
sqrt-prod42.9%
add-sqr-sqrt92.9%
Applied egg-rr92.9%
(FPCore (x y) :precision binary64 (if (<= x 8e+132) (- (* x x) (* y y)) (* (+ x y) (+ x y))))
double code(double x, double y) {
double tmp;
if (x <= 8e+132) {
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 <= 8d+132) 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 <= 8e+132) {
tmp = (x * x) - (y * y);
} else {
tmp = (x + y) * (x + y);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 8e+132: tmp = (x * x) - (y * y) else: tmp = (x + y) * (x + y) return tmp
function code(x, y) tmp = 0.0 if (x <= 8e+132) 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 <= 8e+132) tmp = (x * x) - (y * y); else tmp = (x + y) * (x + y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 8e+132], 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 8 \cdot 10^{+132}:\\
\;\;\;\;x \cdot x - y \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(x + y\right) \cdot \left(x + y\right)\\
\end{array}
\end{array}
if x < 7.99999999999999993e132Initial program 94.4%
if 7.99999999999999993e132 < x Initial program 69.8%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt58.1%
sqrt-unprod93.0%
sqr-neg93.0%
sqrt-prod34.9%
add-sqr-sqrt83.7%
Applied egg-rr83.7%
(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 90.2%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt47.2%
sqrt-unprod72.2%
sqr-neg72.2%
sqrt-prod27.7%
add-sqr-sqrt52.8%
Applied egg-rr52.8%
herbie shell --seed 2024085
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))