
(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 (* (- x y) (+ y x)))
double code(double x, double y) {
return (x - y) * (y + x);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x - y) * (y + x)
end function
public static double code(double x, double y) {
return (x - y) * (y + x);
}
def code(x, y): return (x - y) * (y + x)
function code(x, y) return Float64(Float64(x - y) * Float64(y + x)) end
function tmp = code(x, y) tmp = (x - y) * (y + x); end
code[x_, y_] := N[(N[(x - y), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x - y\right) \cdot \left(y + x\right)
\end{array}
Initial program 93.8%
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
difference-of-squaresN/A
*-commutativeN/A
lower-*.f64N/A
lower--.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (- (* x x) (* y y)))) (if (or (<= t_0 -5e-310) (not (<= t_0 INFINITY))) (* (- y) y) (* x x))))
double code(double x, double y) {
double t_0 = (x * x) - (y * y);
double tmp;
if ((t_0 <= -5e-310) || !(t_0 <= ((double) INFINITY))) {
tmp = -y * y;
} else {
tmp = x * x;
}
return tmp;
}
public static double code(double x, double y) {
double t_0 = (x * x) - (y * y);
double tmp;
if ((t_0 <= -5e-310) || !(t_0 <= Double.POSITIVE_INFINITY)) {
tmp = -y * y;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): t_0 = (x * x) - (y * y) tmp = 0 if (t_0 <= -5e-310) or not (t_0 <= math.inf): tmp = -y * y else: tmp = x * x return tmp
function code(x, y) t_0 = Float64(Float64(x * x) - Float64(y * y)) tmp = 0.0 if ((t_0 <= -5e-310) || !(t_0 <= Inf)) tmp = Float64(Float64(-y) * y); else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) t_0 = (x * x) - (y * y); tmp = 0.0; if ((t_0 <= -5e-310) || ~((t_0 <= Inf))) tmp = -y * y; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -5e-310], N[Not[LessEqual[t$95$0, Infinity]], $MachinePrecision]], N[((-y) * y), $MachinePrecision], N[(x * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot x - y \cdot y\\
\mathbf{if}\;t\_0 \leq -5 \cdot 10^{-310} \lor \neg \left(t\_0 \leq \infty\right):\\
\;\;\;\;\left(-y\right) \cdot y\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if (-.f64 (*.f64 x x) (*.f64 y y)) < -4.999999999999985e-310 or +inf.0 < (-.f64 (*.f64 x x) (*.f64 y y)) Initial program 88.3%
Taylor expanded in x around 0
mul-1-negN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6496.3
Applied rewrites96.3%
if -4.999999999999985e-310 < (-.f64 (*.f64 x x) (*.f64 y y)) < +inf.0Initial program 100.0%
Taylor expanded in x around 0
mul-1-negN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6410.1
Applied rewrites10.1%
Taylor expanded in x around inf
unpow2N/A
lower-*.f6499.8
Applied rewrites99.8%
Final simplification97.9%
(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.8%
Taylor expanded in x around 0
mul-1-negN/A
unpow2N/A
distribute-lft-neg-inN/A
lower-*.f64N/A
lower-neg.f6456.2
Applied rewrites56.2%
Taylor expanded in x around inf
unpow2N/A
lower-*.f6448.9
Applied rewrites48.9%
herbie shell --seed 2024320
(FPCore (x y)
:name "Examples.Basics.BasicTests:f2 from sbv-4.4"
:precision binary64
(- (* x x) (* y y)))