Expression, p14

Percentage Accurate: 99.9% → 100.0%
Time: 3.5s
Alternatives: 4
Speedup: 1.0×

Specification

?
\[\left(\left(\left(56789 \leq a \land a \leq 98765\right) \land \left(0 \leq b \land b \leq 1\right)\right) \land \left(0 \leq c \land c \leq 0.0016773\right)\right) \land \left(0 \leq d \land d \leq 0.0016773\right)\]
\[\begin{array}{l} \\ a \cdot \left(\left(b + c\right) + d\right) \end{array} \]
(FPCore (a b c d) :precision binary64 (* a (+ (+ b c) d)))
double code(double a, double b, double c, double d) {
	return a * ((b + c) + 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 = a * ((b + c) + d)
end function
public static double code(double a, double b, double c, double d) {
	return a * ((b + c) + d);
}
def code(a, b, c, d):
	return a * ((b + c) + d)
function code(a, b, c, d)
	return Float64(a * Float64(Float64(b + c) + d))
end
function tmp = code(a, b, c, d)
	tmp = a * ((b + c) + d);
end
code[a_, b_, c_, d_] := N[(a * N[(N[(b + c), $MachinePrecision] + d), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
a \cdot \left(\left(b + c\right) + d\right)
\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 4 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} \\ a \cdot \left(\left(b + c\right) + d\right) \end{array} \]
(FPCore (a b c d) :precision binary64 (* a (+ (+ b c) d)))
double code(double a, double b, double c, double d) {
	return a * ((b + c) + 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 = a * ((b + c) + d)
end function
public static double code(double a, double b, double c, double d) {
	return a * ((b + c) + d);
}
def code(a, b, c, d):
	return a * ((b + c) + d)
function code(a, b, c, d)
	return Float64(a * Float64(Float64(b + c) + d))
end
function tmp = code(a, b, c, d)
	tmp = a * ((b + c) + d);
end
code[a_, b_, c_, d_] := N[(a * N[(N[(b + c), $MachinePrecision] + d), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 100.0% accurate, 0.1× speedup?

\[\begin{array}{l} [b, c, d] = \mathsf{sort}([b, c, d])\\ \\ \mathsf{fma}\left(d, a, a \cdot \left(b + c\right)\right) \end{array} \]
NOTE: b, c, and d should be sorted in increasing order before calling this function.
(FPCore (a b c d) :precision binary64 (fma d a (* a (+ b c))))
assert(b < c && c < d);
double code(double a, double b, double c, double d) {
	return fma(d, a, (a * (b + c)));
}
b, c, d = sort([b, c, d])
function code(a, b, c, d)
	return fma(d, a, Float64(a * Float64(b + c)))
end
NOTE: b, c, and d should be sorted in increasing order before calling this function.
code[a_, b_, c_, d_] := N[(d * a + N[(a * N[(b + c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[b, c, d] = \mathsf{sort}([b, c, d])\\
\\
\mathsf{fma}\left(d, a, a \cdot \left(b + c\right)\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[a \cdot \left(\left(b + c\right) + d\right) \]
  2. Step-by-step derivation
    1. +-commutative100.0%

      \[\leadsto a \cdot \color{blue}{\left(d + \left(b + c\right)\right)} \]
    2. distribute-rgt-in100.0%

      \[\leadsto \color{blue}{d \cdot a + \left(b + c\right) \cdot a} \]
    3. fma-def100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(d, a, \left(b + c\right) \cdot a\right)} \]
    4. *-commutative100.0%

      \[\leadsto \mathsf{fma}\left(d, a, \color{blue}{a \cdot \left(b + c\right)}\right) \]
  3. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(d, a, a \cdot \left(b + c\right)\right)} \]
  4. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(d, a, a \cdot \left(b + c\right)\right) \]

Alternative 2: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} [b, c, d] = \mathsf{sort}([b, c, d])\\ \\ a \cdot \left(d + \left(b + c\right)\right) \end{array} \]
NOTE: b, c, and d should be sorted in increasing order before calling this function.
(FPCore (a b c d) :precision binary64 (* a (+ d (+ b c))))
assert(b < c && c < d);
double code(double a, double b, double c, double d) {
	return a * (d + (b + c));
}
NOTE: 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 = a * (d + (b + c))
end function
assert b < c && c < d;
public static double code(double a, double b, double c, double d) {
	return a * (d + (b + c));
}
[b, c, d] = sort([b, c, d])
def code(a, b, c, d):
	return a * (d + (b + c))
b, c, d = sort([b, c, d])
function code(a, b, c, d)
	return Float64(a * Float64(d + Float64(b + c)))
end
b, c, d = num2cell(sort([b, c, d])){:}
function tmp = code(a, b, c, d)
	tmp = a * (d + (b + c));
end
NOTE: b, c, and d should be sorted in increasing order before calling this function.
code[a_, b_, c_, d_] := N[(a * N[(d + N[(b + c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[b, c, d] = \mathsf{sort}([b, c, d])\\
\\
a \cdot \left(d + \left(b + c\right)\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[a \cdot \left(\left(b + c\right) + d\right) \]
  2. Final simplification100.0%

    \[\leadsto a \cdot \left(d + \left(b + c\right)\right) \]

Alternative 3: 99.7% accurate, 1.4× speedup?

\[\begin{array}{l} [b, c, d] = \mathsf{sort}([b, c, d])\\ \\ a \cdot \left(d + c\right) \end{array} \]
NOTE: b, c, and d should be sorted in increasing order before calling this function.
(FPCore (a b c d) :precision binary64 (* a (+ d c)))
assert(b < c && c < d);
double code(double a, double b, double c, double d) {
	return a * (d + c);
}
NOTE: 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 = a * (d + c)
end function
assert b < c && c < d;
public static double code(double a, double b, double c, double d) {
	return a * (d + c);
}
[b, c, d] = sort([b, c, d])
def code(a, b, c, d):
	return a * (d + c)
b, c, d = sort([b, c, d])
function code(a, b, c, d)
	return Float64(a * Float64(d + c))
end
b, c, d = num2cell(sort([b, c, d])){:}
function tmp = code(a, b, c, d)
	tmp = a * (d + c);
end
NOTE: b, c, and d should be sorted in increasing order before calling this function.
code[a_, b_, c_, d_] := N[(a * N[(d + c), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[b, c, d] = \mathsf{sort}([b, c, d])\\
\\
a \cdot \left(d + c\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[a \cdot \left(\left(b + c\right) + d\right) \]
  2. Taylor expanded in b around 0 62.6%

    \[\leadsto \color{blue}{a \cdot \left(c + d\right)} \]
  3. Final simplification62.6%

    \[\leadsto a \cdot \left(d + c\right) \]

Alternative 4: 93.7% accurate, 2.3× speedup?

\[\begin{array}{l} [b, c, d] = \mathsf{sort}([b, c, d])\\ \\ d \cdot a \end{array} \]
NOTE: b, c, and d should be sorted in increasing order before calling this function.
(FPCore (a b c d) :precision binary64 (* d a))
assert(b < c && c < d);
double code(double a, double b, double c, double d) {
	return d * a;
}
NOTE: 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 = d * a
end function
assert b < c && c < d;
public static double code(double a, double b, double c, double d) {
	return d * a;
}
[b, c, d] = sort([b, c, d])
def code(a, b, c, d):
	return d * a
b, c, d = sort([b, c, d])
function code(a, b, c, d)
	return Float64(d * a)
end
b, c, d = num2cell(sort([b, c, d])){:}
function tmp = code(a, b, c, d)
	tmp = d * a;
end
NOTE: b, c, and d should be sorted in increasing order before calling this function.
code[a_, b_, c_, d_] := N[(d * a), $MachinePrecision]
\begin{array}{l}
[b, c, d] = \mathsf{sort}([b, c, d])\\
\\
d \cdot a
\end{array}
Derivation
  1. Initial program 100.0%

    \[a \cdot \left(\left(b + c\right) + d\right) \]
  2. Taylor expanded in d around inf 30.5%

    \[\leadsto \color{blue}{a \cdot d} \]
  3. Step-by-step derivation
    1. *-commutative30.5%

      \[\leadsto \color{blue}{d \cdot a} \]
  4. Simplified30.5%

    \[\leadsto \color{blue}{d \cdot a} \]
  5. Final simplification30.5%

    \[\leadsto d \cdot a \]

Developer target: 99.9% accurate, 0.8× speedup?

\[\begin{array}{l} \\ a \cdot b + a \cdot \left(c + d\right) \end{array} \]
(FPCore (a b c d) :precision binary64 (+ (* a b) (* a (+ c d))))
double code(double a, double b, double c, double d) {
	return (a * b) + (a * (c + 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 = (a * b) + (a * (c + d))
end function
public static double code(double a, double b, double c, double d) {
	return (a * b) + (a * (c + d));
}
def code(a, b, c, d):
	return (a * b) + (a * (c + d))
function code(a, b, c, d)
	return Float64(Float64(a * b) + Float64(a * Float64(c + d)))
end
function tmp = code(a, b, c, d)
	tmp = (a * b) + (a * (c + d));
end
code[a_, b_, c_, d_] := N[(N[(a * b), $MachinePrecision] + N[(a * N[(c + d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Reproduce

?
herbie shell --seed 2023178 
(FPCore (a b c d)
  :name "Expression, p14"
  :precision binary64
  :pre (and (and (and (and (<= 56789.0 a) (<= a 98765.0)) (and (<= 0.0 b) (<= b 1.0))) (and (<= 0.0 c) (<= c 0.0016773))) (and (<= 0.0 d) (<= d 0.0016773)))

  :herbie-target
  (+ (* a b) (* a (+ c d)))

  (* a (+ (+ b c) d)))