
(FPCore (a b) :precision binary64 (- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))
double code(double a, double b) {
return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 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 * (((a * a) * (1.0d0 + a)) + ((b * b) * (1.0d0 - (3.0d0 * a)))))) - 1.0d0
end function
public static double code(double a, double b) {
return (Math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0;
}
def code(a, b): return (math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0
function code(a, b) return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(Float64(Float64(a * a) * Float64(1.0 + a)) + Float64(Float64(b * b) * Float64(1.0 - Float64(3.0 * a)))))) - 1.0) end
function tmp = code(a, b) tmp = ((((a * a) + (b * b)) ^ 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 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[(N[(N[(a * a), $MachinePrecision] * N[(1.0 + a), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * N[(1.0 - N[(3.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))
double code(double a, double b) {
return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 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 * (((a * a) * (1.0d0 + a)) + ((b * b) * (1.0d0 - (3.0d0 * a)))))) - 1.0d0
end function
public static double code(double a, double b) {
return (Math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0;
}
def code(a, b): return (math.pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0
function code(a, b) return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(Float64(Float64(a * a) * Float64(1.0 + a)) + Float64(Float64(b * b) * Float64(1.0 - Float64(3.0 * a)))))) - 1.0) end
function tmp = code(a, b) tmp = ((((a * a) + (b * b)) ^ 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 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[(N[(N[(a * a), $MachinePrecision] * N[(1.0 + a), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * N[(1.0 - N[(3.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
\begin{array}{l}
\\
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1
\end{array}
(FPCore (a b) :precision binary64 (let* ((t_0 (+ (* a a) (* b b)))) (+ (+ (pow t_0 2.0) (* 4.0 t_0)) -1.0)))
double code(double a, double b) {
double t_0 = (a * a) + (b * b);
return (pow(t_0, 2.0) + (4.0 * t_0)) + -1.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8) :: t_0
t_0 = (a * a) + (b * b)
code = ((t_0 ** 2.0d0) + (4.0d0 * t_0)) + (-1.0d0)
end function
public static double code(double a, double b) {
double t_0 = (a * a) + (b * b);
return (Math.pow(t_0, 2.0) + (4.0 * t_0)) + -1.0;
}
def code(a, b): t_0 = (a * a) + (b * b) return (math.pow(t_0, 2.0) + (4.0 * t_0)) + -1.0
function code(a, b) t_0 = Float64(Float64(a * a) + Float64(b * b)) return Float64(Float64((t_0 ^ 2.0) + Float64(4.0 * t_0)) + -1.0) end
function tmp = code(a, b) t_0 = (a * a) + (b * b); tmp = ((t_0 ^ 2.0) + (4.0 * t_0)) + -1.0; end
code[a_, b_] := Block[{t$95$0 = N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[Power[t$95$0, 2.0], $MachinePrecision] + N[(4.0 * t$95$0), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := a \cdot a + b \cdot b\\
\left({t\_0}^{2} + 4 \cdot t\_0\right) + -1
\end{array}
\end{array}
Initial program 73.3%
add-sqr-sqrt40.2%
sqrt-unprod60.5%
swap-sqr60.5%
metadata-eval60.5%
metadata-eval60.5%
swap-sqr60.5%
sqrt-unprod30.8%
add-sqr-sqrt84.7%
*-commutative84.7%
cancel-sign-sub-inv84.7%
Applied egg-rr84.7%
Taylor expanded in a around 0 91.3%
Taylor expanded in a around 0 91.3%
Taylor expanded in a around 0 99.9%
Final simplification99.9%
(FPCore (a b) :precision binary64 (if (or (<= a -4e+16) (not (<= a 8e+20))) (pow a 4.0) (+ (+ (* (* b b) 4.0) (pow b 4.0)) -1.0)))
double code(double a, double b) {
double tmp;
if ((a <= -4e+16) || !(a <= 8e+20)) {
tmp = pow(a, 4.0);
} else {
tmp = (((b * b) * 4.0) + pow(b, 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 <= (-4d+16)) .or. (.not. (a <= 8d+20))) then
tmp = a ** 4.0d0
else
tmp = (((b * b) * 4.0d0) + (b ** 4.0d0)) + (-1.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((a <= -4e+16) || !(a <= 8e+20)) {
tmp = Math.pow(a, 4.0);
} else {
tmp = (((b * b) * 4.0) + Math.pow(b, 4.0)) + -1.0;
}
return tmp;
}
def code(a, b): tmp = 0 if (a <= -4e+16) or not (a <= 8e+20): tmp = math.pow(a, 4.0) else: tmp = (((b * b) * 4.0) + math.pow(b, 4.0)) + -1.0 return tmp
function code(a, b) tmp = 0.0 if ((a <= -4e+16) || !(a <= 8e+20)) tmp = a ^ 4.0; else tmp = Float64(Float64(Float64(Float64(b * b) * 4.0) + (b ^ 4.0)) + -1.0); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((a <= -4e+16) || ~((a <= 8e+20))) tmp = a ^ 4.0; else tmp = (((b * b) * 4.0) + (b ^ 4.0)) + -1.0; end tmp_2 = tmp; end
code[a_, b_] := If[Or[LessEqual[a, -4e+16], N[Not[LessEqual[a, 8e+20]], $MachinePrecision]], N[Power[a, 4.0], $MachinePrecision], N[(N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + N[Power[b, 4.0], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -4 \cdot 10^{+16} \lor \neg \left(a \leq 8 \cdot 10^{+20}\right):\\
\;\;\;\;{a}^{4}\\
\mathbf{else}:\\
\;\;\;\;\left(\left(b \cdot b\right) \cdot 4 + {b}^{4}\right) + -1\\
\end{array}
\end{array}
if a < -4e16 or 8e20 < a Initial program 50.7%
associate--l+50.7%
+-commutative50.7%
+-commutative50.7%
sub-neg50.7%
associate-+l+50.7%
+-commutative50.7%
fma-define50.7%
Simplified59.3%
Taylor expanded in a around inf 92.5%
associate-*r/92.5%
metadata-eval92.5%
Simplified92.5%
Taylor expanded in a around inf 92.5%
if -4e16 < a < 8e20Initial program 96.0%
associate--l+96.0%
+-commutative96.0%
+-commutative96.0%
sub-neg96.0%
associate-+l+96.0%
+-commutative96.0%
fma-define96.0%
Simplified96.0%
Taylor expanded in a around 0 99.7%
pow299.7%
Applied egg-rr99.7%
Final simplification96.1%
(FPCore (a b) :precision binary64 (if (or (<= a -2.9e+19) (not (<= a 2.3e+20))) (pow a 4.0) (+ (* (* b b) 4.0) -1.0)))
double code(double a, double b) {
double tmp;
if ((a <= -2.9e+19) || !(a <= 2.3e+20)) {
tmp = pow(a, 4.0);
} else {
tmp = ((b * b) * 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 <= (-2.9d+19)) .or. (.not. (a <= 2.3d+20))) then
tmp = a ** 4.0d0
else
tmp = ((b * b) * 4.0d0) + (-1.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((a <= -2.9e+19) || !(a <= 2.3e+20)) {
tmp = Math.pow(a, 4.0);
} else {
tmp = ((b * b) * 4.0) + -1.0;
}
return tmp;
}
def code(a, b): tmp = 0 if (a <= -2.9e+19) or not (a <= 2.3e+20): tmp = math.pow(a, 4.0) else: tmp = ((b * b) * 4.0) + -1.0 return tmp
function code(a, b) tmp = 0.0 if ((a <= -2.9e+19) || !(a <= 2.3e+20)) tmp = a ^ 4.0; else tmp = Float64(Float64(Float64(b * b) * 4.0) + -1.0); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((a <= -2.9e+19) || ~((a <= 2.3e+20))) tmp = a ^ 4.0; else tmp = ((b * b) * 4.0) + -1.0; end tmp_2 = tmp; end
code[a_, b_] := If[Or[LessEqual[a, -2.9e+19], N[Not[LessEqual[a, 2.3e+20]], $MachinePrecision]], N[Power[a, 4.0], $MachinePrecision], N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq -2.9 \cdot 10^{+19} \lor \neg \left(a \leq 2.3 \cdot 10^{+20}\right):\\
\;\;\;\;{a}^{4}\\
\mathbf{else}:\\
\;\;\;\;\left(b \cdot b\right) \cdot 4 + -1\\
\end{array}
\end{array}
if a < -2.9e19 or 2.3e20 < a Initial program 50.7%
associate--l+50.7%
+-commutative50.7%
+-commutative50.7%
sub-neg50.7%
associate-+l+50.7%
+-commutative50.7%
fma-define50.7%
Simplified59.3%
Taylor expanded in a around inf 92.5%
associate-*r/92.5%
metadata-eval92.5%
Simplified92.5%
Taylor expanded in a around inf 92.5%
if -2.9e19 < a < 2.3e20Initial program 96.0%
associate--l+96.0%
+-commutative96.0%
+-commutative96.0%
sub-neg96.0%
associate-+l+96.0%
+-commutative96.0%
fma-define96.0%
Simplified96.0%
Taylor expanded in a around 0 99.7%
Taylor expanded in b around 0 78.4%
pow299.7%
Applied egg-rr78.4%
Final simplification85.4%
(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 73.3%
associate--l+73.3%
+-commutative73.3%
+-commutative73.3%
sub-neg73.3%
associate-+l+73.3%
+-commutative73.3%
fma-define73.3%
Simplified77.6%
Taylor expanded in a around 0 70.2%
Taylor expanded in b around 0 52.0%
pow270.2%
Applied egg-rr52.0%
Final simplification52.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 73.3%
associate--l+73.3%
+-commutative73.3%
+-commutative73.3%
sub-neg73.3%
associate-+l+73.3%
+-commutative73.3%
fma-define73.3%
Simplified77.6%
Taylor expanded in a around 0 70.2%
Taylor expanded in b around 0 22.8%
herbie shell --seed 2024136
(FPCore (a b)
:name "Bouland and Aaronson, Equation (25)"
:precision binary64
(- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))