
(FPCore (a b) :precision binary64 (sqrt (- (* a a) (* b b))))
double code(double a, double b) {
return sqrt(((a * a) - (b * b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(((a * a) - (b * b)))
end function
public static double code(double a, double b) {
return Math.sqrt(((a * a) - (b * b)));
}
def code(a, b): return math.sqrt(((a * a) - (b * b)))
function code(a, b) return sqrt(Float64(Float64(a * a) - Float64(b * b))) end
function tmp = code(a, b) tmp = sqrt(((a * a) - (b * b))); end
code[a_, b_] := N[Sqrt[N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{a \cdot a - b \cdot b}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b) :precision binary64 (sqrt (- (* a a) (* b b))))
double code(double a, double b) {
return sqrt(((a * a) - (b * b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt(((a * a) - (b * b)))
end function
public static double code(double a, double b) {
return Math.sqrt(((a * a) - (b * b)));
}
def code(a, b): return math.sqrt(((a * a) - (b * b)))
function code(a, b) return sqrt(Float64(Float64(a * a) - Float64(b * b))) end
function tmp = code(a, b) tmp = sqrt(((a * a) - (b * b))); end
code[a_, b_] := N[Sqrt[N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{a \cdot a - b \cdot b}
\end{array}
NOTE: a should be positive before calling this function NOTE: b should be positive before calling this function (FPCore (a b) :precision binary64 (* (sqrt (- a b)) (/ (+ a b) (sqrt (+ a b)))))
a = abs(a);
b = abs(b);
double code(double a, double b) {
return sqrt((a - b)) * ((a + b) / sqrt((a + b)));
}
NOTE: a should be positive before calling this function
NOTE: b should be positive before calling this function
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt((a - b)) * ((a + b) / sqrt((a + b)))
end function
a = Math.abs(a);
b = Math.abs(b);
public static double code(double a, double b) {
return Math.sqrt((a - b)) * ((a + b) / Math.sqrt((a + b)));
}
a = abs(a) b = abs(b) def code(a, b): return math.sqrt((a - b)) * ((a + b) / math.sqrt((a + b)))
a = abs(a) b = abs(b) function code(a, b) return Float64(sqrt(Float64(a - b)) * Float64(Float64(a + b) / sqrt(Float64(a + b)))) end
a = abs(a) b = abs(b) function tmp = code(a, b) tmp = sqrt((a - b)) * ((a + b) / sqrt((a + b))); end
NOTE: a should be positive before calling this function NOTE: b should be positive before calling this function code[a_, b_] := N[(N[Sqrt[N[(a - b), $MachinePrecision]], $MachinePrecision] * N[(N[(a + b), $MachinePrecision] / N[Sqrt[N[(a + b), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a = |a|\\
b = |b|\\
\\
\sqrt{a - b} \cdot \frac{a + b}{\sqrt{a + b}}
\end{array}
Initial program 58.7%
difference-of-squares59.0%
Simplified59.0%
*-commutative59.0%
sqrt-prod48.1%
Applied egg-rr48.1%
add-sqr-sqrt21.0%
sqrt-unprod46.5%
sqr-neg46.5%
sqrt-unprod26.4%
add-sqr-sqrt46.9%
sub-neg46.9%
flip--27.6%
sqrt-div27.8%
Applied egg-rr48.4%
+-commutative48.4%
+-commutative48.4%
Simplified48.4%
Final simplification48.4%
NOTE: a should be positive before calling this function NOTE: b should be positive before calling this function (FPCore (a b) :precision binary64 (fma -0.5 (/ b (/ a b)) a))
a = abs(a);
b = abs(b);
double code(double a, double b) {
return fma(-0.5, (b / (a / b)), a);
}
a = abs(a) b = abs(b) function code(a, b) return fma(-0.5, Float64(b / Float64(a / b)), a) end
NOTE: a should be positive before calling this function NOTE: b should be positive before calling this function code[a_, b_] := N[(-0.5 * N[(b / N[(a / b), $MachinePrecision]), $MachinePrecision] + a), $MachinePrecision]
\begin{array}{l}
a = |a|\\
b = |b|\\
\\
\mathsf{fma}\left(-0.5, \frac{b}{\frac{a}{b}}, a\right)
\end{array}
Initial program 58.7%
difference-of-squares59.0%
Simplified59.0%
Taylor expanded in a around -inf 0.0%
Simplified48.8%
Final simplification48.8%
NOTE: a should be positive before calling this function NOTE: b should be positive before calling this function (FPCore (a b) :precision binary64 a)
a = abs(a);
b = abs(b);
double code(double a, double b) {
return a;
}
NOTE: a should be positive before calling this function
NOTE: b should be positive before calling this function
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = a
end function
a = Math.abs(a);
b = Math.abs(b);
public static double code(double a, double b) {
return a;
}
a = abs(a) b = abs(b) def code(a, b): return a
a = abs(a) b = abs(b) function code(a, b) return a end
a = abs(a) b = abs(b) function tmp = code(a, b) tmp = a; end
NOTE: a should be positive before calling this function NOTE: b should be positive before calling this function code[a_, b_] := a
\begin{array}{l}
a = |a|\\
b = |b|\\
\\
a
\end{array}
Initial program 58.7%
difference-of-squares59.0%
Simplified59.0%
Taylor expanded in a around inf 48.5%
Final simplification48.5%
(FPCore (a b) :precision binary64 (* (sqrt (+ (fabs a) (fabs b))) (sqrt (- (fabs a) (fabs b)))))
double code(double a, double b) {
return sqrt((fabs(a) + fabs(b))) * sqrt((fabs(a) - fabs(b)));
}
real(8) function code(a, b)
real(8), intent (in) :: a
real(8), intent (in) :: b
code = sqrt((abs(a) + abs(b))) * sqrt((abs(a) - abs(b)))
end function
public static double code(double a, double b) {
return Math.sqrt((Math.abs(a) + Math.abs(b))) * Math.sqrt((Math.abs(a) - Math.abs(b)));
}
def code(a, b): return math.sqrt((math.fabs(a) + math.fabs(b))) * math.sqrt((math.fabs(a) - math.fabs(b)))
function code(a, b) return Float64(sqrt(Float64(abs(a) + abs(b))) * sqrt(Float64(abs(a) - abs(b)))) end
function tmp = code(a, b) tmp = sqrt((abs(a) + abs(b))) * sqrt((abs(a) - abs(b))); end
code[a_, b_] := N[(N[Sqrt[N[(N[Abs[a], $MachinePrecision] + N[Abs[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(N[Abs[a], $MachinePrecision] - N[Abs[b], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\sqrt{\left|a\right| + \left|b\right|} \cdot \sqrt{\left|a\right| - \left|b\right|}
\end{array}
herbie shell --seed 2023230
(FPCore (a b)
:name "bug366, discussion (missed optimization)"
:precision binary64
:herbie-target
(* (sqrt (+ (fabs a) (fabs b))) (sqrt (- (fabs a) (fabs b))))
(sqrt (- (* a a) (* b b))))