sqrt(a) + sqrt(b) + sqrt(c ) + sqrt(d)

Percentage Accurate: 99.9% → 99.9%
Time: 14.6s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\left(\left(\left(-1.79 \cdot 10^{+308} \leq a \land a \leq 1.79 \cdot 10^{+308}\right) \land \left(-1.79 \cdot 10^{+308} \leq b \land b \leq 1.79 \cdot 10^{+308}\right)\right) \land \left(-1.79 \cdot 10^{+308} \leq c \land c \leq 1.79 \cdot 10^{+308}\right)\right) \land \left(-1.79 \cdot 10^{+308} \leq d \land d \leq 1.79 \cdot 10^{+308}\right)\]
\[\begin{array}{l} \\ \left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (+ (+ (+ (sqrt a) (sqrt b)) (sqrt c)) (sqrt d)))
double code(double a, double b, double c, double d) {
	return ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d);
}
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    code = ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d)
end function
public static double code(double a, double b, double c, double d) {
	return ((Math.sqrt(a) + Math.sqrt(b)) + Math.sqrt(c)) + Math.sqrt(d);
}
def code(a, b, c, d):
	return ((math.sqrt(a) + math.sqrt(b)) + math.sqrt(c)) + math.sqrt(d)
function code(a, b, c, d)
	return Float64(Float64(Float64(sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d))
end
function tmp = code(a, b, c, d)
	tmp = ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d);
end
code[a_, b_, c_, d_] := N[(N[(N[(N[Sqrt[a], $MachinePrecision] + N[Sqrt[b], $MachinePrecision]), $MachinePrecision] + N[Sqrt[c], $MachinePrecision]), $MachinePrecision] + N[Sqrt[d], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 3 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d} \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (+ (+ (+ (sqrt a) (sqrt b)) (sqrt c)) (sqrt d)))
double code(double a, double b, double c, double d) {
	return ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d);
}
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    code = ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d)
end function
public static double code(double a, double b, double c, double d) {
	return ((Math.sqrt(a) + Math.sqrt(b)) + Math.sqrt(c)) + Math.sqrt(d);
}
def code(a, b, c, d):
	return ((math.sqrt(a) + math.sqrt(b)) + math.sqrt(c)) + math.sqrt(d)
function code(a, b, c, d)
	return Float64(Float64(Float64(sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d))
end
function tmp = code(a, b, c, d)
	tmp = ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d);
end
code[a_, b_, c_, d_] := N[(N[(N[(N[Sqrt[a], $MachinePrecision] + N[Sqrt[b], $MachinePrecision]), $MachinePrecision] + N[Sqrt[c], $MachinePrecision]), $MachinePrecision] + N[Sqrt[d], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d}
\end{array}

Alternative 1: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} [a, b, c, d] = \mathsf{sort}([a, b, c, d])\\ \\ \left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d} \end{array} \]
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
(FPCore (a b c d)
 :precision binary64
 (+ (+ (+ (sqrt a) (sqrt b)) (sqrt c)) (sqrt d)))
assert(a < b && b < c && c < d);
double code(double a, double b, double c, double d) {
	return ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d);
}
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    code = ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d)
end function
assert a < b && b < c && c < d;
public static double code(double a, double b, double c, double d) {
	return ((Math.sqrt(a) + Math.sqrt(b)) + Math.sqrt(c)) + Math.sqrt(d);
}
[a, b, c, d] = sort([a, b, c, d])
def code(a, b, c, d):
	return ((math.sqrt(a) + math.sqrt(b)) + math.sqrt(c)) + math.sqrt(d)
