
(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 (+ (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 (<= a 3.1e-283)
(pow b 4.0)
(if (<= a 1.5e-258)
-1.0
(if (<= a 3.5e-196)
(pow b 4.0)
(if (<= a 4.55e-185)
-1.0
(if (<= a 4.7e-155)
(pow b 4.0)
(if (<= a 2.7e-122)
-1.0
(if (<= a 3.8e+28) (pow b 4.0) (pow a 4.0)))))))))
double code(double a, double b) {
double tmp;
if (a <= 3.1e-283) {
tmp = pow(b, 4.0);
} else if (a <= 1.5e-258) {
tmp = -1.0;
} else if (a <= 3.5e-196) {
tmp = pow(b, 4.0);
} else if (a <= 4.55e-185) {
tmp = -1.0;
} else if (a <= 4.7e-155) {
tmp = pow(b, 4.0);
} else if (a <= 2.7e-122) {
tmp = -1.0;
} else if (a <= 3.8e+28) {
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 <= 3.1d-283) then
tmp = b ** 4.0d0
else if (a <= 1.5d-258) then
tmp = -1.0d0
else if (a <= 3.5d-196) then
tmp = b ** 4.0d0
else if (a <= 4.55d-185) then
tmp = -1.0d0
else if (a <= 4.7d-155) then
tmp = b ** 4.0d0
else if (a <= 2.7d-122) then
tmp = -1.0d0
else if (a <= 3.8d+28) 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 <= 3.1e-283) {
tmp = Math.pow(b, 4.0);
} else if (a <= 1.5e-258) {
tmp = -1.0;
} else if (a <= 3.5e-196) {
tmp = Math.pow(b, 4.0);
} else if (a <= 4.55e-185) {
tmp = -1.0;
} else if (a <= 4.7e-155) {
tmp = Math.pow(b, 4.0);
} else if (a <= 2.7e-122) {
tmp = -1.0;
} else if (a <= 3.8e+28) {
tmp = Math.pow(b, 4.0);
} else {
tmp = Math.pow(a, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= 3.1e-283: tmp = math.pow(b, 4.0) elif a <= 1.5e-258: tmp = -1.0 elif a <= 3.5e-196: tmp = math.pow(b, 4.0) elif a <= 4.55e-185: tmp = -1.0 elif a <= 4.7e-155: tmp = math.pow(b, 4.0) elif a <= 2.7e-122: tmp = -1.0 elif a <= 3.8e+28: tmp = math.pow(b, 4.0) else: tmp = math.pow(a, 4.0) return tmp
function code(a, b) tmp = 0.0 if (a <= 3.1e-283) tmp = b ^ 4.0; elseif (a <= 1.5e-258) tmp = -1.0; elseif (a <= 3.5e-196) tmp = b ^ 4.0; elseif (a <= 4.55e-185) tmp = -1.0; elseif (a <= 4.7e-155) tmp = b ^ 4.0; elseif (a <= 2.7e-122) tmp = -1.0; elseif (a <= 3.8e+28) tmp = b ^ 4.0; else tmp = a ^ 4.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= 3.1e-283) tmp = b ^ 4.0; elseif (a <= 1.5e-258) tmp = -1.0; elseif (a <= 3.5e-196) tmp = b ^ 4.0; elseif (a <= 4.55e-185) tmp = -1.0; elseif (a <= 4.7e-155) tmp = b ^ 4.0; elseif (a <= 2.7e-122) tmp = -1.0; elseif (a <= 3.8e+28) tmp = b ^ 4.0; else tmp = a ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, 3.1e-283], N[Power[b, 4.0], $MachinePrecision], If[LessEqual[a, 1.5e-258], -1.0, If[LessEqual[a, 3.5e-196], N[Power[b, 4.0], $MachinePrecision], If[LessEqual[a, 4.55e-185], -1.0, If[LessEqual[a, 4.7e-155], N[Power[b, 4.0], $MachinePrecision], If[LessEqual[a, 2.7e-122], -1.0, If[LessEqual[a, 3.8e+28], N[Power[b, 4.0], $MachinePrecision], N[Power[a, 4.0], $MachinePrecision]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 3.1 \cdot 10^{-283}:\\
\;\;\;\;{b}^{4}\\
\mathbf{elif}\;a \leq 1.5 \cdot 10^{-258}:\\
\;\;\;\;-1\\
\mathbf{elif}\;a \leq 3.5 \cdot 10^{-196}:\\
\;\;\;\;{b}^{4}\\
\mathbf{elif}\;a \leq 4.55 \cdot 10^{-185}:\\
\;\;\;\;-1\\
\mathbf{elif}\;a \leq 4.7 \cdot 10^{-155}:\\
\;\;\;\;{b}^{4}\\
\mathbf{elif}\;a \leq 2.7 \cdot 10^{-122}:\\
\;\;\;\;-1\\
\mathbf{elif}\;a \leq 3.8 \cdot 10^{+28}:\\
\;\;\;\;{b}^{4}\\
\mathbf{else}:\\
\;\;\;\;{a}^{4}\\
\end{array}
\end{array}
if a < 3.10000000000000004e-283 or 1.5000000000000001e-258 < a < 3.50000000000000004e-196 or 4.55000000000000015e-185 < a < 4.6999999999999998e-155 or 2.70000000000000009e-122 < a < 3.7999999999999999e28Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around 0 80.3%
Taylor expanded in b around inf 56.0%
if 3.10000000000000004e-283 < a < 1.5000000000000001e-258 or 3.50000000000000004e-196 < a < 4.55000000000000015e-185 or 4.6999999999999998e-155 < a < 2.70000000000000009e-122Initial program 100.0%
associate--l+100.0%
fma-define100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in a around 0 100.0%
Taylor expanded in b around 0 99.7%
if 3.7999999999999999e28 < a Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in b around 0 95.3%
Taylor expanded in a around inf 95.3%
(FPCore (a b) :precision binary64 (+ (+ (* (* b b) 4.0) (pow (+ (* b b) (* a a)) 2.0)) -1.0))
double code(double a, double b) {
return (((b * b) * 4.0) + pow(((b * b) + (a * a)), 2.0)) + -1.0;
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = (((b * b) * 4.0d0) + (((b * b) + (a * a)) ** 2.0d0)) + (-1.0d0)
end function
public static double code(double a, double b) {
return (((b * b) * 4.0) + Math.pow(((b * b) + (a * a)), 2.0)) + -1.0;
}
def code(a, b): return (((b * b) * 4.0) + math.pow(((b * b) + (a * a)), 2.0)) + -1.0
function code(a, b) return Float64(Float64(Float64(Float64(b * b) * 4.0) + (Float64(Float64(b * b) + Float64(a * a)) ^ 2.0)) + -1.0) end
function tmp = code(a, b) tmp = (((b * b) * 4.0) + (((b * b) + (a * a)) ^ 2.0)) + -1.0; end
code[a_, b_] := N[(N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + N[Power[N[(N[(b * b), $MachinePrecision] + N[(a * a), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\left(\left(b \cdot b\right) \cdot 4 + {\left(b \cdot b + a \cdot a\right)}^{2}\right) + -1
\end{array}
Initial program 99.9%
Final simplification99.9%
(FPCore (a b) :precision binary64 (if (<= b 12.0) (+ (pow a 4.0) -1.0) (+ (+ (* (* b b) 4.0) -1.0) (pow b 4.0))))
double code(double a, double b) {
double tmp;
if (b <= 12.0) {
tmp = pow(a, 4.0) + -1.0;
} else {
tmp = (((b * b) * 4.0) + -1.0) + 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 <= 12.0d0) then
tmp = (a ** 4.0d0) + (-1.0d0)
else
tmp = (((b * b) * 4.0d0) + (-1.0d0)) + (b ** 4.0d0)
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if (b <= 12.0) {
tmp = Math.pow(a, 4.0) + -1.0;
} else {
tmp = (((b * b) * 4.0) + -1.0) + Math.pow(b, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 12.0: tmp = math.pow(a, 4.0) + -1.0 else: tmp = (((b * b) * 4.0) + -1.0) + math.pow(b, 4.0) return tmp
function code(a, b) tmp = 0.0 if (b <= 12.0) tmp = Float64((a ^ 4.0) + -1.0); else tmp = Float64(Float64(Float64(Float64(b * b) * 4.0) + -1.0) + (b ^ 4.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 12.0) tmp = (a ^ 4.0) + -1.0; else tmp = (((b * b) * 4.0) + -1.0) + (b ^ 4.0); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 12.0], N[(N[Power[a, 4.0], $MachinePrecision] + -1.0), $MachinePrecision], N[(N[(N[(N[(b * b), $MachinePrecision] * 4.0), $MachinePrecision] + -1.0), $MachinePrecision] + N[Power[b, 4.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 12:\\
\;\;\;\;{a}^{4} + -1\\
\mathbf{else}:\\
\;\;\;\;\left(\left(b \cdot b\right) \cdot 4 + -1\right) + {b}^{4}\\
\end{array}
\end{array}
if b < 12Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in b around 0 79.7%
if 12 < b Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around 0 93.3%
Final simplification83.4%
(FPCore (a b) :precision binary64 (if (<= b 13.2) (+ (pow a 4.0) -1.0) (pow b 4.0)))
double code(double a, double b) {
double tmp;
if (b <= 13.2) {
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 <= 13.2d0) 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 <= 13.2) {
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 <= 13.2: 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 <= 13.2) 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 <= 13.2) tmp = (a ^ 4.0) + -1.0; else tmp = b ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 13.2], 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 13.2:\\
\;\;\;\;{a}^{4} + -1\\
\mathbf{else}:\\
\;\;\;\;{b}^{4}\\
\end{array}
\end{array}
if b < 13.199999999999999Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in b around 0 79.7%
if 13.199999999999999 < b Initial program 99.9%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around 0 93.3%
Taylor expanded in b around inf 92.2%
Final simplification83.1%
(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%
associate--l+99.9%
fma-define99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in a around 0 83.2%
Taylor expanded in b around 0 31.2%
if 1 < a Initial program 99.9%
associate--l+99.9%
fma-define100.0%
*-commutative100.0%
Simplified100.0%
Taylor expanded in b around 0 92.8%
Taylor expanded in a around inf 92.8%
(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 71.7%
Taylor expanded in b around 0 23.5%
herbie shell --seed 2024085
(FPCore (a b)
:name "Bouland and Aaronson, Equation (26)"
:precision binary64
(- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (* b b))) 1.0))