
(FPCore (a b) :precision binary64 (- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (* b b))) 1.0))
double code(double a, double b) {
return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) - 1.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((((a * a) + (b * b)) ** 2.0d0) + (4.0d0 * (b * b))) - 1.0d0
end function
public static double code(double a, double b) {
return (Math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) - 1.0;
}
def code(a, b): return (math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) - 1.0
function code(a, b) return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(b * b))) - 1.0) end
function tmp = code(a, b) tmp = ((((a * a) + (b * b)) ^ 2.0) + (4.0 * (b * b))) - 1.0; end
code[a_, b_] := N[(N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(b \cdot b\right)\right) - 1
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (* b b))) 1.0))
double code(double a, double b) {
return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) - 1.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((((a * a) + (b * b)) ** 2.0d0) + (4.0d0 * (b * b))) - 1.0d0
end function
public static double code(double a, double b) {
return (Math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) - 1.0;
}
def code(a, b): return (math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) - 1.0
function code(a, b) return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(b * b))) - 1.0) end
function tmp = code(a, b) tmp = ((((a * a) + (b * b)) ^ 2.0) + (4.0 * (b * b))) - 1.0; end
code[a_, b_] := N[(N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(b \cdot b\right)\right) - 1
\end{array}
(FPCore (a b) :precision binary64 (fma (hypot a b) (* (hypot a b) (pow (hypot a b) 2.0)) (fma (pow b 2.0) 4.0 -1.0)))
double code(double a, double b) {
return fma(hypot(a, b), (hypot(a, b) * pow(hypot(a, b), 2.0)), fma(pow(b, 2.0), 4.0, -1.0));
}
function code(a, b) return fma(hypot(a, b), Float64(hypot(a, b) * (hypot(a, b) ^ 2.0)), fma((b ^ 2.0), 4.0, -1.0)) end
code[a_, b_] := N[(N[Sqrt[a ^ 2 + b ^ 2], $MachinePrecision] * N[(N[Sqrt[a ^ 2 + b ^ 2], $MachinePrecision] * N[Power[N[Sqrt[a ^ 2 + b ^ 2], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] + N[(N[Power[b, 2.0], $MachinePrecision] * 4.0 + -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\mathsf{hypot}\left(a, b\right), \mathsf{hypot}\left(a, b\right) \cdot {\left(\mathsf{hypot}\left(a, b\right)\right)}^{2}, \mathsf{fma}\left({b}^{2}, 4, -1\right)\right)
\end{array}
Initial program 99.9%
associate--l+99.9%
unpow299.9%
add-sqr-sqrt99.9%
associate-*l*99.9%
fma-define99.9%
hypot-define99.9%
hypot-define99.9%
add-sqr-sqrt99.9%
pow299.9%
hypot-define99.9%
*-commutative99.9%
fma-neg99.9%
pow299.9%
metadata-eval99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (a b)
:precision binary64
(if (<= a 2.2e-281)
(pow b 4.0)
(if (<= a 5.2e-231)
-1.0
(if (<= a 9e-115)
(pow b 4.0)
(if (<= a 7.5e-47)
-1.0
(if (<= a 1.16e+19) (pow b 4.0) (pow a 4.0)))))))
double code(double a, double b) {
double tmp;
if (a <= 2.2e-281) {
tmp = pow(b, 4.0);
} else if (a <= 5.2e-231) {
tmp = -1.0;
} else if (a <= 9e-115) {
tmp = pow(b, 4.0);
} else if (a <= 7.5e-47) {
tmp = -1.0;
} else if (a <= 1.16e+19) {
tmp = pow(b, 4.0);
} else {
tmp = pow(a, 4.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= 2.2d-281) then
tmp = b ** 4.0d0
else if (a <= 5.2d-231) then
tmp = -1.0d0
else if (a <= 9d-115) then
tmp = b ** 4.0d0
else if (a <= 7.5d-47) then
tmp = -1.0d0
else if (a <= 1.16d+19) then
tmp = b ** 4.0d0
else
tmp = a ** 4.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= 2.2e-281) {
tmp = Math.pow(b, 4.0);
} else if (a <= 5.2e-231) {
tmp = -1.0;
} else if (a <= 9e-115) {
tmp = Math.pow(b, 4.0);
} else if (a <= 7.5e-47) {
tmp = -1.0;
} else if (a <= 1.16e+19) {
tmp = Math.pow(b, 4.0);
} else {
tmp = Math.pow(a, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= 2.2e-281: tmp = math.pow(b, 4.0) elif a <= 5.2e-231: tmp = -1.0 elif a <= 9e-115: tmp = math.pow(b, 4.0) elif a <= 7.5e-47: tmp = -1.0 elif a <= 1.16e+19: tmp = math.pow(b, 4.0) else: tmp = math.pow(a, 4.0) return tmp
function code(a, b) tmp = 0.0 if (a <= 2.2e-281) tmp = b ^ 4.0; elseif (a <= 5.2e-231) tmp = -1.0; elseif (a <= 9e-115) tmp = b ^ 4.0; elseif (a <= 7.5e-47) tmp = -1.0; elseif (a <= 1.16e+19) tmp = b ^ 4.0; else tmp = a ^ 4.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= 2.2e-281) tmp = b ^ 4.0; elseif (a <= 5.2e-231) tmp = -1.0; elseif (a <= 9e-115) tmp = b ^ 4.0; elseif (a <= 7.5e-47) tmp = -1.0; elseif (a <= 1.16e+19) tmp = b ^ 4.0; else tmp = a ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, 2.2e-281], N[Power[b, 4.0], $MachinePrecision], If[LessEqual[a, 5.2e-231], -1.0, If[LessEqual[a, 9e-115], N[Power[b, 4.0], $MachinePrecision], If[LessEqual[a, 7.5e-47], -1.0, If[LessEqual[a, 1.16e+19], N[Power[b, 4.0], $MachinePrecision], N[Power[a, 4.0], $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 2.2 \cdot 10^{-281}:\\
\;\;\;\;{b}^{4}\\
\mathbf{elif}\;a \leq 5.2 \cdot 10^{-231}:\\
\;\;\;\;-1\\
\mathbf{elif}\;a \leq 9 \cdot 10^{-115}:\\
\;\;\;\;{b}^{4}\\
\mathbf{elif}\;a \leq 7.5 \cdot 10^{-47}:\\
\;\;\;\;-1\\
\mathbf{elif}\;a \leq 1.16 \cdot 10^{+19}:\\
\;\;\;\;{b}^{4}\\
\mathbf{else}:\\
\;\;\;\;{a}^{4}\\
\end{array}
\end{array}
if a < 2.20000000000000004e-281 or 5.20000000000000006e-231 < a < 9.00000000000000046e-115 or 7.49999999999999969e-47 < a < 1.16e19Initial program 99.8%
associate--l+99.8%
unpow299.8%
add-sqr-sqrt99.8%
associate-*l*99.9%
fma-define99.9%
hypot-define99.9%
hypot-define99.9%
add-sqr-sqrt99.9%
pow299.9%
hypot-define99.9%
*-commutative99.9%
fma-neg99.9%
pow299.9%
metadata-eval99.9%
Applied egg-rr99.9%
Taylor expanded in b around inf 53.4%
if 2.20000000000000004e-281 < a < 5.20000000000000006e-231 or 9.00000000000000046e-115 < a < 7.49999999999999969e-47Initial program 100.0%
Taylor expanded in b around 0 75.5%
Taylor expanded in a around 0 75.5%
if 1.16e19 < a Initial program 99.9%
associate--l+99.9%
unpow299.9%
add-sqr-sqrt99.9%
associate-*l*99.9%
fma-define99.9%
hypot-define99.9%
hypot-define99.9%
add-sqr-sqrt99.9%
pow299.9%
hypot-define99.9%
*-commutative99.9%
fma-neg99.9%
pow299.9%
metadata-eval99.9%
Applied egg-rr99.9%
Taylor expanded in a around inf 95.9%
Final simplification66.1%
(FPCore (a b) :precision binary64 (+ (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (* b b))) -1.0))
double code(double a, double b) {
return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) + -1.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((((a * a) + (b * b)) ** 2.0d0) + (4.0d0 * (b * b))) + (-1.0d0)
end function
public static double code(double a, double b) {
return (Math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) + -1.0;
}
def code(a, b): return (math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (b * b))) + -1.0
function code(a, b) return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(b * b))) + -1.0) end
function tmp = code(a, b) tmp = ((((a * a) + (b * b)) ^ 2.0) + (4.0 * (b * b))) + -1.0; end
code[a_, b_] := N[(N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(b \cdot b\right)\right) + -1
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (a b) :precision binary64 (if (<= a 3.45e+18) (+ (+ (* 4.0 (* b b)) (pow b 4.0)) -1.0) (+ (pow a 4.0) -1.0)))
double code(double a, double b) {
double tmp;
if (a <= 3.45e+18) {
tmp = ((4.0 * (b * b)) + pow(b, 4.0)) + -1.0;
} else {
tmp = pow(a, 4.0) + -1.0;
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= 3.45d+18) then
tmp = ((4.0d0 * (b * b)) + (b ** 4.0d0)) + (-1.0d0)
else
tmp = (a ** 4.0d0) + (-1.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= 3.45e+18) {
tmp = ((4.0 * (b * b)) + Math.pow(b, 4.0)) + -1.0;
} else {
tmp = Math.pow(a, 4.0) + -1.0;
}
return tmp;
}
def code(a, b): tmp = 0 if a <= 3.45e+18: tmp = ((4.0 * (b * b)) + math.pow(b, 4.0)) + -1.0 else: tmp = math.pow(a, 4.0) + -1.0 return tmp
function code(a, b) tmp = 0.0 if (a <= 3.45e+18) tmp = Float64(Float64(Float64(4.0 * Float64(b * b)) + (b ^ 4.0)) + -1.0); else tmp = Float64((a ^ 4.0) + -1.0); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= 3.45e+18) tmp = ((4.0 * (b * b)) + (b ^ 4.0)) + -1.0; else tmp = (a ^ 4.0) + -1.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, 3.45e+18], N[(N[(N[(4.0 * N[(b * b), $MachinePrecision]), $MachinePrecision] + N[Power[b, 4.0], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(N[Power[a, 4.0], $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 3.45 \cdot 10^{+18}:\\
\;\;\;\;\left(4 \cdot \left(b \cdot b\right) + {b}^{4}\right) + -1\\
\mathbf{else}:\\
\;\;\;\;{a}^{4} + -1\\
\end{array}
\end{array}
if a < 3.45e18Initial program 99.9%
Taylor expanded in a around 0 79.1%
if 3.45e18 < a Initial program 99.9%
Taylor expanded in b around 0 95.9%
Final simplification83.4%
(FPCore (a b) :precision binary64 (if (<= b 45000000000.0) (+ (pow a 4.0) -1.0) (pow b 4.0)))
double code(double a, double b) {
double tmp;
if (b <= 45000000000.0) {
tmp = pow(a, 4.0) + -1.0;
} else {
tmp = pow(b, 4.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (b <= 45000000000.0d0) then
tmp = (a ** 4.0d0) + (-1.0d0)
else
tmp = b ** 4.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 45000000000.0) {
tmp = Math.pow(a, 4.0) + -1.0;
} else {
tmp = Math.pow(b, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 45000000000.0: tmp = math.pow(a, 4.0) + -1.0 else: tmp = math.pow(b, 4.0) return tmp
function code(a, b) tmp = 0.0 if (b <= 45000000000.0) tmp = Float64((a ^ 4.0) + -1.0); else tmp = b ^ 4.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 45000000000.0) tmp = (a ^ 4.0) + -1.0; else tmp = b ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 45000000000.0], N[(N[Power[a, 4.0], $MachinePrecision] + -1.0), $MachinePrecision], N[Power[b, 4.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 45000000000:\\
\;\;\;\;{a}^{4} + -1\\
\mathbf{else}:\\
\;\;\;\;{b}^{4}\\
\end{array}
\end{array}
if b < 4.5e10Initial program 99.9%
Taylor expanded in b around 0 78.5%
if 4.5e10 < b Initial program 99.9%
associate--l+99.9%
unpow299.9%
add-sqr-sqrt99.9%
associate-*l*100.0%
fma-define100.0%
hypot-define100.0%
hypot-define100.0%
add-sqr-sqrt100.0%
pow2100.0%
hypot-define100.0%
*-commutative100.0%
fma-neg100.0%
pow2100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Taylor expanded in b around inf 93.1%
Final simplification81.7%
(FPCore (a b) :precision binary64 (if (<= a 1.0) -1.0 (pow a 4.0)))
double code(double a, double b) {
double tmp;
if (a <= 1.0) {
tmp = -1.0;
} else {
tmp = pow(a, 4.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: tmp
if (a <= 1.0d0) then
tmp = -1.0d0
else
tmp = a ** 4.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (a <= 1.0) {
tmp = -1.0;
} else {
tmp = Math.pow(a, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= 1.0: tmp = -1.0 else: tmp = math.pow(a, 4.0) return tmp
function code(a, b) tmp = 0.0 if (a <= 1.0) tmp = -1.0; else tmp = a ^ 4.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= 1.0) tmp = -1.0; else tmp = a ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, 1.0], -1.0, N[Power[a, 4.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 1:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;{a}^{4}\\
\end{array}
\end{array}
if a < 1Initial program 99.9%
Taylor expanded in b around 0 61.6%
Taylor expanded in a around 0 30.2%
if 1 < a Initial program 99.9%
associate--l+99.9%
unpow299.9%
add-sqr-sqrt99.9%
associate-*l*99.9%
fma-define99.9%
hypot-define99.9%
hypot-define99.9%
add-sqr-sqrt99.9%
pow299.9%
hypot-define99.9%
*-commutative99.9%
fma-neg99.9%
pow299.9%
metadata-eval99.9%
Applied egg-rr99.9%
Taylor expanded in a around inf 90.7%
Final simplification47.0%
(FPCore (a b) :precision binary64 -1.0)
double code(double a, double b) {
return -1.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = -1.0d0
end function
public static double code(double a, double b) {
return -1.0;
}
def code(a, b): return -1.0
function code(a, b) return -1.0 end
function tmp = code(a, b) tmp = -1.0; end
code[a_, b_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 99.9%
Taylor expanded in b around 0 69.7%
Taylor expanded in a around 0 22.0%
Final simplification22.0%
herbie shell --seed 2024077
(FPCore (a b)
:name "Bouland and Aaronson, Equation (26)"
:precision binary64
(- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (* b b))) 1.0))