
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-b + sqrt(((b * b) - ((3.0d0 * a) * c)))) / (3.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(3.0 * a) * c)))) / Float64(3.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(3.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-b + sqrt(((b * b) - ((3.0d0 * a) * c)))) / (3.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(3.0 * a) * c)))) / Float64(3.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(3.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a}
\end{array}
(FPCore (a b c) :precision binary64 (/ c (- (- b) (sqrt (fma b b (* (* c a) -3.0))))))
double code(double a, double b, double c) {
return c / (-b - sqrt(fma(b, b, ((c * a) * -3.0))));
}
function code(a, b, c) return Float64(c / Float64(Float64(-b) - sqrt(fma(b, b, Float64(Float64(c * a) * -3.0))))) end
code[a_, b_, c_] := N[(c / N[((-b) - N[Sqrt[N[(b * b + N[(N[(c * a), $MachinePrecision] * -3.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c}{\left(-b\right) - \sqrt{\mathsf{fma}\left(b, b, \left(c \cdot a\right) \cdot -3\right)}}
\end{array}
Initial program 29.6%
neg-sub029.6%
associate-+l-29.6%
sub0-neg29.6%
neg-mul-129.6%
associate-*r/29.6%
metadata-eval29.6%
metadata-eval29.6%
times-frac29.6%
*-commutative29.6%
times-frac29.6%
associate-*l/29.6%
Simplified29.6%
add-log-exp10.9%
associate-*r*10.9%
exp-prod8.2%
*-commutative8.2%
exp-prod8.1%
Applied egg-rr8.1%
log-pow14.9%
log-pow15.5%
Simplified15.5%
flip-+15.5%
add-sqr-sqrt16.0%
add-log-exp17.5%
*-commutative17.5%
add-log-exp30.5%
*-commutative30.5%
Applied egg-rr30.5%
*-un-lft-identity30.5%
associate-/l/30.5%
sqr-neg30.5%
associate--r-99.3%
*-commutative99.3%
Applied egg-rr99.3%
div-inv99.2%
+-inverses99.2%
Applied egg-rr99.2%
+-lft-identity99.2%
associate-*r/99.3%
*-rgt-identity99.3%
associate-/r*99.6%
associate-*r*99.3%
times-frac99.6%
associate-/l*99.7%
*-inverses99.7%
metadata-eval99.7%
fma-neg99.8%
associate-*r*99.8%
distribute-rgt-neg-in99.8%
metadata-eval99.8%
Simplified99.8%
Final simplification99.8%
(FPCore (a b c) :precision binary64 (let* ((t_0 (* c (* a 3.0)))) (* t_0 (/ (/ 1.0 (* a 3.0)) (- (- b) (sqrt (- (* b b) t_0)))))))
double code(double a, double b, double c) {
double t_0 = c * (a * 3.0);
return t_0 * ((1.0 / (a * 3.0)) / (-b - sqrt(((b * b) - t_0))));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: t_0
t_0 = c * (a * 3.0d0)
code = t_0 * ((1.0d0 / (a * 3.0d0)) / (-b - sqrt(((b * b) - t_0))))
end function
public static double code(double a, double b, double c) {
double t_0 = c * (a * 3.0);
return t_0 * ((1.0 / (a * 3.0)) / (-b - Math.sqrt(((b * b) - t_0))));
}
def code(a, b, c): t_0 = c * (a * 3.0) return t_0 * ((1.0 / (a * 3.0)) / (-b - math.sqrt(((b * b) - t_0))))
function code(a, b, c) t_0 = Float64(c * Float64(a * 3.0)) return Float64(t_0 * Float64(Float64(1.0 / Float64(a * 3.0)) / Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - t_0))))) end
function tmp = code(a, b, c) t_0 = c * (a * 3.0); tmp = t_0 * ((1.0 / (a * 3.0)) / (-b - sqrt(((b * b) - t_0)))); end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 * N[(N[(1.0 / N[(a * 3.0), $MachinePrecision]), $MachinePrecision] / N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot \left(a \cdot 3\right)\\
t_0 \cdot \frac{\frac{1}{a \cdot 3}}{\left(-b\right) - \sqrt{b \cdot b - t_0}}
\end{array}
\end{array}
Initial program 29.6%
neg-sub029.6%
associate-+l-29.6%
sub0-neg29.6%
neg-mul-129.6%
associate-*r/29.6%
metadata-eval29.6%
metadata-eval29.6%
times-frac29.6%
*-commutative29.6%
times-frac29.6%
associate-*l/29.6%
Simplified29.6%
add-log-exp10.9%
associate-*r*10.9%
exp-prod8.2%
*-commutative8.2%
exp-prod8.1%
Applied egg-rr8.1%
log-pow14.9%
log-pow15.5%
Simplified15.5%
flip-+15.5%
add-sqr-sqrt16.0%
add-log-exp17.5%
*-commutative17.5%
add-log-exp30.5%
*-commutative30.5%
Applied egg-rr30.5%
*-un-lft-identity30.5%
associate-/l/30.5%
sqr-neg30.5%
associate--r-99.3%
*-commutative99.3%
Applied egg-rr99.3%
div-inv99.2%
+-inverses99.2%
Applied egg-rr99.2%
+-lft-identity99.2%
associate-/r*99.3%
Simplified99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (let* ((t_0 (* c (* a 3.0)))) (/ t_0 (* (* a 3.0) (- (- b) (sqrt (- (* b b) t_0)))))))
double code(double a, double b, double c) {
double t_0 = c * (a * 3.0);
return t_0 / ((a * 3.0) * (-b - sqrt(((b * b) - t_0))));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: t_0
t_0 = c * (a * 3.0d0)
code = t_0 / ((a * 3.0d0) * (-b - sqrt(((b * b) - t_0))))
end function
public static double code(double a, double b, double c) {
double t_0 = c * (a * 3.0);
return t_0 / ((a * 3.0) * (-b - Math.sqrt(((b * b) - t_0))));
}
def code(a, b, c): t_0 = c * (a * 3.0) return t_0 / ((a * 3.0) * (-b - math.sqrt(((b * b) - t_0))))
function code(a, b, c) t_0 = Float64(c * Float64(a * 3.0)) return Float64(t_0 / Float64(Float64(a * 3.0) * Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - t_0))))) end
function tmp = code(a, b, c) t_0 = c * (a * 3.0); tmp = t_0 / ((a * 3.0) * (-b - sqrt(((b * b) - t_0)))); end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 / N[(N[(a * 3.0), $MachinePrecision] * N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot \left(a \cdot 3\right)\\
\frac{t_0}{\left(a \cdot 3\right) \cdot \left(\left(-b\right) - \sqrt{b \cdot b - t_0}\right)}
\end{array}
\end{array}
Initial program 29.6%
neg-sub029.6%
associate-+l-29.6%
sub0-neg29.6%
neg-mul-129.6%
associate-*r/29.6%
metadata-eval29.6%
metadata-eval29.6%
times-frac29.6%
*-commutative29.6%
times-frac29.6%
associate-*l/29.6%
Simplified29.6%
add-log-exp10.9%
associate-*r*10.9%
exp-prod8.2%
*-commutative8.2%
exp-prod8.1%
Applied egg-rr8.1%
log-pow14.9%
log-pow15.5%
Simplified15.5%
flip-+15.5%
add-sqr-sqrt16.0%
add-log-exp17.5%
*-commutative17.5%
add-log-exp30.5%
*-commutative30.5%
Applied egg-rr30.5%
*-un-lft-identity30.5%
associate-/l/30.5%
sqr-neg30.5%
associate--r-99.3%
*-commutative99.3%
Applied egg-rr99.3%
*-un-lft-identity99.3%
+-inverses99.3%
Applied egg-rr99.3%
Final simplification99.3%
(FPCore (a b c) :precision binary64 (/ (+ (* c (* a 3.0)) (- (* b b) (* b b))) (* (* a 3.0) (+ (* 1.5 (/ (* c a) b)) (* b -2.0)))))
double code(double a, double b, double c) {
return ((c * (a * 3.0)) + ((b * b) - (b * b))) / ((a * 3.0) * ((1.5 * ((c * a) / b)) + (b * -2.0)));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = ((c * (a * 3.0d0)) + ((b * b) - (b * b))) / ((a * 3.0d0) * ((1.5d0 * ((c * a) / b)) + (b * (-2.0d0))))
end function
public static double code(double a, double b, double c) {
return ((c * (a * 3.0)) + ((b * b) - (b * b))) / ((a * 3.0) * ((1.5 * ((c * a) / b)) + (b * -2.0)));
}
def code(a, b, c): return ((c * (a * 3.0)) + ((b * b) - (b * b))) / ((a * 3.0) * ((1.5 * ((c * a) / b)) + (b * -2.0)))
function code(a, b, c) return Float64(Float64(Float64(c * Float64(a * 3.0)) + Float64(Float64(b * b) - Float64(b * b))) / Float64(Float64(a * 3.0) * Float64(Float64(1.5 * Float64(Float64(c * a) / b)) + Float64(b * -2.0)))) end
function tmp = code(a, b, c) tmp = ((c * (a * 3.0)) + ((b * b) - (b * b))) / ((a * 3.0) * ((1.5 * ((c * a) / b)) + (b * -2.0))); end
code[a_, b_, c_] := N[(N[(N[(c * N[(a * 3.0), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(N[(a * 3.0), $MachinePrecision] * N[(N[(1.5 * N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision] + N[(b * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \left(a \cdot 3\right) + \left(b \cdot b - b \cdot b\right)}{\left(a \cdot 3\right) \cdot \left(1.5 \cdot \frac{c \cdot a}{b} + b \cdot -2\right)}
\end{array}
Initial program 29.6%
neg-sub029.6%
associate-+l-29.6%
sub0-neg29.6%
neg-mul-129.6%
associate-*r/29.6%
metadata-eval29.6%
metadata-eval29.6%
times-frac29.6%
*-commutative29.6%
times-frac29.6%
associate-*l/29.6%
Simplified29.6%
add-log-exp10.9%
associate-*r*10.9%
exp-prod8.2%
*-commutative8.2%
exp-prod8.1%
Applied egg-rr8.1%
log-pow14.9%
log-pow15.5%
Simplified15.5%
flip-+15.5%
add-sqr-sqrt16.0%
add-log-exp17.5%
*-commutative17.5%
add-log-exp30.5%
*-commutative30.5%
Applied egg-rr30.5%
*-un-lft-identity30.5%
associate-/l/30.5%
sqr-neg30.5%
associate--r-99.3%
*-commutative99.3%
Applied egg-rr99.3%
Taylor expanded in b around inf 91.3%
Final simplification91.3%
(FPCore (a b c) :precision binary64 (* -0.5 (/ c b)))
double code(double a, double b, double c) {
return -0.5 * (c / b);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (-0.5d0) * (c / b)
end function
public static double code(double a, double b, double c) {
return -0.5 * (c / b);
}
def code(a, b, c): return -0.5 * (c / b)
function code(a, b, c) return Float64(-0.5 * Float64(c / b)) end
function tmp = code(a, b, c) tmp = -0.5 * (c / b); end
code[a_, b_, c_] := N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \frac{c}{b}
\end{array}
Initial program 29.6%
/-rgt-identity29.6%
metadata-eval29.6%
associate-/r/29.6%
metadata-eval29.6%
metadata-eval29.6%
times-frac29.6%
*-commutative29.6%
times-frac29.6%
associate-/r*29.6%
Simplified29.6%
Taylor expanded in b around inf 82.7%
Final simplification82.7%
herbie shell --seed 2023174
(FPCore (a b c)
:name "Cubic critical, medium range"
:precision binary64
:pre (and (and (and (< 1.1102230246251565e-16 a) (< a 9007199254740992.0)) (and (< 1.1102230246251565e-16 b) (< b 9007199254740992.0))) (and (< 1.1102230246251565e-16 c) (< c 9007199254740992.0)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))