
(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 93.0%
add-sqr-sqrt93.0%
sqrt-unprod77.5%
pow277.5%
pow277.5%
pow-prod-up77.4%
metadata-eval77.4%
Applied egg-rr77.4%
sqrt-pow193.0%
metadata-eval93.0%
unpow293.0%
difference-of-squares100.0%
+-commutative100.0%
Applied egg-rr100.0%
(FPCore (x y) :precision binary64 (if (<= (* y y) 4e-123) (* x x) (* y (- y))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 4e-123) {
tmp = x * x;
} else {
tmp = y * -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) <= 4d-123) then
tmp = x * x
else
tmp = y * -y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y * y) <= 4e-123) {
tmp = x * x;
} else {
tmp = y * -y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 4e-123: tmp = x * x else: tmp = y * -y return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 4e-123) tmp = Float64(x * x); else tmp = Float64(y * Float64(-y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y * y) <= 4e-123) tmp = x * x; else tmp = y * -y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 4e-123], N[(x * x), $MachinePrecision], N[(y * (-y)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq 4 \cdot 10^{-123}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 4.0000000000000002e-123Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.9%
sqrt-unprod93.7%
sqr-neg93.7%
sqrt-prod47.8%
add-sqr-sqrt88.3%
Applied egg-rr88.3%
Taylor expanded in x around inf 88.6%
Taylor expanded in x around inf 88.9%
if 4.0000000000000002e-123 < (*.f64 y y) Initial program 87.6%
Taylor expanded in x around 0 75.8%
neg-mul-175.8%
Simplified75.8%
unpow275.8%
distribute-lft-neg-in75.8%
Applied egg-rr75.8%
Final simplification81.5%
(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 93.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt47.9%
sqrt-unprod75.1%
sqr-neg75.1%
sqrt-prod27.1%
add-sqr-sqrt51.4%
Applied egg-rr51.4%
Taylor expanded in x around inf 57.0%
Taylor expanded in x around inf 52.2%
herbie shell --seed 2024157
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))