
(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 6 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 (hypot a b) 4.0) (fma b (* b 4.0) -1.0)))
double code(double a, double b) {
return pow(hypot(a, b), 4.0) + fma(b, (b * 4.0), -1.0);
}
function code(a, b) return Float64((hypot(a, b) ^ 4.0) + fma(b, Float64(b * 4.0), -1.0)) end
code[a_, b_] := N[(N[Power[N[Sqrt[a ^ 2 + b ^ 2], $MachinePrecision], 4.0], $MachinePrecision] + N[(b * N[(b * 4.0), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(\mathsf{hypot}\left(a, b\right)\right)}^{4} + \mathsf{fma}\left(b, b \cdot 4, -1\right)
\end{array}
Initial program 99.9%
associate--l+99.9%
unpow299.9%
unpow199.9%
sqr-pow99.9%
associate-*r*99.9%
Simplified100.0%
Final simplification100.0%
(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 (<= b 4e-237)
(pow a 4.0)
(if (<= b 3.8e-224)
-1.0
(if (<= b 1.4e-52)
(pow a 4.0)
(if (<= b 2.42e-23)
-1.0
(if (<= b 3.3e+66) (pow a 4.0) (pow b 4.0)))))))
double code(double a, double b) {
double tmp;
if (b <= 4e-237) {
tmp = pow(a, 4.0);
} else if (b <= 3.8e-224) {
tmp = -1.0;
} else if (b <= 1.4e-52) {
tmp = pow(a, 4.0);
} else if (b <= 2.42e-23) {
tmp = -1.0;
} else if (b <= 3.3e+66) {
tmp = 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 <= 4d-237) then
tmp = a ** 4.0d0
else if (b <= 3.8d-224) then
tmp = -1.0d0
else if (b <= 1.4d-52) then
tmp = a ** 4.0d0
else if (b <= 2.42d-23) then
tmp = -1.0d0
else if (b <= 3.3d+66) then
tmp = 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 <= 4e-237) {
tmp = Math.pow(a, 4.0);
} else if (b <= 3.8e-224) {
tmp = -1.0;
} else if (b <= 1.4e-52) {
tmp = Math.pow(a, 4.0);
} else if (b <= 2.42e-23) {
tmp = -1.0;
} else if (b <= 3.3e+66) {
tmp = Math.pow(a, 4.0);
} else {
tmp = Math.pow(b, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if b <= 4e-237: tmp = math.pow(a, 4.0) elif b <= 3.8e-224: tmp = -1.0 elif b <= 1.4e-52: tmp = math.pow(a, 4.0) elif b <= 2.42e-23: tmp = -1.0 elif b <= 3.3e+66: tmp = math.pow(a, 4.0) else: tmp = math.pow(b, 4.0) return tmp
function code(a, b) tmp = 0.0 if (b <= 4e-237) tmp = a ^ 4.0; elseif (b <= 3.8e-224) tmp = -1.0; elseif (b <= 1.4e-52) tmp = a ^ 4.0; elseif (b <= 2.42e-23) tmp = -1.0; elseif (b <= 3.3e+66) tmp = a ^ 4.0; else tmp = b ^ 4.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (b <= 4e-237) tmp = a ^ 4.0; elseif (b <= 3.8e-224) tmp = -1.0; elseif (b <= 1.4e-52) tmp = a ^ 4.0; elseif (b <= 2.42e-23) tmp = -1.0; elseif (b <= 3.3e+66) tmp = a ^ 4.0; else tmp = b ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[b, 4e-237], N[Power[a, 4.0], $MachinePrecision], If[LessEqual[b, 3.8e-224], -1.0, If[LessEqual[b, 1.4e-52], N[Power[a, 4.0], $MachinePrecision], If[LessEqual[b, 2.42e-23], -1.0, If[LessEqual[b, 3.3e+66], N[Power[a, 4.0], $MachinePrecision], N[Power[b, 4.0], $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 4 \cdot 10^{-237}:\\
\;\;\;\;{a}^{4}\\
\mathbf{elif}\;b \leq 3.8 \cdot 10^{-224}:\\
\;\;\;\;-1\\
\mathbf{elif}\;b \leq 1.4 \cdot 10^{-52}:\\
\;\;\;\;{a}^{4}\\
\mathbf{elif}\;b \leq 2.42 \cdot 10^{-23}:\\
\;\;\;\;-1\\
\mathbf{elif}\;b \leq 3.3 \cdot 10^{+66}:\\
\;\;\;\;{a}^{4}\\
\mathbf{else}:\\
\;\;\;\;{b}^{4}\\
\end{array}
\end{array}
if b < 4e-237 or 3.80000000000000002e-224 < b < 1.39999999999999997e-52 or 2.42000000000000005e-23 < b < 3.3000000000000001e66Initial program 99.9%
associate--l+99.9%
unpow299.9%
unpow199.9%
sqr-pow99.9%
associate-*r*99.9%
Simplified100.0%
Taylor expanded in a around inf 51.9%
if 4e-237 < b < 3.80000000000000002e-224 or 1.39999999999999997e-52 < b < 2.42000000000000005e-23Initial program 100.0%
associate--l+100.0%
unpow2100.0%
unpow1100.0%
sqr-pow100.0%
associate-*r*100.0%
Simplified100.0%
Taylor expanded in a around 0 87.6%
associate--l+87.6%
*-commutative87.6%
unpow287.6%
metadata-eval87.6%
swap-sqr87.6%
unpow287.6%
associate-+r-87.6%
unpow287.6%
swap-sqr87.6%
metadata-eval87.6%
unpow287.6%
metadata-eval87.6%
pow-sqr87.6%
distribute-lft-out87.6%
fma-neg87.6%
unpow287.6%
unpow287.6%
metadata-eval87.6%
Simplified87.6%
Taylor expanded in b around 0 87.6%
if 3.3000000000000001e66 < b Initial program 100.0%
associate--l+100.0%
unpow2100.0%
unpow1100.0%
sqr-pow100.0%
associate-*r*100.0%
Simplified100.0%
Taylor expanded in b around inf 100.0%
Final simplification61.7%
(FPCore (a b) :precision binary64 (if (<= (* b b) 1e+133) (+ (pow a 4.0) -1.0) (pow b 4.0)))
double code(double a, double b) {
double tmp;
if ((b * b) <= 1e+133) {
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) <= 1d+133) 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) <= 1e+133) {
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) <= 1e+133: 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) <= 1e+133) 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) <= 1e+133) 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], 1e+133], 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 10^{+133}:\\
\;\;\;\;{a}^{4} + -1\\
\mathbf{else}:\\
\;\;\;\;{b}^{4}\\
\end{array}
\end{array}
if (*.f64 b b) < 1e133Initial program 99.8%
associate--l+99.8%
unpow299.8%
unpow199.8%
sqr-pow99.8%
associate-*r*99.9%
Simplified100.0%
Taylor expanded in b around 0 93.0%
if 1e133 < (*.f64 b b) Initial program 100.0%
associate--l+100.0%
unpow2100.0%
unpow1100.0%
sqr-pow100.0%
associate-*r*100.0%
Simplified100.0%
Taylor expanded in b around inf 100.0%
Final simplification95.8%
(FPCore (a b) :precision binary64 (if (<= a 5.8e-6) -1.0 (pow a 4.0)))
double code(double a, double b) {
double tmp;
if (a <= 5.8e-6) {
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 <= 5.8d-6) 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 <= 5.8e-6) {
tmp = -1.0;
} else {
tmp = Math.pow(a, 4.0);
}
return tmp;
}
def code(a, b): tmp = 0 if a <= 5.8e-6: tmp = -1.0 else: tmp = math.pow(a, 4.0) return tmp
function code(a, b) tmp = 0.0 if (a <= 5.8e-6) tmp = -1.0; else tmp = a ^ 4.0; end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if (a <= 5.8e-6) tmp = -1.0; else tmp = a ^ 4.0; end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[a, 5.8e-6], -1.0, N[Power[a, 4.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;a \leq 5.8 \cdot 10^{-6}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;{a}^{4}\\
\end{array}
\end{array}
if a < 5.8000000000000004e-6Initial program 99.9%
associate--l+99.9%
unpow299.9%
unpow199.9%
sqr-pow99.9%
associate-*r*99.9%
Simplified100.0%
Taylor expanded in a around 0 77.5%
associate--l+77.5%
*-commutative77.5%
unpow277.5%
metadata-eval77.5%
swap-sqr77.5%
unpow277.5%
associate-+r-77.5%
unpow277.5%
swap-sqr77.5%
metadata-eval77.5%
unpow277.5%
metadata-eval77.5%
pow-sqr77.4%
distribute-lft-out77.4%
fma-neg77.4%
unpow277.4%
unpow277.4%
metadata-eval77.4%
Simplified77.4%
Taylor expanded in b around 0 31.9%
if 5.8000000000000004e-6 < a Initial program 99.9%
associate--l+99.9%
unpow299.9%
unpow199.9%
sqr-pow99.9%
associate-*r*100.0%
Simplified100.0%
Taylor expanded in a around inf 87.3%
Final simplification46.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%
unpow299.9%
unpow199.9%
sqr-pow99.9%
associate-*r*99.9%
Simplified100.0%
Taylor expanded in a around 0 67.0%
associate--l+67.0%
*-commutative67.0%
unpow267.0%
metadata-eval67.0%
swap-sqr67.0%
unpow267.0%
associate-+r-67.0%
unpow267.0%
swap-sqr67.0%
metadata-eval67.0%
unpow267.0%
metadata-eval67.0%
pow-sqr66.9%
distribute-lft-out66.9%
fma-neg66.9%
unpow266.9%
unpow266.9%
metadata-eval66.9%
Simplified66.9%
Taylor expanded in b around 0 23.6%
Final simplification23.6%
herbie shell --seed 2023217
(FPCore (a b)
:name "Bouland and Aaronson, Equation (26)"
:precision binary64
(- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (* b b))) 1.0))