
(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 3 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 (* (+ y x) (- x y)))
double code(double x, double y) {
return (y + x) * (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y + x) * (x - y)
end function
public static double code(double x, double y) {
return (y + x) * (x - y);
}
def code(x, y): return (y + x) * (x - y)
function code(x, y) return Float64(Float64(y + x) * Float64(x - y)) end
function tmp = code(x, y) tmp = (y + x) * (x - y); end
code[x_, y_] := N[(N[(y + x), $MachinePrecision] * N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(y + x\right) \cdot \left(x - y\right)
\end{array}
Initial program 94.9%
add-sqr-sqrt49.1%
associate-*l*49.1%
prod-diff40.1%
Applied egg-rr40.1%
prod-diff49.1%
associate-*r*49.1%
add-sqr-sqrt94.9%
difference-of-squares100.0%
+-commutative100.0%
Applied egg-rr100.0%
(FPCore (x y) :precision binary64 (if (<= (* x x) 5e-161) (* y (- y)) (* x x)))
double code(double x, double y) {
double tmp;
if ((x * x) <= 5e-161) {
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) <= 5d-161) 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) <= 5e-161) {
tmp = y * -y;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x * x) <= 5e-161: tmp = y * -y else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if (Float64(x * x) <= 5e-161) 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) <= 5e-161) tmp = y * -y; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(x * x), $MachinePrecision], 5e-161], N[(y * (-y)), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 5 \cdot 10^{-161}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 4.9999999999999999e-161Initial program 100.0%
Taylor expanded in x around 0 93.0%
neg-mul-193.0%
Simplified93.0%
unpow293.0%
distribute-lft-neg-in93.0%
Applied egg-rr93.0%
if 4.9999999999999999e-161 < (*.f64 x x) Initial program 92.1%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.4%
sqrt-unprod83.1%
sqr-neg83.1%
sqrt-prod40.0%
add-sqr-sqrt77.1%
Applied egg-rr77.1%
Taylor expanded in x around inf 80.5%
Taylor expanded in x around inf 77.6%
Final simplification83.1%
(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 94.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt47.6%
sqrt-unprod74.5%
sqr-neg74.5%
sqrt-prod28.4%
add-sqr-sqrt54.5%
Applied egg-rr54.5%
Taylor expanded in x around inf 57.3%
Taylor expanded in x around inf 55.3%
herbie shell --seed 2024170
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))