x1 * x2 * x3 * x4

Percentage Accurate: 99.5% → 99.5%
Time: 868.0ms
Alternatives: 2
Speedup: N/A×

Specification

?
\[\left(\left(\left(10^{-20} \leq x1 \land x1 \leq 2 \cdot 10^{-20}\right) \land \left(10^{-20} \leq x2 \land x2 \leq 2 \cdot 10^{-20}\right)\right) \land \left(10^{+15} \leq x3 \land x3 \leq 2 \cdot 10^{+15}\right)\right) \land \left(10^{+15} \leq x4 \land x4 \leq 2 \cdot 10^{+15}\right)\]
\[\left(\left(x1 \cdot x2\right) \cdot x3\right) \cdot x4 \]
(FPCore (x1 x2 x3 x4)
  :precision binary64
  (* (* (* x1 x2) x3) x4))
double code(double x1, double x2, double x3, double x4) {
	return ((x1 * x2) * x3) * x4;
}
real(8) function code(x1, x2, x3, x4)
    real(8), intent (in) :: x1
    real(8), intent (in) :: x2
    real(8), intent (in) :: x3
    real(8), intent (in) :: x4
    code = ((x1 * x2) * x3) * x4
end function
public static double code(double x1, double x2, double x3, double x4) {
	return ((x1 * x2) * x3) * x4;
}
def code(x1, x2, x3, x4):
	return ((x1 * x2) * x3) * x4
function code(x1, x2, x3, x4)
	return Float64(Float64(Float64(x1 * x2) * x3) * x4)
end
function tmp = code(x1, x2, x3, x4)
	tmp = ((x1 * x2) * x3) * x4;
end
code[x1_, x2_, x3_, x4_] := N[(N[(N[(x1 * x2), $MachinePrecision] * x3), $MachinePrecision] * x4), $MachinePrecision]
\left(\left(x1 \cdot x2\right) \cdot x3\right) \cdot x4

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 2 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.5% accurate, 1.0× speedup?

\[\left(\left(x1 \cdot x2\right) \cdot x3\right) \cdot x4 \]
(FPCore (x1 x2 x3 x4)
  :precision binary64
  (* (* (* x1 x2) x3) x4))
double code(double x1, double x2, double x3, double x4) {
	return ((x1 * x2) * x3) * x4;
}
real(8) function code(x1, x2, x3, x4)
    real(8), intent (in) :: x1
    real(8), intent (in) :: x2
    real(8), intent (in) :: x3
    real(8), intent (in) :: x4
    code = ((x1 * x2) * x3) * x4
end function
public static double code(double x1, double x2, double x3, double x4) {
	return ((x1 * x2) * x3) * x4;
}
def code(x1, x2, x3, x4):
	return ((x1 * x2) * x3) * x4
function code(x1, x2, x3, x4)
	return Float64(Float64(Float64(x1 * x2) * x3) * x4)
end
function tmp = code(x1, x2, x3, x4)
	tmp = ((x1 * x2) * x3) * x4;
end
code[x1_, x2_, x3_, x4_] := N[(N[(N[(x1 * x2), $MachinePrecision] * x3), $MachinePrecision] * x4), $MachinePrecision]
\left(\left(x1 \cdot x2\right) \cdot x3\right) \cdot x4

Alternative 1: 99.5% accurate, 0.5× speedup?

\[\left(\mathsf{max}\left(x3, x4\right) \cdot \mathsf{max}\left(x1, x2\right)\right) \cdot \left(\mathsf{min}\left(x3, x4\right) \cdot \mathsf{min}\left(x1, x2\right)\right) \]
(FPCore (x1 x2 x3 x4)
  :precision binary64
  (* (* (fmax x3 x4) (fmax x1 x2)) (* (fmin x3 x4) (fmin x1 x2))))
double code(double x1, double x2, double x3, double x4) {
	return (fmax(x3, x4) * fmax(x1, x2)) * (fmin(x3, x4) * fmin(x1, x2));
}
real(8) function code(x1, x2, x3, x4)
    real(8), intent (in) :: x1
    real(8), intent (in) :: x2
    real(8), intent (in) :: x3
    real(8), intent (in) :: x4
    code = (merge(x4, merge(x3, max(x3, x4), x4 /= x4), x3 /= x3) * merge(x2, merge(x1, max(x1, x2), x2 /= x2), x1 /= x1)) * (merge(x4, merge(x3, min(x3, x4), x4 /= x4), x3 /= x3) * merge(x2, merge(x1, min(x1, x2), x2 /= x2), x1 /= x1))
