
(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}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(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 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= y 4.6e-150) (* x (+ x (* y 2.0))) (* y (- y x))))
double code(double x, double y) {
double tmp;
if (y <= 4.6e-150) {
tmp = x * (x + (y * 2.0));
} else {
tmp = y * (y - x);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 4.6d-150) then
tmp = x * (x + (y * 2.0d0))
else
tmp = y * (y - x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 4.6e-150) {
tmp = x * (x + (y * 2.0));
} else {
tmp = y * (y - x);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 4.6e-150: tmp = x * (x + (y * 2.0)) else: tmp = y * (y - x) return tmp
function code(x, y) tmp = 0.0 if (y <= 4.6e-150) tmp = Float64(x * Float64(x + Float64(y * 2.0))); else tmp = Float64(y * Float64(y - x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 4.6e-150) tmp = x * (x + (y * 2.0)); else tmp = y * (y - x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 4.6e-150], N[(x * N[(x + N[(y * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y * N[(y - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 4.6 \cdot 10^{-150}:\\
\;\;\;\;x \cdot \left(x + y \cdot 2\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y - x\right)\\
\end{array}
\end{array}
if y < 4.60000000000000006e-150Initial program 100.0%
Taylor expanded in x around inf 66.6%
*-commutative66.6%
associate-*l*66.6%
unpow266.6%
distribute-lft-out67.8%
Simplified67.8%
if 4.60000000000000006e-150 < y Initial program 100.0%
flip-+93.4%
associate-*r/78.2%
pow278.2%
pow278.2%
Applied egg-rr78.2%
associate-/l*93.3%
+-commutative93.3%
Simplified93.3%
Taylor expanded in x around 0 74.5%
Applied egg-rr73.2%
Final simplification69.8%
(FPCore (x y) :precision binary64 (if (<= x -1e-310) (* x (- y)) (* 2.0 (* x y))))
double code(double x, double y) {
double tmp;
if (x <= -1e-310) {
tmp = x * -y;
} else {
tmp = 2.0 * (x * y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1d-310)) then
tmp = x * -y
else
tmp = 2.0d0 * (x * y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e-310) {
tmp = x * -y;
} else {
tmp = 2.0 * (x * y);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e-310: tmp = x * -y else: tmp = 2.0 * (x * y) return tmp
function code(x, y) tmp = 0.0 if (x <= -1e-310) tmp = Float64(x * Float64(-y)); else tmp = Float64(2.0 * Float64(x * y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e-310) tmp = x * -y; else tmp = 2.0 * (x * y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e-310], N[(x * (-y)), $MachinePrecision], N[(2.0 * N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-310}:\\
\;\;\;\;x \cdot \left(-y\right)\\
\mathbf{else}:\\
\;\;\;\;2 \cdot \left(x \cdot y\right)\\
\end{array}
\end{array}
if x < -9.999999999999969e-311Initial program 100.0%
flip-+93.3%
associate-*r/77.7%
pow277.7%
pow277.7%
Applied egg-rr77.7%
associate-/l*93.1%
+-commutative93.1%
Simplified93.1%
Taylor expanded in x around 0 52.7%
Applied egg-rr52.2%
Taylor expanded in y around 0 11.3%
neg-mul-111.3%
distribute-lft-neg-in11.3%
*-commutative11.3%
Simplified11.3%
if -9.999999999999969e-311 < x Initial program 100.0%
Taylor expanded in x around inf 52.4%
*-commutative52.4%
associate-*l*52.4%
unpow252.4%
distribute-lft-out53.8%
Simplified53.8%
Taylor expanded in x around 0 15.9%
Final simplification13.8%
(FPCore (x y) :precision binary64 (if (<= x -8.8e+220) (* x (- y)) (* y (+ x y))))
double code(double x, double y) {
double tmp;
if (x <= -8.8e+220) {
tmp = x * -y;
} 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 (x <= (-8.8d+220)) then
tmp = x * -y
else
tmp = y * (x + y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -8.8e+220) {
tmp = x * -y;
} else {
tmp = y * (x + y);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -8.8e+220: tmp = x * -y else: tmp = y * (x + y) return tmp
function code(x, y) tmp = 0.0 if (x <= -8.8e+220) tmp = Float64(x * Float64(-y)); else tmp = Float64(y * Float64(x + y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -8.8e+220) tmp = x * -y; else tmp = y * (x + y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -8.8e+220], N[(x * (-y)), $MachinePrecision], N[(y * N[(x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.8 \cdot 10^{+220}:\\
\;\;\;\;x \cdot \left(-y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(x + y\right)\\
\end{array}
\end{array}
if x < -8.79999999999999957e220Initial program 100.0%
flip-+77.8%
associate-*r/77.8%
pow277.8%
pow277.8%
Applied egg-rr77.8%
associate-/l*77.8%
+-commutative77.8%
Simplified77.8%
Taylor expanded in x around 0 18.3%
Applied egg-rr18.8%
Taylor expanded in y around 0 18.8%
neg-mul-118.8%
distribute-lft-neg-in18.8%
*-commutative18.8%
Simplified18.8%
if -8.79999999999999957e220 < x Initial program 100.0%
flip-+96.2%
associate-*r/80.2%
pow280.2%
pow280.2%
Applied egg-rr80.2%
associate-/l*96.1%
+-commutative96.1%
Simplified96.1%
Taylor expanded in x around 0 60.4%
associate-/r/60.5%
/-rgt-identity60.5%
+-commutative60.5%
Applied egg-rr60.5%
Final simplification57.6%
(FPCore (x y) :precision binary64 (if (<= y -8e-303) (* 2.0 (* x y)) (* y (- y x))))
double code(double x, double y) {
double tmp;
if (y <= -8e-303) {
tmp = 2.0 * (x * y);
} else {
tmp = y * (y - x);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-8d-303)) then
tmp = 2.0d0 * (x * y)
else
tmp = y * (y - x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -8e-303) {
tmp = 2.0 * (x * y);
} else {
tmp = y * (y - x);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -8e-303: tmp = 2.0 * (x * y) else: tmp = y * (y - x) return tmp
function code(x, y) tmp = 0.0 if (y <= -8e-303) tmp = Float64(2.0 * Float64(x * y)); else tmp = Float64(y * Float64(y - x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -8e-303) tmp = 2.0 * (x * y); else tmp = y * (y - x); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -8e-303], N[(2.0 * N[(x * y), $MachinePrecision]), $MachinePrecision], N[(y * N[(y - x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -8 \cdot 10^{-303}:\\
\;\;\;\;2 \cdot \left(x \cdot y\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot \left(y - x\right)\\
\end{array}
\end{array}
if y < -7.99999999999999944e-303Initial program 100.0%
Taylor expanded in x around inf 60.7%
*-commutative60.7%
associate-*l*60.7%
unpow260.7%
distribute-lft-out62.1%
Simplified62.1%
Taylor expanded in x around 0 15.4%
if -7.99999999999999944e-303 < y Initial program 100.0%
flip-+94.8%
associate-*r/80.0%
pow280.0%
pow280.0%
Applied egg-rr80.0%
associate-/l*94.7%
+-commutative94.7%
Simplified94.7%
Taylor expanded in x around 0 63.5%
Applied egg-rr62.7%
Final simplification36.9%
(FPCore (x y) :precision binary64 (if (<= x -1e-310) (* x (- y)) (* x y)))
double code(double x, double y) {
double tmp;
if (x <= -1e-310) {
tmp = x * -y;
} else {
tmp = x * y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1d-310)) then
tmp = x * -y
else
tmp = x * y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e-310) {
tmp = x * -y;
} else {
tmp = x * y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e-310: tmp = x * -y else: tmp = x * y return tmp
function code(x, y) tmp = 0.0 if (x <= -1e-310) tmp = Float64(x * Float64(-y)); else tmp = Float64(x * y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e-310) tmp = x * -y; else tmp = x * y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e-310], N[(x * (-y)), $MachinePrecision], N[(x * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-310}:\\
\;\;\;\;x \cdot \left(-y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if x < -9.999999999999969e-311Initial program 100.0%
flip-+93.3%
associate-*r/77.7%
pow277.7%
pow277.7%
Applied egg-rr77.7%
associate-/l*93.1%
+-commutative93.1%
Simplified93.1%
Taylor expanded in x around 0 52.7%
Applied egg-rr52.2%
Taylor expanded in y around 0 11.3%
neg-mul-111.3%
distribute-lft-neg-in11.3%
*-commutative11.3%
Simplified11.3%
if -9.999999999999969e-311 < x Initial program 100.0%
flip-+96.3%
associate-*r/82.1%
pow282.1%
pow282.1%
Applied egg-rr82.1%
associate-/l*96.2%
+-commutative96.2%
Simplified96.2%
Taylor expanded in x around 0 61.6%
Taylor expanded in y around 0 15.9%
*-commutative15.9%
Simplified15.9%
Final simplification13.8%
(FPCore (x y) :precision binary64 (* x y))
double code(double x, double y) {
return x * y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * y
end function
public static double code(double x, double y) {
return x * y;
}
def code(x, y): return x * y
function code(x, y) return Float64(x * y) end
function tmp = code(x, y) tmp = x * y; end
code[x_, y_] := N[(x * y), $MachinePrecision]
\begin{array}{l}
\\
x \cdot y
\end{array}
Initial program 100.0%
flip-+94.9%
associate-*r/80.1%
pow280.1%
pow280.1%
Applied egg-rr80.1%
associate-/l*94.8%
+-commutative94.8%
Simplified94.8%
Taylor expanded in x around 0 57.4%
Taylor expanded in y around 0 14.3%
*-commutative14.3%
Simplified14.3%
Final simplification14.3%
(FPCore (x y) :precision binary64 (+ (* x x) (+ (* y y) (* 2.0 (* y x)))))
double code(double x, double y) {
return (x * x) + ((y * y) + (2.0 * (y * x)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x * x) + ((y * y) + (2.0d0 * (y * x)))
end function
public static double code(double x, double y) {
return (x * x) + ((y * y) + (2.0 * (y * x)));
}
def code(x, y): return (x * x) + ((y * y) + (2.0 * (y * x)))
function code(x, y) return Float64(Float64(x * x) + Float64(Float64(y * y) + Float64(2.0 * Float64(y * x)))) end
function tmp = code(x, y) tmp = (x * x) + ((y * y) + (2.0 * (y * x))); end
code[x_, y_] := N[(N[(x * x), $MachinePrecision] + N[(N[(y * y), $MachinePrecision] + N[(2.0 * N[(y * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot x + \left(y \cdot y + 2 \cdot \left(y \cdot x\right)\right)
\end{array}
herbie shell --seed 2024014
(FPCore (x y)
:name "Examples.Basics.BasicTests:f3 from sbv-4.4"
:precision binary64
:herbie-target
(+ (* x x) (+ (* y y) (* 2.0 (* y x))))
(* (+ x y) (+ x y)))