a, b, c, d = sort([a, b, c, d])
function code(a, b, c, d)
	return Float64(Float64(Float64(sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d))
end
a, b, c, d = num2cell(sort([a, b, c, d])){:}
function tmp = code(a, b, c, d)
	tmp = ((sqrt(a) + sqrt(b)) + sqrt(c)) + sqrt(d);
end
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
code[a_, b_, c_, d_] := N[(N[(N[(N[Sqrt[a], $MachinePrecision] + N[Sqrt[b], $MachinePrecision]), $MachinePrecision] + N[Sqrt[c], $MachinePrecision]), $MachinePrecision] + N[Sqrt[d], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b, c, d] = \mathsf{sort}([a, b, c, d])\\
\\
\left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d}
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d} \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 2: 91.6% accurate, 1.4× speedup?

\[\begin{array}{l} [a, b, c, d] = \mathsf{sort}([a, b, c, d])\\ \\ \left(\sqrt{d} + \sqrt{b}\right) + \sqrt{a} \end{array} \]
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
(FPCore (a b c d) :precision binary64 (+ (+ (sqrt d) (sqrt b)) (sqrt a)))
assert(a < b && b < c && c < d);
double code(double a, double b, double c, double d) {
	return (sqrt(d) + sqrt(b)) + sqrt(a);
}
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    code = (sqrt(d) + sqrt(b)) + sqrt(a)
end function
assert a < b && b < c && c < d;
public static double code(double a, double b, double c, double d) {
	return (Math.sqrt(d) + Math.sqrt(b)) + Math.sqrt(a);
}
[a, b, c, d] = sort([a, b, c, d])
def code(a, b, c, d):
	return (math.sqrt(d) + math.sqrt(b)) + math.sqrt(a)
a, b, c, d = sort([a, b, c, d])
function code(a, b, c, d)
	return Float64(Float64(sqrt(d) + sqrt(b)) + sqrt(a))
end
a, b, c, d = num2cell(sort([a, b, c, d])){:}
function tmp = code(a, b, c, d)
	tmp = (sqrt(d) + sqrt(b)) + sqrt(a);
end
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
code[a_, b_, c_, d_] := N[(N[(N[Sqrt[d], $MachinePrecision] + N[Sqrt[b], $MachinePrecision]), $MachinePrecision] + N[Sqrt[a], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a, b, c, d] = \mathsf{sort}([a, b, c, d])\\
\\
\left(\sqrt{d} + \sqrt{b}\right) + \sqrt{a}
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d}} \]
    2. lift-+.f64N/A

      \[\leadsto \color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right)} + \sqrt{d} \]
    3. associate-+l+N/A

      \[\leadsto \color{blue}{\left(\sqrt{a} + \sqrt{b}\right) + \left(\sqrt{c} + \sqrt{d}\right)} \]
    4. +-commutativeN/A

      \[\leadsto \color{blue}{\left(\sqrt{c} + \sqrt{d}\right) + \left(\sqrt{a} + \sqrt{b}\right)} \]
    5. flip-+N/A

      \[\leadsto \color{blue}{\frac{\sqrt{c} \cdot \sqrt{c} - \sqrt{d} \cdot \sqrt{d}}{\sqrt{c} - \sqrt{d}}} + \left(\sqrt{a} + \sqrt{b}\right) \]
    6. div-invN/A

      \[\leadsto \color{blue}{\left(\sqrt{c} \cdot \sqrt{c} - \sqrt{d} \cdot \sqrt{d}\right) \cdot \frac{1}{\sqrt{c} - \sqrt{d}}} + \left(\sqrt{a} + \sqrt{b}\right) \]
    7. lower-fma.f64N/A

      \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{c} \cdot \sqrt{c} - \sqrt{d} \cdot \sqrt{d}, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right)} \]
    8. lift-sqrt.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{\sqrt{c}} \cdot \sqrt{c} - \sqrt{d} \cdot \sqrt{d}, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    9. lift-sqrt.f64N/A

      \[\leadsto \mathsf{fma}\left(\sqrt{c} \cdot \color{blue}{\sqrt{c}} - \sqrt{d} \cdot \sqrt{d}, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    10. rem-square-sqrtN/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{c} - \sqrt{d} \cdot \sqrt{d}, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    11. lift-sqrt.f64N/A

      \[\leadsto \mathsf{fma}\left(c - \color{blue}{\sqrt{d}} \cdot \sqrt{d}, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    12. lift-sqrt.f64N/A

      \[\leadsto \mathsf{fma}\left(c - \sqrt{d} \cdot \color{blue}{\sqrt{d}}, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    13. rem-square-sqrtN/A

      \[\leadsto \mathsf{fma}\left(c - \color{blue}{d}, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    14. lower--.f64N/A

      \[\leadsto \mathsf{fma}\left(\color{blue}{c - d}, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    15. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(c - d, \frac{\color{blue}{\frac{1}{2} + \frac{1}{2}}}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    16. lower-/.f64N/A

      \[\leadsto \mathsf{fma}\left(c - d, \color{blue}{\frac{\frac{1}{2} + \frac{1}{2}}{\sqrt{c} - \sqrt{d}}}, \sqrt{a} + \sqrt{b}\right) \]
    17. metadata-evalN/A

      \[\leadsto \mathsf{fma}\left(c - d, \frac{\color{blue}{1}}{\sqrt{c} - \sqrt{d}}, \sqrt{a} + \sqrt{b}\right) \]
    18. lower--.f6499.6

      \[\leadsto \mathsf{fma}\left(c - d, \frac{1}{\color{blue}{\sqrt{c} - \sqrt{d}}}, \sqrt{a} + \sqrt{b}\right) \]
  4. Applied rewrites99.6%

    \[\leadsto \color{blue}{\mathsf{fma}\left(c - d, \frac{1}{\sqrt{c} - \sqrt{d}}, \sqrt{b} + \sqrt{a}\right)} \]
  5. Taylor expanded in d around inf

    \[\leadsto \color{blue}{\left(\sqrt{a} + \sqrt{b}\right) - -1 \cdot \sqrt{d}} \]
  6. Step-by-step derivation
    1. cancel-sign-sub-invN/A

      \[\leadsto \color{blue}{\left(\sqrt{a} + \sqrt{b}\right) + \left(\mathsf{neg}\left(-1\right)\right) \cdot \sqrt{d}} \]
    2. metadata-evalN/A

      \[\leadsto \left(\sqrt{a} + \sqrt{b}\right) + \color{blue}{1} \cdot \sqrt{d} \]
    3. *-lft-identityN/A

      \[\leadsto \left(\sqrt{a} + \sqrt{b}\right) + \color{blue}{\sqrt{d}} \]
    4. associate-+r+N/A

      \[\leadsto \color{blue}{\sqrt{a} + \left(\sqrt{b} + \sqrt{d}\right)} \]
    5. +-commutativeN/A

      \[\leadsto \color{blue}{\left(\sqrt{b} + \sqrt{d}\right) + \sqrt{a}} \]
    6. lower-+.f64N/A

      \[\leadsto \color{blue}{\left(\sqrt{b} + \sqrt{d}\right) + \sqrt{a}} \]
    7. +-commutativeN/A

      \[\leadsto \color{blue}{\left(\sqrt{d} + \sqrt{b}\right)} + \sqrt{a} \]
    8. lower-+.f64N/A

      \[\leadsto \color{blue}{\left(\sqrt{d} + \sqrt{b}\right)} + \sqrt{a} \]
    9. lower-sqrt.f64N/A

      \[\leadsto \left(\color{blue}{\sqrt{d}} + \sqrt{b}\right) + \sqrt{a} \]
    10. lower-sqrt.f64N/A

      \[\leadsto \left(\sqrt{d} + \color{blue}{\sqrt{b}}\right) + \sqrt{a} \]
    11. lower-sqrt.f6474.4

      \[\leadsto \left(\sqrt{d} + \sqrt{b}\right) + \color{blue}{\sqrt{a}} \]
  7. Applied rewrites74.4%

    \[\leadsto \color{blue}{\left(\sqrt{d} + \sqrt{b}\right) + \sqrt{a}} \]
  8. Add Preprocessing

Alternative 3: 91.6% accurate, 4.5× speedup?

\[\begin{array}{l} [a, b, c, d] = \mathsf{sort}([a, b, c, d])\\ \\ \sqrt{d} \end{array} \]
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
(FPCore (a b c d) :precision binary64 (sqrt d))
assert(a < b && b < c && c < d);
double code(double a, double b, double c, double d) {
	return sqrt(d);
}
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
real(8) function code(a, b, c, d)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8), intent (in) :: d
    code = sqrt(d)
end function
assert a < b && b < c && c < d;
public static double code(double a, double b, double c, double d) {
	return Math.sqrt(d);
}
[a, b, c, d] = sort([a, b, c, d])
def code(a, b, c, d):
	return math.sqrt(d)
a, b, c, d = sort([a, b, c, d])
function code(a, b, c, d)
	return sqrt(d)
end
a, b, c, d = num2cell(sort([a, b, c, d])){:}
function tmp = code(a, b, c, d)
	tmp = sqrt(d);
end
NOTE: a, b, c, and d should be sorted in increasing order before calling this function.
code[a_, b_, c_, d_] := N[Sqrt[d], $MachinePrecision]
\begin{array}{l}
[a, b, c, d] = \mathsf{sort}([a, b, c, d])\\
\\
\sqrt{d}
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(\left(\sqrt{a} + \sqrt{b}\right) + \sqrt{c}\right) + \sqrt{d} \]
  2. Add Preprocessing
  3. Taylor expanded in d around inf

    \[\leadsto \color{blue}{\sqrt{d}} \]
  4. Step-by-step derivation
    1. lower-sqrt.f6429.4

      \[\leadsto \color{blue}{\sqrt{d}} \]
  5. Applied rewrites29.4%

    \[\leadsto \color{blue}{\sqrt{d}} \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 1 
(FPCore (a b c d)
  :name "sqrt(a) + sqrt(b) + sqrt(c ) + sqrt(d)"
  :precision binary64
  :pre (and (and (and (and (<= -1.79e+308 a) (<= a 1.79e+308)) (and (<= -1.79e+308 b) (<= b 1.79e+308))) (and (<= -1.79e+308 c) (<= c 1.79e+308))) (and (<= -1.79e+308 d) (<= d 1.79e+308)))
  (+ (+ (+ (sqrt a) (sqrt b)) (sqrt c)) (sqrt d)))