
(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 (* (- 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 95.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt55.4%
sqrt-unprod76.8%
sqr-neg76.8%
sqrt-prod22.5%
add-sqr-sqrt49.6%
Applied egg-rr49.6%
add-sqr-sqrt22.5%
sqrt-prod76.8%
add-sqr-sqrt21.8%
add-sqr-sqrt21.8%
sqr-neg21.8%
swap-sqr21.8%
sqrt-unprod0.0%
add-sqr-sqrt44.4%
distribute-rgt-neg-out44.4%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
(FPCore (x y) :precision binary64 (if (<= (* y y) 2e+57) (* x x) (* y (- x y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 2e+57) {
tmp = x * x;
} else {
tmp = 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 ((y * y) <= 2d+57) then
tmp = x * x
else
tmp = y * (x - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 2e+57) {
tmp = x * x;
} else {
tmp = y * (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 2e+57: tmp = x * x else: tmp = y * (x - y) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 2e+57) tmp = Float64(x * x); else tmp = Float64(y * Float64(x - y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 2e+57) tmp = x * x; else tmp = y * (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e+57], N[(x * x), $MachinePrecision], N[(y * N[(x - y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 2 \cdot 10^{+57}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x - y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 2.0000000000000001e57Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt57.3%
sqrt-unprod90.6%
sqr-neg90.6%
sqrt-prod33.3%
add-sqr-sqrt78.2%
Applied egg-rr78.2%
Taylor expanded in x around inf 78.6%
Taylor expanded in x around inf 78.9%
if 2.0000000000000001e57 < (*.f64 y y) Initial program 90.8%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt53.2%
sqrt-unprod61.2%
sqr-neg61.2%
sqrt-prod10.4%
add-sqr-sqrt17.2%
Applied egg-rr17.2%
add-sqr-sqrt10.4%
sqrt-prod61.2%
add-sqr-sqrt8.7%
add-sqr-sqrt8.7%
sqr-neg8.7%
swap-sqr8.7%
sqrt-unprod0.0%
add-sqr-sqrt46.5%
distribute-rgt-neg-out46.5%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 86.6%
Final simplification82.5%
(FPCore (x y) :precision binary64 (if (<= (* x x) 4e+192) (* y (- y)) (* x x)))
double code(double x, double y) {
double tmp;
if ((x * x) <= 4e+192) {
tmp = 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) <= 4d+192) then
tmp = 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) <= 4e+192) {
tmp = y * -y;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 4e+192: tmp = y * -y else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 4e+192) tmp = Float64(y * Float64(-y)); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x * x) <= 4e+192) tmp = y * -y; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 4e+192], N[(y * (-y)), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 4 \cdot 10^{+192}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 4.00000000000000016e192Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt56.8%
sqrt-unprod69.6%
sqr-neg69.6%
sqrt-prod12.6%
add-sqr-sqrt29.0%
Applied egg-rr29.0%
add-sqr-sqrt12.6%
sqrt-prod69.6%
add-sqr-sqrt12.6%
add-sqr-sqrt12.6%
sqr-neg12.6%
swap-sqr12.6%
sqrt-unprod0.0%
add-sqr-sqrt42.9%
distribute-rgt-neg-out42.9%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 75.0%
Taylor expanded in x around 0 75.2%
neg-mul-175.2%
Simplified75.2%
if 4.00000000000000016e192 < (*.f64 x x) Initial program 86.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt52.4%
sqrt-unprod91.7%
sqr-neg91.7%
sqrt-prod42.9%
add-sqr-sqrt91.7%
Applied egg-rr91.7%
Taylor expanded in x around inf 95.4%
Taylor expanded in x around inf 91.7%
Final simplification80.6%
(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 95.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt55.4%
sqrt-unprod76.8%
sqr-neg76.8%
sqrt-prod22.5%
add-sqr-sqrt49.6%
Applied egg-rr49.6%
Taylor expanded in x around inf 52.3%
Taylor expanded in x around inf 50.4%
herbie shell --seed 2024139
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))