end function
public static double code(double x1, double x2, double x3, double x4) {
	return (fmax(x3, x4) * fmax(x1, x2)) * (fmin(x3, x4) * fmin(x1, x2));
}
def code(x1, x2, x3, x4):
	return (fmax(x3, x4) * fmax(x1, x2)) * (fmin(x3, x4) * fmin(x1, x2))
function code(x1, x2, x3, x4)
	return Float64(Float64(((x3 != x3) ? x4 : ((x4 != x4) ? x3 : max(x3, x4))) * ((x1 != x1) ? x2 : ((x2 != x2) ? x1 : max(x1, x2)))) * Float64(((x3 != x3) ? x4 : ((x4 != x4) ? x3 : min(x3, x4))) * ((x1 != x1) ? x2 : ((x2 != x2) ? x1 : min(x1, x2)))))
end
function tmp = code(x1, x2, x3, x4)
	tmp = (max(x3, x4) * max(x1, x2)) * (min(x3, x4) * min(x1, x2));
end
code[x1_, x2_, x3_, x4_] := N[(N[(N[Max[x3, x4], $MachinePrecision] * N[Max[x1, x2], $MachinePrecision]), $MachinePrecision] * N[(N[Min[x3, x4], $MachinePrecision] * N[Min[x1, x2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(\mathsf{max}\left(x3, x4\right) \cdot \mathsf{max}\left(x1, x2\right)\right) \cdot \left(\mathsf{min}\left(x3, x4\right) \cdot \mathsf{min}\left(x1, x2\right)\right)
Derivation
  1. Initial program 99.5%

    \[\left(\left(x1 \cdot x2\right) \cdot x3\right) \cdot x4 \]
  2. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(\left(x1 \cdot x2\right) \cdot x3\right) \cdot x4} \]
    2. *-commutativeN/A

      \[\leadsto \color{blue}{x4 \cdot \left(\left(x1 \cdot x2\right) \cdot x3\right)} \]
    3. lift-*.f64N/A

      \[\leadsto x4 \cdot \color{blue}{\left(\left(x1 \cdot x2\right) \cdot x3\right)} \]
    4. lift-*.f64N/A

      \[\leadsto x4 \cdot \left(\color{blue}{\left(x1 \cdot x2\right)} \cdot x3\right) \]
    5. *-commutativeN/A

      \[\leadsto x4 \cdot \left(\color{blue}{\left(x2 \cdot x1\right)} \cdot x3\right) \]
    6. associate-*l*N/A

      \[\leadsto x4 \cdot \color{blue}{\left(x2 \cdot \left(x1 \cdot x3\right)\right)} \]
    7. associate-*r*N/A

      \[\leadsto \color{blue}{\left(x4 \cdot x2\right) \cdot \left(x1 \cdot x3\right)} \]
    8. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(x4 \cdot x2\right) \cdot \left(x1 \cdot x3\right)} \]
    9. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(x4 \cdot x2\right)} \cdot \left(x1 \cdot x3\right) \]
    10. *-commutativeN/A

      \[\leadsto \left(x4 \cdot x2\right) \cdot \color{blue}{\left(x3 \cdot x1\right)} \]
    11. lower-*.f6499.5%

      \[\leadsto \left(x4 \cdot x2\right) \cdot \color{blue}{\left(x3 \cdot x1\right)} \]
  3. Applied rewrites99.5%

    \[\leadsto \color{blue}{\left(x4 \cdot x2\right) \cdot \left(x3 \cdot x1\right)} \]
  4. Add Preprocessing

Alternative 2: 99.5% accurate, 0.6× speedup?

\[\left(\mathsf{max}\left(x3, x4\right) \cdot x1\right) \cdot \left(\mathsf{min}\left(x3, x4\right) \cdot x2\right) \]
(FPCore (x1 x2 x3 x4)
  :precision binary64
  (* (* (fmax x3 x4) x1) (* (fmin x3 x4) x2)))
