
(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 94.1%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt48.3%
sqrt-unprod75.5%
sqr-neg75.5%
sqrt-prod28.2%
add-sqr-sqrt55.2%
Applied egg-rr55.2%
add-sqr-sqrt28.2%
sqrt-prod75.5%
add-sqr-sqrt28.2%
add-sqr-sqrt28.2%
sqr-neg28.2%
swap-sqr28.2%
sqrt-unprod0.0%
add-sqr-sqrt51.4%
distribute-rgt-neg-out51.4%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
(FPCore (x y)
:precision binary64
(if (<= (* y y) 2e-185)
(* x x)
(if (<= (* y y) 2e-133)
(* y (- y))
(if (<= (* y y) 2e-61) (* x x) (* y (- x y))))))
double code(double x, double y) {
double tmp;
if ((y * y) <= 2e-185) {
tmp = x * x;
} else if ((y * y) <= 2e-133) {
tmp = y * -y;
} else if ((y * y) <= 2e-61) {
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-185) then
tmp = x * x
else if ((y * y) <= 2d-133) then
tmp = y * -y
else if ((y * y) <= 2d-61) 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-185) {
tmp = x * x;
} else if ((y * y) <= 2e-133) {
tmp = y * -y;
} else if ((y * y) <= 2e-61) {
tmp = x * x;
} else {
tmp = y * (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if (y * y) <= 2e-185: tmp = x * x elif (y * y) <= 2e-133: tmp = y * -y elif (y * y) <= 2e-61: tmp = x * x else: tmp = y * (x - y) return tmp
function code(x, y) tmp = 0.0 if (Float64(y * y) <= 2e-185) tmp = Float64(x * x); elseif (Float64(y * y) <= 2e-133) tmp = Float64(y * Float64(-y)); elseif (Float64(y * y) <= 2e-61) 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-185) tmp = x * x; elseif ((y * y) <= 2e-133) tmp = y * -y; elseif ((y * y) <= 2e-61) tmp = x * x; else tmp = y * (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], 2e-185], N[(x * x), $MachinePrecision], If[LessEqual[N[(y * y), $MachinePrecision], 2e-133], N[(y * (-y)), $MachinePrecision], If[LessEqual[N[(y * y), $MachinePrecision], 2e-61], 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^{-185}:\\
\;\;\;\;x \cdot x\\
\mathbf{elif}\;y \cdot y \leq 2 \cdot 10^{-133}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\mathbf{elif}\;y \cdot y \leq 2 \cdot 10^{-61}:\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x - y\right)\\
\end{array}
\end{array}
if (*.f64 y y) < 2e-185 or 2.0000000000000001e-133 < (*.f64 y y) < 2.0000000000000001e-61Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt45.1%
sqrt-unprod93.7%
sqr-neg93.7%
sqrt-prod48.6%
add-sqr-sqrt91.2%
Applied egg-rr91.2%
Taylor expanded in x around inf 91.4%
Taylor expanded in x around inf 91.9%
if 2e-185 < (*.f64 y y) < 2.0000000000000001e-133Initial program 99.9%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt43.4%
sqrt-unprod57.7%
sqr-neg57.7%
sqrt-prod13.9%
add-sqr-sqrt27.4%
Applied egg-rr27.4%
add-sqr-sqrt13.9%
sqrt-prod57.7%
add-sqr-sqrt13.9%
add-sqr-sqrt13.9%
sqr-neg13.9%
swap-sqr13.9%
sqrt-unprod0.0%
add-sqr-sqrt55.8%
distribute-rgt-neg-out55.8%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 72.5%
Taylor expanded in x around 0 74.2%
neg-mul-174.2%
Simplified74.2%
if 2.0000000000000001e-61 < (*.f64 y y) Initial program 88.2%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt51.8%
sqrt-unprod61.5%
sqr-neg61.5%
sqrt-prod11.9%
add-sqr-sqrt26.8%
Applied egg-rr26.8%
add-sqr-sqrt11.9%
sqrt-prod61.5%
add-sqr-sqrt11.9%
add-sqr-sqrt11.9%
sqr-neg11.9%
swap-sqr11.9%
sqrt-unprod0.0%
add-sqr-sqrt47.9%
distribute-rgt-neg-out47.9%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 78.8%
Final simplification84.3%
(FPCore (x y)
:precision binary64
(if (or (<= (* x x) 6e-72)
(and (not (<= (* x x) 1.5e+61)) (<= (* x x) 1e+167)))
(* y (- y))
(* x x)))
double code(double x, double y) {
double tmp;
if (((x * x) <= 6e-72) || (!((x * x) <= 1.5e+61) && ((x * x) <= 1e+167))) {
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) <= 6d-72) .or. (.not. ((x * x) <= 1.5d+61)) .and. ((x * x) <= 1d+167)) 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) <= 6e-72) || (!((x * x) <= 1.5e+61) && ((x * x) <= 1e+167))) {
tmp = y * -y;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if ((x * x) <= 6e-72) or (not ((x * x) <= 1.5e+61) and ((x * x) <= 1e+167)): tmp = y * -y else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if ((Float64(x * x) <= 6e-72) || (!(Float64(x * x) <= 1.5e+61) && (Float64(x * x) <= 1e+167))) 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) <= 6e-72) || (~(((x * x) <= 1.5e+61)) && ((x * x) <= 1e+167))) tmp = y * -y; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[N[(x * x), $MachinePrecision], 6e-72], And[N[Not[LessEqual[N[(x * x), $MachinePrecision], 1.5e+61]], $MachinePrecision], LessEqual[N[(x * x), $MachinePrecision], 1e+167]]], N[(y * (-y)), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 6 \cdot 10^{-72} \lor \neg \left(x \cdot x \leq 1.5 \cdot 10^{+61}\right) \land x \cdot x \leq 10^{+167}:\\
\;\;\;\;y \cdot \left(-y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (*.f64 x x) < 6e-72 or 1.5e61 < (*.f64 x x) < 1e167Initial program 100.0%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt46.1%
sqrt-unprod65.1%
sqr-neg65.1%
sqrt-prod18.8%
add-sqr-sqrt29.9%
Applied egg-rr29.9%
add-sqr-sqrt18.8%
sqrt-prod65.1%
add-sqr-sqrt18.8%
add-sqr-sqrt18.8%
sqr-neg18.8%
swap-sqr18.8%
sqrt-unprod0.0%
add-sqr-sqrt53.5%
distribute-rgt-neg-out53.5%
add-sqr-sqrt100.0%
sub-neg100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 83.2%
Taylor expanded in x around 0 83.7%
neg-mul-183.7%
Simplified83.7%
if 6e-72 < (*.f64 x x) < 1.5e61 or 1e167 < (*.f64 x x) Initial program 87.7%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt50.8%
sqrt-unprod86.9%
sqr-neg86.9%
sqrt-prod38.5%
add-sqr-sqrt83.0%
Applied egg-rr83.0%
Taylor expanded in x around inf 89.2%
Taylor expanded in x around inf 83.7%
Final simplification83.7%
(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.1%
difference-of-squares100.0%
sub-neg100.0%
add-sqr-sqrt48.3%
sqrt-unprod75.5%
sqr-neg75.5%
sqrt-prod28.2%
add-sqr-sqrt55.2%
Applied egg-rr55.2%
Taylor expanded in x around inf 59.1%
Taylor expanded in x around inf 56.1%
herbie shell --seed 2024110
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))