
(FPCore (A B C F)
:precision binary64
(let* ((t_0 (- (pow B 2.0) (* (* 4.0 A) C))))
(/
(-
(sqrt
(*
(* 2.0 (* t_0 F))
(- (+ A C) (sqrt (+ (pow (- A C) 2.0) (pow B 2.0)))))))
t_0)))
double code(double A, double B, double C, double F) {
double t_0 = pow(B, 2.0) - ((4.0 * A) * C);
return -sqrt(((2.0 * (t_0 * F)) * ((A + C) - sqrt((pow((A - C), 2.0) + pow(B, 2.0)))))) / t_0;
}
real(8) function code(a, b, c, f)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: f
real(8) :: t_0
t_0 = (b ** 2.0d0) - ((4.0d0 * a) * c)
code = -sqrt(((2.0d0 * (t_0 * f)) * ((a + c) - sqrt((((a - c) ** 2.0d0) + (b ** 2.0d0)))))) / t_0
end function
public static double code(double A, double B, double C, double F) {
double t_0 = Math.pow(B, 2.0) - ((4.0 * A) * C);
return -Math.sqrt(((2.0 * (t_0 * F)) * ((A + C) - Math.sqrt((Math.pow((A - C), 2.0) + Math.pow(B, 2.0)))))) / t_0;
}
def code(A, B, C, F): t_0 = math.pow(B, 2.0) - ((4.0 * A) * C) return -math.sqrt(((2.0 * (t_0 * F)) * ((A + C) - math.sqrt((math.pow((A - C), 2.0) + math.pow(B, 2.0)))))) / t_0
function code(A, B, C, F) t_0 = Float64((B ^ 2.0) - Float64(Float64(4.0 * A) * C)) return Float64(Float64(-sqrt(Float64(Float64(2.0 * Float64(t_0 * F)) * Float64(Float64(A + C) - sqrt(Float64((Float64(A - C) ^ 2.0) + (B ^ 2.0))))))) / t_0) end
function tmp = code(A, B, C, F) t_0 = (B ^ 2.0) - ((4.0 * A) * C); tmp = -sqrt(((2.0 * (t_0 * F)) * ((A + C) - sqrt((((A - C) ^ 2.0) + (B ^ 2.0)))))) / t_0; end
code[A_, B_, C_, F_] := Block[{t$95$0 = N[(N[Power[B, 2.0], $MachinePrecision] - N[(N[(4.0 * A), $MachinePrecision] * C), $MachinePrecision]), $MachinePrecision]}, N[((-N[Sqrt[N[(N[(2.0 * N[(t$95$0 * F), $MachinePrecision]), $MachinePrecision] * N[(N[(A + C), $MachinePrecision] - N[Sqrt[N[(N[Power[N[(A - C), $MachinePrecision], 2.0], $MachinePrecision] + N[Power[B, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) / t$95$0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {B}^{2} - \left(4 \cdot A\right) \cdot C\\
\frac{-\sqrt{\left(2 \cdot \left(t\_0 \cdot F\right)\right) \cdot \left(\left(A + C\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)}}{t\_0}
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (A B C F)
:precision binary64
(let* ((t_0 (- (pow B 2.0) (* (* 4.0 A) C))))
(/
(-
(sqrt
(*
(* 2.0 (* t_0 F))
(- (+ A C) (sqrt (+ (pow (- A C) 2.0) (pow B 2.0)))))))
t_0)))
double code(double A, double B, double C, double F) {
double t_0 = pow(B, 2.0) - ((4.0 * A) * C);
return -sqrt(((2.0 * (t_0 * F)) * ((A + C) - sqrt((pow((A - C), 2.0) + pow(B, 2.0)))))) / t_0;
}
real(8) function code(a, b, c, f)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: f
real(8) :: t_0
t_0 = (b ** 2.0d0) - ((4.0d0 * a) * c)
code = -sqrt(((2.0d0 * (t_0 * f)) * ((a + c) - sqrt((((a - c) ** 2.0d0) + (b ** 2.0d0)))))) / t_0
end function
public static double code(double A, double B, double C, double F) {
double t_0 = Math.pow(B, 2.0) - ((4.0 * A) * C);
return -Math.sqrt(((2.0 * (t_0 * F)) * ((A + C) - Math.sqrt((Math.pow((A - C), 2.0) + Math.pow(B, 2.0)))))) / t_0;
}
def code(A, B, C, F): t_0 = math.pow(B, 2.0) - ((4.0 * A) * C) return -math.sqrt(((2.0 * (t_0 * F)) * ((A + C) - math.sqrt((math.pow((A - C), 2.0) + math.pow(B, 2.0)))))) / t_0
function code(A, B, C, F) t_0 = Float64((B ^ 2.0) - Float64(Float64(4.0 * A) * C)) return Float64(Float64(-sqrt(Float64(Float64(2.0 * Float64(t_0 * F)) * Float64(Float64(A + C) - sqrt(Float64((Float64(A - C) ^ 2.0) + (B ^ 2.0))))))) / t_0) end
function tmp = code(A, B, C, F) t_0 = (B ^ 2.0) - ((4.0 * A) * C); tmp = -sqrt(((2.0 * (t_0 * F)) * ((A + C) - sqrt((((A - C) ^ 2.0) + (B ^ 2.0)))))) / t_0; end
code[A_, B_, C_, F_] := Block[{t$95$0 = N[(N[Power[B, 2.0], $MachinePrecision] - N[(N[(4.0 * A), $MachinePrecision] * C), $MachinePrecision]), $MachinePrecision]}, N[((-N[Sqrt[N[(N[(2.0 * N[(t$95$0 * F), $MachinePrecision]), $MachinePrecision] * N[(N[(A + C), $MachinePrecision] - N[Sqrt[N[(N[Power[N[(A - C), $MachinePrecision], 2.0], $MachinePrecision] + N[Power[B, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) / t$95$0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {B}^{2} - \left(4 \cdot A\right) \cdot C\\
\frac{-\sqrt{\left(2 \cdot \left(t\_0 \cdot F\right)\right) \cdot \left(\left(A + C\right) - \sqrt{{\left(A - C\right)}^{2} + {B}^{2}}\right)}}{t\_0}
\end{array}
\end{array}
B_m = (fabs.f64 B)
NOTE: A, B_m, C, and F should be sorted in increasing order before calling this function.
(FPCore (A B_m C F)
:precision binary64
(let* ((t_0 (fma B_m B_m (* C (* A -4.0)))))
(if (<= B_m 1.75e+26)
(* (/ -1.0 t_0) (sqrt (* t_0 (* (+ A A) (* 2.0 F)))))
(* (sqrt (- (/ F B_m))) (- (sqrt 2.0))))))B_m = fabs(B);
assert(A < B_m && B_m < C && C < F);
double code(double A, double B_m, double C, double F) {
double t_0 = fma(B_m, B_m, (C * (A * -4.0)));
double tmp;
if (B_m <= 1.75e+26) {
tmp = (-1.0 / t_0) * sqrt((t_0 * ((A + A) * (2.0 * F))));
} else {
tmp = sqrt(-(F / B_m)) * -sqrt(2.0);
}
return tmp;
}
B_m = abs(B) A, B_m, C, F = sort([A, B_m, C, F]) function code(A, B_m, C, F) t_0 = fma(B_m, B_m, Float64(C * Float64(A * -4.0))) tmp = 0.0 if (B_m <= 1.75e+26) tmp = Float64(Float64(-1.0 / t_0) * sqrt(Float64(t_0 * Float64(Float64(A + A) * Float64(2.0 * F))))); else tmp = Float64(sqrt(Float64(-Float64(F / B_m))) * Float64(-sqrt(2.0))); end return tmp end
B_m = N[Abs[B], $MachinePrecision]
NOTE: A, B_m, C, and F should be sorted in increasing order before calling this function.
code[A_, B$95$m_, C_, F_] := Block[{t$95$0 = N[(B$95$m * B$95$m + N[(C * N[(A * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[B$95$m, 1.75e+26], N[(N[(-1.0 / t$95$0), $MachinePrecision] * N[Sqrt[N[(t$95$0 * N[(N[(A + A), $MachinePrecision] * N[(2.0 * F), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Sqrt[(-N[(F / B$95$m), $MachinePrecision])], $MachinePrecision] * (-N[Sqrt[2.0], $MachinePrecision])), $MachinePrecision]]]
\begin{array}{l}
B_m = \left|B\right|
\\
[A, B_m, C, F] = \mathsf{sort}([A, B_m, C, F])\\
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(B\_m, B\_m, C \cdot \left(A \cdot -4\right)\right)\\
\mathbf{if}\;B\_m \leq 1.75 \cdot 10^{+26}:\\
\;\;\;\;\frac{-1}{t\_0} \cdot \sqrt{t\_0 \cdot \left(\left(A + A\right) \cdot \left(2 \cdot F\right)\right)}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\frac{F}{B\_m}} \cdot \left(-\sqrt{2}\right)\\
\end{array}
\end{array}
if B < 1.75e26Initial program 23.9%
Taylor expanded in C around inf
cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-+.f6445.8
Applied rewrites45.8%
lift-/.f64N/A
frac-2negN/A
lift-neg.f64N/A
remove-double-negN/A
lift--.f64N/A
sub-negN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lift-*.f64N/A
Applied rewrites45.8%
lift-/.f64N/A
clear-numN/A
associate-/r/N/A
lower-*.f64N/A
frac-2negN/A
metadata-evalN/A
lift-neg.f64N/A
remove-double-negN/A
lower-/.f6445.7
Applied rewrites45.7%
lift-*.f64N/A
lift-*.f64N/A
associate-*r*N/A
*-commutativeN/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f6446.9
Applied rewrites46.9%
if 1.75e26 < B Initial program 8.4%
Taylor expanded in F around 0
mul-1-negN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
Applied rewrites16.6%
Taylor expanded in B around inf
Applied rewrites45.0%
Final simplification46.1%
B_m = (fabs.f64 B)
NOTE: A, B_m, C, and F should be sorted in increasing order before calling this function.
(FPCore (A B_m C F)
:precision binary64
(let* ((t_0 (fma B_m B_m (* C (* A -4.0)))))
(if (<= (pow B_m 2.0) 5e-210)
(/
(sqrt (* (+ A A) (* (* C A) (* F -8.0))))
(fma B_m (- B_m) (* C (* A 4.0))))
(if (<= (pow B_m 2.0) 5e+50)
(/ (sqrt (* 2.0 (* F (* t_0 (+ A A))))) (- t_0))
(* (sqrt (- (/ F B_m))) (- (sqrt 2.0)))))))B_m = fabs(B);
assert(A < B_m && B_m < C && C < F);
double code(double A, double B_m, double C, double F) {
double t_0 = fma(B_m, B_m, (C * (A * -4.0)));
double tmp;
if (pow(B_m, 2.0) <= 5e-210) {
tmp = sqrt(((A + A) * ((C * A) * (F * -8.0)))) / fma(B_m, -B_m, (C * (A * 4.0)));
} else if (pow(B_m, 2.0) <= 5e+50) {
tmp = sqrt((2.0 * (F * (t_0 * (A + A))))) / -t_0;
} else {
tmp = sqrt(-(F / B_m)) * -sqrt(2.0);
}
return tmp;
}
B_m = abs(B) A, B_m, C, F = sort([A, B_m, C, F]) function code(A, B_m, C, F) t_0 = fma(B_m, B_m, Float64(C * Float64(A * -4.0))) tmp = 0.0 if ((B_m ^ 2.0) <= 5e-210) tmp = Float64(sqrt(Float64(Float64(A + A) * Float64(Float64(C * A) * Float64(F * -8.0)))) / fma(B_m, Float64(-B_m), Float64(C * Float64(A * 4.0)))); elseif ((B_m ^ 2.0) <= 5e+50) tmp = Float64(sqrt(Float64(2.0 * Float64(F * Float64(t_0 * Float64(A + A))))) / Float64(-t_0)); else tmp = Float64(sqrt(Float64(-Float64(F / B_m))) * Float64(-sqrt(2.0))); end return tmp end
B_m = N[Abs[B], $MachinePrecision]
NOTE: A, B_m, C, and F should be sorted in increasing order before calling this function.
code[A_, B$95$m_, C_, F_] := Block[{t$95$0 = N[(B$95$m * B$95$m + N[(C * N[(A * -4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Power[B$95$m, 2.0], $MachinePrecision], 5e-210], N[(N[Sqrt[N[(N[(A + A), $MachinePrecision] * N[(N[(C * A), $MachinePrecision] * N[(F * -8.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[(B$95$m * (-B$95$m) + N[(C * N[(A * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[Power[B$95$m, 2.0], $MachinePrecision], 5e+50], N[(N[Sqrt[N[(2.0 * N[(F * N[(t$95$0 * N[(A + A), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / (-t$95$0)), $MachinePrecision], N[(N[Sqrt[(-N[(F / B$95$m), $MachinePrecision])], $MachinePrecision] * (-N[Sqrt[2.0], $MachinePrecision])), $MachinePrecision]]]]
\begin{array}{l}
B_m = \left|B\right|
\\
[A, B_m, C, F] = \mathsf{sort}([A, B_m, C, F])\\
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(B\_m, B\_m, C \cdot \left(A \cdot -4\right)\right)\\
\mathbf{if}\;{B\_m}^{2} \leq 5 \cdot 10^{-210}:\\
\;\;\;\;\frac{\sqrt{\left(A + A\right) \cdot \left(\left(C \cdot A\right) \cdot \left(F \cdot -8\right)\right)}}{\mathsf{fma}\left(B\_m, -B\_m, C \cdot \left(A \cdot 4\right)\right)}\\
\mathbf{elif}\;{B\_m}^{2} \leq 5 \cdot 10^{+50}:\\
\;\;\;\;\frac{\sqrt{2 \cdot \left(F \cdot \left(t\_0 \cdot \left(A + A\right)\right)\right)}}{-t\_0}\\
\mathbf{else}:\\
\;\;\;\;\sqrt{-\frac{F}{B\_m}} \cdot \left(-\sqrt{2}\right)\\
\end{array}
\end{array}
if (pow.f64 B #s(literal 2 binary64)) < 5.0000000000000002e-210Initial program 18.5%
Taylor expanded in C around inf
cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-+.f6445.1
Applied rewrites45.1%
Taylor expanded in B around 0
lower-*.f64N/A
lower-*.f64N/A
lower-*.f6437.8
Applied rewrites37.8%
Applied rewrites44.3%
if 5.0000000000000002e-210 < (pow.f64 B #s(literal 2 binary64)) < 5e50Initial program 35.8%
Taylor expanded in C around inf
cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower-+.f6440.4
Applied rewrites40.4%
lift-/.f64N/A
frac-2negN/A
lift-neg.f64N/A
remove-double-negN/A
lift--.f64N/A
sub-negN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lift-*.f64N/A
Applied rewrites40.5%
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f6437.4
Applied rewrites37.4%
if 5e50 < (pow.f64 B #s(literal 2 binary64)) Initial program 12.9%
Taylor expanded in F around 0
mul-1-negN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
Applied rewrites16.4%
Taylor expanded in B around inf
Applied rewrites46.3%
Final simplification43.7%
herbie shell --seed 2024226
(FPCore (A B C F)
:name "ABCF->ab-angle b"
:precision binary64
(/ (- (sqrt (* (* 2.0 (* (- (pow B 2.0) (* (* 4.0 A) C)) F)) (- (+ A C) (sqrt (+ (pow (- A C) 2.0) (pow B 2.0))))))) (- (pow B 2.0) (* (* 4.0 A) C))))