double code(double x1, double x2, double x3, double x4) {
	return (fmax(x3, x4) * x1) * (fmin(x3, x4) * x2);
}
real(8) function code(x1, x2, x3, x4)
    real(8), intent (in) :: x1
    real(8), intent (in) :: x2
    real(8), intent (in) :: x3
    real(8), intent (in) :: x4
    code = (merge(x4, merge(x3, max(x3, x4), x4 /= x4), x3 /= x3) * x1) * (merge(x4, merge(x3, min(x3, x4), x4 /= x4), x3 /= x3) * x2)
end function
public static double code(double x1, double x2, double x3, double x4) {
	return (fmax(x3, x4) * x1) * (fmin(x3, x4) * x2);
}
def code(x1, x2, x3, x4):
	return (fmax(x3, x4) * x1) * (fmin(x3, x4) * x2)
function code(x1, x2, x3, x4)
	return Float64(Float64(((x3 != x3) ? x4 : ((x4 != x4) ? x3 : max(x3, x4))) * x1) * Float64(((x3 != x3) ? x4 : ((x4 != x4) ? x3 : min(x3, x4))) * x2))
end
function tmp = code(x1, x2, x3, x4)
	tmp = (max(x3, x4) * x1) * (min(x3, x4) * x2);
end
code[x1_, x2_, x3_, x4_] := N[(N[(N[Max[x3, x4], $MachinePrecision] * x1), $MachinePrecision] * N[(N[Min[x3, x4], $MachinePrecision] * x2), $MachinePrecision]), $MachinePrecision]
\left(\mathsf{max}\left(x3, x4\right) \cdot x1\right) \cdot \left(\mathsf{min}\left(x3, x4\right) \cdot x2\right)
Derivation
  1. Initial program 99.5%

    \[\left(\left(x1 \cdot x2\right) \cdot x3\right) \cdot x4 \]
  2. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto \color{blue}{\left(\left(x1 \cdot x2\right) \cdot x3\right) \cdot x4} \]
    2. *-commutativeN/A

      \[\leadsto \color{blue}{x4 \cdot \left(\left(x1 \cdot x2\right) \cdot x3\right)} \]
    3. lift-*.f64N/A

      \[\leadsto x4 \cdot \color{blue}{\left(\left(x1 \cdot x2\right) \cdot x3\right)} \]
    4. lift-*.f64N/A

      \[\leadsto x4 \cdot \left(\color{blue}{\left(x1 \cdot x2\right)} \cdot x3\right) \]
    5. associate-*l*N/A

      \[\leadsto x4 \cdot \color{blue}{\left(x1 \cdot \left(x2 \cdot x3\right)\right)} \]
    6. associate-*r*N/A

      \[\leadsto \color{blue}{\left(x4 \cdot x1\right) \cdot \left(x2 \cdot x3\right)} \]
    7. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(x4 \cdot x1\right) \cdot \left(x2 \cdot x3\right)} \]
    8. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(x4 \cdot x1\right)} \cdot \left(x2 \cdot x3\right) \]
    9. *-commutativeN/A

      \[\leadsto \left(x4 \cdot x1\right) \cdot \color{blue}{\left(x3 \cdot x2\right)} \]
    10. lower-*.f6499.4%

      \[\leadsto \left(x4 \cdot x1\right) \cdot \color{blue}{\left(x3 \cdot x2\right)} \]
  3. Applied rewrites99.4%

    \[\leadsto \color{blue}{\left(x4 \cdot x1\right) \cdot \left(x3 \cdot x2\right)} \]
  4. Add Preprocessing

Reproduce

?
herbie shell --seed 1 
(FPCore (x1 x2 x3 x4)
  :name "x1 * x2 * x3 * x4"
  :precision binary64
  :pre (and (and (and (and (<= 1e-20 x1) (<= x1 2e-20)) (and (<= 1e-20 x2) (<= x2 2e-20))) (and (<= 1e+15 x3) (<= x3 2e+15))) (and (<= 1e+15 x4) (<= x4 2e+15)))
  (* (* (* x1 x2) x3) x4))