
(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 8 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 (+ (pow (fma a a (* b b)) 2.0) (+ (* (* b b) 4.0) -1.0)))
double code(double a, double b) {
return pow(fma(a, a, (b * b)), 2.0) + (((b * b) * 4.0) + -1.0);
}
function code(a, b) return Float64((fma(a, a, Float64(b * b)) ^ 2.0) + Float64(Float64(Float64(b * b) * 4.0) + -1.0)) end
code[a_, b_] := N[(N[Power[N[(a * a + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(\mathsf{fma}\left(a, a, b \cdot b\right)\right)}^{2} + \left(\left(b \cdot b\right) \cdot 4 + -1\right)
\end{array}
Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Final simplification99.9%
(FPCore (a b) :precision binary64 (if (<= (* b b) 4e+87) (+ (+ (* (* b b) 4.0) -1.0) (pow a 4.0)) (pow b 4.0)))
double code(double a, double b) {
double tmp;
if ((b * b) <= 4e+87) {
tmp = (((b * b) * 4.0) + -1.0) + pow(a, 4.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 * b) <= 4d+87) then
tmp = (((b * b) * 4.0d0) + (-1.0d0)) + (a ** 4.0d0)
else
tmp = b ** 4.0d0
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((b * b) <= 4e+87) {
tmp = (((b * b) * 4.0) + -1.0) + Math.pow(a, 4.0);
} else {
tmp = Math.pow(b, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if (b * b) <= 4e+87: tmp = (((b * b) * 4.0) + -1.0) + math.pow(a, 4.0) else: tmp = math.pow(b, 4.0) return tmp
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 4e+87) tmp = Float64(Float64(Float64(Float64(b * b) * 4.0) + -1.0) + (a ^ 4.0)); else tmp = b ^ 4.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((b * b) <= 4e+87) tmp = (((b * b) * 4.0) + -1.0) + (a ^ 4.0); else tmp = b ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 4e+87], N[(N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + -1.0), $MachinePrecision] + N[Power[a, 4.0], $MachinePrecision]), $MachinePrecision], N[Power[b, 4.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 4 \cdot 10^{+87}:\\
\;\;\;\;\left(\left(b \cdot b\right) \cdot 4 + -1\right) + {a}^{4}\\
\mathbf{else}:\\
\;\;\;\;{b}^{4}\\
\end{array}
\end{array}
if (*.f64 b b) < 3.9999999999999998e87Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around inf 95.0%
if 3.9999999999999998e87 < (*.f64 b b) Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
expm1-log1p-u98.7%
associate-+r-98.7%
fma-define98.7%
*-commutative98.7%
add-exp-log98.7%
expm1-define98.7%
log1p-expm1-u98.7%
add-sqr-sqrt98.7%
pow298.7%
Applied egg-rr98.7%
Taylor expanded in b around inf 97.4%
Final simplification95.9%
(FPCore (a b) :precision binary64 (+ (+ (pow (+ (* b b) (* a a)) 2.0) (* (* b b) 4.0)) -1.0))
double code(double a, double b) {
return (pow(((b * b) + (a * a)), 2.0) + ((b * b) * 4.0)) + -1.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((((b * b) + (a * a)) ** 2.0d0) + ((b * b) * 4.0d0)) + (-1.0d0)
end function
public static double code(double a, double b) {
return (Math.pow(((b * b) + (a * a)), 2.0) + ((b * b) * 4.0)) + -1.0;
}
def code(a, b): return (math.pow(((b * b) + (a * a)), 2.0) + ((b * b) * 4.0)) + -1.0
function code(a, b) return Float64(Float64((Float64(Float64(b * b) + Float64(a * a)) ^ 2.0) + Float64(Float64(b * b) * 4.0)) + -1.0) end
function tmp = code(a, b) tmp = ((((b * b) + (a * a)) ^ 2.0) + ((b * b) * 4.0)) + -1.0; end
code[a_, b_] := N[(N[(N[Power[N[(N[(b * b), $MachinePrecision] + N[(a * a), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\left({\left(b \cdot b + a \cdot a\right)}^{2} + \left(b \cdot b\right) \cdot 4\right) + -1
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (a b) :precision binary64 (let* ((t_0 (+ (* (* b b) 4.0) -1.0))) (if (<= a 260000000000.0) (+ t_0 (pow b 4.0)) (+ t_0 (pow a 4.0)))))
double code(double a, double b) {
double t_0 = ((b * b) * 4.0) + -1.0;
double tmp;
if (a <= 260000000000.0) {
tmp = t_0 + pow(b, 4.0);
} else {
tmp = t_0 + pow(a, 4.0);
}
return tmp;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: t_0
real(8) :: tmp
t_0 = ((b * b) * 4.0d0) + (-1.0d0)
if (a <= 260000000000.0d0) then
tmp = t_0 + (b ** 4.0d0)
else
tmp = t_0 + (a ** 4.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double t_0 = ((b * b) * 4.0) + -1.0;
double tmp;
if (a <= 260000000000.0) {
tmp = t_0 + Math.pow(b, 4.0);
} else {
tmp = t_0 + Math.pow(a, 4.0);
}
return tmp;
}
def code(a, b): t_0 = ((b * b) * 4.0) + -1.0 tmp = 0 if a <= 260000000000.0: tmp = t_0 + math.pow(b, 4.0) else: tmp = t_0 + math.pow(a, 4.0) return tmp
function code(a, b) t_0 = Float64(Float64(Float64(b * b) * 4.0) + -1.0) tmp = 0.0 if (a <= 260000000000.0) tmp = Float64(t_0 + (b ^ 4.0)); else tmp = Float64(t_0 + (a ^ 4.0)); end return tmp end
function tmp_2 = code(a, b) t_0 = ((b * b) * 4.0) + -1.0; tmp = 0.0; if (a <= 260000000000.0) tmp = t_0 + (b ^ 4.0); else tmp = t_0 + (a ^ 4.0); end tmp_2 = tmp; end
code[a_, b_] := Block[{t$95$0 = N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[a, 260000000000.0], N[(t$95$0 + N[Power[b, 4.0], $MachinePrecision]), $MachinePrecision], N[(t$95$0 + N[Power[a, 4.0], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(b \cdot b\right) \cdot 4 + -1\\
\mathbf{if}\;a \leq 260000000000:\\
\;\;\;\;t\_0 + {b}^{4}\\
\mathbf{else}:\\
\;\;\;\;t\_0 + {a}^{4}\\
\end{array}
\end{array}
if a < 2.6e11Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around 0 78.8%
if 2.6e11 < a Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around inf 98.4%
Final simplification83.5%
(FPCore (a b) :precision binary64 (if (<= (* b b) 4e+87) (+ (pow a 4.0) -1.0) (pow b 4.0)))
double code(double a, double b) {
double tmp;
if ((b * b) <= 4e+87) {
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 * b) <= 4d+87) 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 * b) <= 4e+87) {
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 * b) <= 4e+87: 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 (Float64(b * b) <= 4e+87) 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 * b) <= 4e+87) tmp = (a ^ 4.0) + -1.0; else tmp = b ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 4e+87], 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 \cdot b \leq 4 \cdot 10^{+87}:\\
\;\;\;\;{a}^{4} + -1\\
\mathbf{else}:\\
\;\;\;\;{b}^{4}\\
\end{array}
\end{array}
if (*.f64 b b) < 3.9999999999999998e87Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in b around 0 94.5%
if 3.9999999999999998e87 < (*.f64 b b) Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
expm1-log1p-u98.7%
associate-+r-98.7%
fma-define98.7%
*-commutative98.7%
add-exp-log98.7%
expm1-define98.7%
log1p-expm1-u98.7%
add-sqr-sqrt98.7%
pow298.7%
Applied egg-rr98.7%
Taylor expanded in b around inf 97.4%
Final simplification95.7%
(FPCore (a b) :precision binary64 (if (<= a 98.0) (+ (* (* b b) 4.0) -1.0) (pow a 4.0)))
double code(double a, double b) {
double tmp;
if (a <= 98.0) {
tmp = ((b * b) * 4.0) + -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 <= 98.0d0) then
tmp = ((b * b) * 4.0d0) + (-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 <= 98.0) {
tmp = ((b * b) * 4.0) + -1.0;
} else {
tmp = Math.pow(a, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= 98.0: tmp = ((b * b) * 4.0) + -1.0 else: tmp = math.pow(a, 4.0) return tmp
function code(a, b) tmp = 0.0 if (a <= 98.0) tmp = Float64(Float64(Float64(b * b) * 4.0) + -1.0); else tmp = a ^ 4.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= 98.0) tmp = ((b * b) * 4.0) + -1.0; else tmp = a ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, 98.0], N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + -1.0), $MachinePrecision], N[Power[a, 4.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 98:\\
\;\;\;\;\left(b \cdot b\right) \cdot 4 + -1\\
\mathbf{else}:\\
\;\;\;\;{a}^{4}\\
\end{array}
\end{array}
if a < 98Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around 0 79.1%
Taylor expanded in b around 0 60.0%
pow260.0%
Applied egg-rr60.0%
if 98 < a Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
expm1-log1p-u98.1%
associate-+r-98.1%
fma-define98.1%
*-commutative98.1%
add-exp-log98.1%
expm1-define98.1%
log1p-expm1-u98.1%
add-sqr-sqrt98.1%
pow298.1%
Applied egg-rr98.1%
Taylor expanded in a around inf 95.5%
Final simplification68.7%
(FPCore (a b) :precision binary64 (+ (* (* b b) 4.0) -1.0))
double code(double a, double b) {
return ((b * b) * 4.0) + -1.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = ((b * b) * 4.0d0) + (-1.0d0)
end function
public static double code(double a, double b) {
return ((b * b) * 4.0) + -1.0;
}
def code(a, b): return ((b * b) * 4.0) + -1.0
function code(a, b) return Float64(Float64(Float64(b * b) * 4.0) + -1.0) end
function tmp = code(a, b) tmp = ((b * b) * 4.0) + -1.0; end
code[a_, b_] := N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\left(b \cdot b\right) \cdot 4 + -1
\end{array}
Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around 0 66.9%
Taylor expanded in b around 0 50.6%
pow250.6%
Applied egg-rr50.6%
Final simplification50.6%
(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%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around 0 66.9%
Taylor expanded in b around 0 24.1%
herbie shell --seed 2024106
(FPCore (a b)
:name "Bouland and Aaronson, Equation (26)"
:precision binary64
(- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (* b b))) 1.0))