
(FPCore (a b) :precision binary64 (sqrt (fabs (/ (- (* a a) (* b b)) (* a a)))))
double code(double a, double b) {
return sqrt(fabs((((a * a) - (b * b)) / (a * a))));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(abs((((a * a) - (b * b)) / (a * a))))
end function
public static double code(double a, double b) {
return Math.sqrt(Math.abs((((a * a) - (b * b)) / (a * a))));
}
def code(a, b): return math.sqrt(math.fabs((((a * a) - (b * b)) / (a * a))))
function code(a, b) return sqrt(abs(Float64(Float64(Float64(a * a) - Float64(b * b)) / Float64(a * a)))) end
function tmp = code(a, b) tmp = sqrt(abs((((a * a) - (b * b)) / (a * a)))); end
code[a_, b_] := N[Sqrt[N[Abs[N[(N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\left|\frac{a \cdot a - b \cdot b}{a \cdot a}\right|}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (sqrt (fabs (/ (- (* a a) (* b b)) (* a a)))))
double code(double a, double b) {
return sqrt(fabs((((a * a) - (b * b)) / (a * a))));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(abs((((a * a) - (b * b)) / (a * a))))
end function
public static double code(double a, double b) {
return Math.sqrt(Math.abs((((a * a) - (b * b)) / (a * a))));
}
def code(a, b): return math.sqrt(math.fabs((((a * a) - (b * b)) / (a * a))))
function code(a, b) return sqrt(abs(Float64(Float64(Float64(a * a) - Float64(b * b)) / Float64(a * a)))) end
function tmp = code(a, b) tmp = sqrt(abs((((a * a) - (b * b)) / (a * a)))); end
code[a_, b_] := N[Sqrt[N[Abs[N[(N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\left|\frac{a \cdot a - b \cdot b}{a \cdot a}\right|}
\end{array}
(FPCore (a b) :precision binary64 (sqrt (fabs (fma (/ (/ b a) a) b -1.0))))
double code(double a, double b) {
return sqrt(fabs(fma(((b / a) / a), b, -1.0)));
}
function code(a, b) return sqrt(abs(fma(Float64(Float64(b / a) / a), b, -1.0))) end
code[a_, b_] := N[Sqrt[N[Abs[N[(N[(N[(b / a), $MachinePrecision] / a), $MachinePrecision] * b + -1.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\left|\mathsf{fma}\left(\frac{\frac{b}{a}}{a}, b, -1\right)\right|}
\end{array}
Initial program 81.6%
lift-fabs.f64N/A
lift-/.f64N/A
lift--.f64N/A
div-subN/A
*-inversesN/A
fabs-subN/A
lower-fabs.f64N/A
sub-negN/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
lower-/.f6482.2
Applied rewrites82.2%
lift-*.f64N/A
lift-/.f64N/A
associate-/r*N/A
lift-/.f64N/A
lower-/.f64100.0
Applied rewrites100.0%
(FPCore (a b) :precision binary64 (if (<= (/ (- (* a a) (* b b)) (* a a)) 0.9999999999959164) (sqrt (fabs (* (+ a b) (/ (- a b) (* a a))))) (sqrt (fabs 1.0))))
double code(double a, double b) {
double tmp;
if ((((a * a) - (b * b)) / (a * a)) <= 0.9999999999959164) {
tmp = sqrt(fabs(((a + b) * ((a - b) / (a * a)))));
} else {
tmp = sqrt(fabs(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 * a) - (b * b)) / (a * a)) <= 0.9999999999959164d0) then
tmp = sqrt(abs(((a + b) * ((a - b) / (a * a)))))
else
tmp = sqrt(abs(1.0d0))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((((a * a) - (b * b)) / (a * a)) <= 0.9999999999959164) {
tmp = Math.sqrt(Math.abs(((a + b) * ((a - b) / (a * a)))));
} else {
tmp = Math.sqrt(Math.abs(1.0));
}
return tmp;
}
def code(a, b): tmp = 0 if (((a * a) - (b * b)) / (a * a)) <= 0.9999999999959164: tmp = math.sqrt(math.fabs(((a + b) * ((a - b) / (a * a))))) else: tmp = math.sqrt(math.fabs(1.0)) return tmp
function code(a, b) tmp = 0.0 if (Float64(Float64(Float64(a * a) - Float64(b * b)) / Float64(a * a)) <= 0.9999999999959164) tmp = sqrt(abs(Float64(Float64(a + b) * Float64(Float64(a - b) / Float64(a * a))))); else tmp = sqrt(abs(1.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((((a * a) - (b * b)) / (a * a)) <= 0.9999999999959164) tmp = sqrt(abs(((a + b) * ((a - b) / (a * a))))); else tmp = sqrt(abs(1.0)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision], 0.9999999999959164], N[Sqrt[N[Abs[N[(N[(a + b), $MachinePrecision] * N[(N[(a - b), $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision], N[Sqrt[N[Abs[1.0], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{a \cdot a - b \cdot b}{a \cdot a} \leq 0.9999999999959164:\\
\;\;\;\;\sqrt{\left|\left(a + b\right) \cdot \frac{a - b}{a \cdot a}\right|}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left|1\right|}\\
\end{array}
\end{array}
if (/.f64 (-.f64 (*.f64 a a) (*.f64 b b)) (*.f64 a a)) < 0.99999999999591638Initial program 99.7%
lift-/.f64N/A
lift--.f64N/A
lift-*.f64N/A
lift-*.f64N/A
difference-of-squaresN/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
lower--.f64N/A
+-commutativeN/A
lower-+.f6499.7
Applied rewrites99.7%
if 0.99999999999591638 < (/.f64 (-.f64 (*.f64 a a) (*.f64 b b)) (*.f64 a a)) Initial program 80.9%
Taylor expanded in b around 0
Applied rewrites99.0%
Final simplification99.0%
(FPCore (a b) :precision binary64 (if (<= (/ (- (* a a) (* b b)) (* a a)) 0.9999999999959164) (sqrt (fabs (- (* (/ b (* a a)) b) 1.0))) (sqrt (fabs 1.0))))
double code(double a, double b) {
double tmp;
if ((((a * a) - (b * b)) / (a * a)) <= 0.9999999999959164) {
tmp = sqrt(fabs((((b / (a * a)) * b) - 1.0)));
} else {
tmp = sqrt(fabs(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 * a) - (b * b)) / (a * a)) <= 0.9999999999959164d0) then
tmp = sqrt(abs((((b / (a * a)) * b) - 1.0d0)))
else
tmp = sqrt(abs(1.0d0))
end if
code = tmp
end function
public static double code(double a, double b) {
double tmp;
if ((((a * a) - (b * b)) / (a * a)) <= 0.9999999999959164) {
tmp = Math.sqrt(Math.abs((((b / (a * a)) * b) - 1.0)));
} else {
tmp = Math.sqrt(Math.abs(1.0));
}
return tmp;
}
def code(a, b): tmp = 0 if (((a * a) - (b * b)) / (a * a)) <= 0.9999999999959164: tmp = math.sqrt(math.fabs((((b / (a * a)) * b) - 1.0))) else: tmp = math.sqrt(math.fabs(1.0)) return tmp
function code(a, b) tmp = 0.0 if (Float64(Float64(Float64(a * a) - Float64(b * b)) / Float64(a * a)) <= 0.9999999999959164) tmp = sqrt(abs(Float64(Float64(Float64(b / Float64(a * a)) * b) - 1.0))); else tmp = sqrt(abs(1.0)); end return tmp end
function tmp_2 = code(a, b) tmp = 0.0; if ((((a * a) - (b * b)) / (a * a)) <= 0.9999999999959164) tmp = sqrt(abs((((b / (a * a)) * b) - 1.0))); else tmp = sqrt(abs(1.0)); end tmp_2 = tmp; end
code[a_, b_] := If[LessEqual[N[(N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision] / N[(a * a), $MachinePrecision]), $MachinePrecision], 0.9999999999959164], N[Sqrt[N[Abs[N[(N[(N[(b / N[(a * a), $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision] - 1.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision], N[Sqrt[N[Abs[1.0], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{a \cdot a - b \cdot b}{a \cdot a} \leq 0.9999999999959164:\\
\;\;\;\;\sqrt{\left|\frac{b}{a \cdot a} \cdot b - 1\right|}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left|1\right|}\\
\end{array}
\end{array}
if (/.f64 (-.f64 (*.f64 a a) (*.f64 b b)) (*.f64 a a)) < 0.99999999999591638Initial program 99.7%
lift-/.f64N/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f6499.5
Applied rewrites99.5%
if 0.99999999999591638 < (/.f64 (-.f64 (*.f64 a a) (*.f64 b b)) (*.f64 a a)) Initial program 80.9%
Taylor expanded in b around 0
Applied rewrites99.0%
Final simplification99.0%
(FPCore (a b) :precision binary64 (if (<= (* b b) 0.0) (sqrt (fabs 1.0)) (sqrt (fabs (fma (/ b (* a a)) b -1.0)))))
double code(double a, double b) {
double tmp;
if ((b * b) <= 0.0) {
tmp = sqrt(fabs(1.0));
} else {
tmp = sqrt(fabs(fma((b / (a * a)), b, -1.0)));
}
return tmp;
}
function code(a, b) tmp = 0.0 if (Float64(b * b) <= 0.0) tmp = sqrt(abs(1.0)); else tmp = sqrt(abs(fma(Float64(b / Float64(a * a)), b, -1.0))); end return tmp end
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 0.0], N[Sqrt[N[Abs[1.0], $MachinePrecision]], $MachinePrecision], N[Sqrt[N[Abs[N[(N[(b / N[(a * a), $MachinePrecision]), $MachinePrecision] * b + -1.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 0:\\
\;\;\;\;\sqrt{\left|1\right|}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{\left|\mathsf{fma}\left(\frac{b}{a \cdot a}, b, -1\right)\right|}\\
\end{array}
\end{array}
if (*.f64 b b) < 0.0Initial program 73.6%
Taylor expanded in b around 0
Applied rewrites98.6%
if 0.0 < (*.f64 b b) Initial program 100.0%
lift-fabs.f64N/A
lift-/.f64N/A
lift--.f64N/A
div-subN/A
*-inversesN/A
fabs-subN/A
lower-fabs.f64N/A
sub-negN/A
lift-*.f64N/A
associate-/l*N/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
(FPCore (a b) :precision binary64 (sqrt (fabs 1.0)))
double code(double a, double b) {
return sqrt(fabs(1.0));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(abs(1.0d0))
end function
public static double code(double a, double b) {
return Math.sqrt(Math.abs(1.0));
}
def code(a, b): return math.sqrt(math.fabs(1.0))
function code(a, b) return sqrt(abs(1.0)) end
function tmp = code(a, b) tmp = sqrt(abs(1.0)); end
code[a_, b_] := N[Sqrt[N[Abs[1.0], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\left|1\right|}
\end{array}
Initial program 81.6%
Taylor expanded in b around 0
Applied rewrites97.0%
herbie shell --seed 2024235
(FPCore (a b)
:name "Eccentricity of an ellipse"
:precision binary64
:pre (and (and (<= 0.0 b) (<= b a)) (<= a 1.0))
(sqrt (fabs (/ (- (* a a) (* b b)) (* a a)))))