Main:bigenough2 from A

Percentage Accurate: 100.0% → 100.0%
Time: 3.3s
Alternatives: 7
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ x + y \cdot \left(z + x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
	return x + (y * (z + x));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + (y * (z + x))
end function
public static double code(double x, double y, double z) {
	return x + (y * (z + x));
}
def code(x, y, z):
	return x + (y * (z + x))
function code(x, y, z)
	return Float64(x + Float64(y * Float64(z + x)))
end
function tmp = code(x, y, z)
	tmp = x + (y * (z + x));
end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + y \cdot \left(z + x\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 7 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x + y \cdot \left(z + x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (+ x (* y (+ z x))))
double code(double x, double y, double z) {
	return x + (y * (z + x));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + (y * (z + x))
end function
public static double code(double x, double y, double z) {
	return x + (y * (z + x));
}
def code(x, y, z):
	return x + (y * (z + x))
function code(x, y, z)
	return Float64(x + Float64(y * Float64(z + x)))
end
function tmp = code(x, y, z)
	tmp = x + (y * (z + x));
end
code[x_, y_, z_] := N[(x + N[(y * N[(z + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + y \cdot \left(z + x\right)
\end{array}

Alternative 1: 100.0% accurate, 0.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(y, x + z, x\right) \end{array} \]
(FPCore (x y z) :precision binary64 (fma y (+ x z) x))
double code(double x, double y, double z) {
	return fma(y, (x + z), x);
}
function code(x, y, z)
	return fma(y, Float64(x + z), x)
end
code[x_, y_, z_] := N[(y * N[(x + z), $MachinePrecision] + x), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(y, x + z, x\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[x + y \cdot \left(z + x\right) \]
  2. Step-by-step derivation
    1. +-commutative100.0%

      \[\leadsto \color{blue}{y \cdot \left(z + x\right) + x} \]
    2. fma-def100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(y, z + x, x\right)} \]
    3. +-commutative100.0%

      \[\leadsto \mathsf{fma}\left(y, \color{blue}{x + z}, x\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(y, x + z, x\right)} \]
  4. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(y, x + z, x\right) \]

Alternative 2: 62.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -7 \cdot 10^{+120}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;y \leq -7 \cdot 10^{+30}:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq -5.8 \cdot 10^{-19}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;y \leq 4.1 \cdot 10^{-18}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \leq 6.2 \cdot 10^{+64}:\\ \;\;\;\;y \cdot z\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -7e+120)
   (* y z)
   (if (<= y -7e+30)
     (* y x)
     (if (<= y -5.8e-19)
       (* y z)
       (if (<= y 4.1e-18) x (if (<= y 6.2e+64) (* y z) (* y x)))))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -7e+120) {
		tmp = y * z;
	} else if (y <= -7e+30) {
		tmp = y * x;
	} else if (y <= -5.8e-19) {
		tmp = y * z;
	} else if (y <= 4.1e-18) {
		tmp = x;
	} else if (y <= 6.2e+64) {
		tmp = y * z;
	} else {
		tmp = y * x;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (y <= (-7d+120)) then
        tmp = y * z
    else if (y <= (-7d+30)) then
        tmp = y * x
    else if (y <= (-5.8d-19)) then
        tmp = y * z
    else if (y <= 4.1d-18) then
        tmp = x
    else if (y <= 6.2d+64) then
        tmp = y * z
    else
        tmp = y * x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -7e+120) {
		tmp = y * z;
	} else if (y <= -7e+30) {
		tmp = y * x;
	} else if (y <= -5.8e-19) {
		tmp = y * z;
	} else if (y <= 4.1e-18) {
		tmp = x;
	} else if (y <= 6.2e+64) {
		tmp = y * z;
	} else {
		tmp = y * x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= -7e+120:
		tmp = y * z
	elif y <= -7e+30:
		tmp = y * x
	elif y <= -5.8e-19:
		tmp = y * z
	elif y <= 4.1e-18:
		tmp = x
	elif y <= 6.2e+64:
		tmp = y * z
	else:
		tmp = y * x
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= -7e+120)
		tmp = Float64(y * z);
	elseif (y <= -7e+30)
		tmp = Float64(y * x);
	elseif (y <= -5.8e-19)
		tmp = Float64(y * z);
	elseif (y <= 4.1e-18)
		tmp = x;
	elseif (y <= 6.2e+64)
		tmp = Float64(y * z);
	else
		tmp = Float64(y * x);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -7e+120)
		tmp = y * z;
	elseif (y <= -7e+30)
		tmp = y * x;
	elseif (y <= -5.8e-19)
		tmp = y * z;
	elseif (y <= 4.1e-18)
		tmp = x;
	elseif (y <= 6.2e+64)
		tmp = y * z;
	else
		tmp = y * x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, -7e+120], N[(y * z), $MachinePrecision], If[LessEqual[y, -7e+30], N[(y * x), $MachinePrecision], If[LessEqual[y, -5.8e-19], N[(y * z), $MachinePrecision], If[LessEqual[y, 4.1e-18], x, If[LessEqual[y, 6.2e+64], N[(y * z), $MachinePrecision], N[(y * x), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -7 \cdot 10^{+120}:\\
\;\;\;\;y \cdot z\\

\mathbf{elif}\;y \leq -7 \cdot 10^{+30}:\\
\;\;\;\;y \cdot x\\

\mathbf{elif}\;y \leq -5.8 \cdot 10^{-19}:\\
\;\;\;\;y \cdot z\\

\mathbf{elif}\;y \leq 4.1 \cdot 10^{-18}:\\
\;\;\;\;x\\

\mathbf{elif}\;y \leq 6.2 \cdot 10^{+64}:\\
\;\;\;\;y \cdot z\\

\mathbf{else}:\\
\;\;\;\;y \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < -7.00000000000000015e120 or -7.00000000000000042e30 < y < -5.8e-19 or 4.0999999999999998e-18 < y < 6.1999999999999998e64

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in z around inf 69.8%

      \[\leadsto x + \color{blue}{y \cdot z} \]
    3. Taylor expanded in x around 0 66.2%

      \[\leadsto \color{blue}{y \cdot z} \]

    if -7.00000000000000015e120 < y < -7.00000000000000042e30 or 6.1999999999999998e64 < y

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in x around inf 66.3%

      \[\leadsto \color{blue}{\left(1 + y\right) \cdot x} \]
    3. Taylor expanded in y around inf 66.3%

      \[\leadsto \color{blue}{y \cdot x} \]

    if -5.8e-19 < y < 4.0999999999999998e-18

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in y around 0 78.0%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification71.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -7 \cdot 10^{+120}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;y \leq -7 \cdot 10^{+30}:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq -5.8 \cdot 10^{-19}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;y \leq 4.1 \cdot 10^{-18}:\\ \;\;\;\;x\\ \mathbf{elif}\;y \leq 6.2 \cdot 10^{+64}:\\ \;\;\;\;y \cdot z\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]

Alternative 3: 85.0% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -6.1 \cdot 10^{-19} \lor \neg \left(y \leq 3.4 \cdot 10^{-18}\right):\\ \;\;\;\;y \cdot \left(x + z\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= y -6.1e-19) (not (<= y 3.4e-18))) (* y (+ x z)) x))
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -6.1e-19) || !(y <= 3.4e-18)) {
		tmp = y * (x + z);
	} else {
		tmp = x;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if ((y <= (-6.1d-19)) .or. (.not. (y <= 3.4d-18))) then
        tmp = y * (x + z)
    else
        tmp = x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((y <= -6.1e-19) || !(y <= 3.4e-18)) {
		tmp = y * (x + z);
	} else {
		tmp = x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (y <= -6.1e-19) or not (y <= 3.4e-18):
		tmp = y * (x + z)
	else:
		tmp = x
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((y <= -6.1e-19) || !(y <= 3.4e-18))
		tmp = Float64(y * Float64(x + z));
	else
		tmp = x;
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((y <= -6.1e-19) || ~((y <= 3.4e-18)))
		tmp = y * (x + z);
	else
		tmp = x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[y, -6.1e-19], N[Not[LessEqual[y, 3.4e-18]], $MachinePrecision]], N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision], x]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -6.1 \cdot 10^{-19} \lor \neg \left(y \leq 3.4 \cdot 10^{-18}\right):\\
\;\;\;\;y \cdot \left(x + z\right)\\

\mathbf{else}:\\
\;\;\;\;x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -6.1000000000000003e-19 or 3.40000000000000001e-18 < y

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in y around inf 97.0%

      \[\leadsto \color{blue}{y \cdot \left(z + x\right)} \]

    if -6.1000000000000003e-19 < y < 3.40000000000000001e-18

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in y around 0 78.0%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification88.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -6.1 \cdot 10^{-19} \lor \neg \left(y \leq 3.4 \cdot 10^{-18}\right):\\ \;\;\;\;y \cdot \left(x + z\right)\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Alternative 4: 99.0% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -56 \lor \neg \left(y \leq 0.0004\right):\\ \;\;\;\;y \cdot \left(x + z\right)\\ \mathbf{else}:\\ \;\;\;\;x + y \cdot z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= y -56.0) (not (<= y 0.0004))) (* y (+ x z)) (+ x (* y z))))
double code(double x, double y, double z) {
	double tmp;
	if ((y <= -56.0) || !(y <= 0.0004)) {
		tmp = y * (x + z);
	} else {
		tmp = x + (y * z);
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if ((y <= (-56.0d0)) .or. (.not. (y <= 0.0004d0))) then
        tmp = y * (x + z)
    else
        tmp = x + (y * z)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((y <= -56.0) || !(y <= 0.0004)) {
		tmp = y * (x + z);
	} else {
		tmp = x + (y * z);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (y <= -56.0) or not (y <= 0.0004):
		tmp = y * (x + z)
	else:
		tmp = x + (y * z)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((y <= -56.0) || !(y <= 0.0004))
		tmp = Float64(y * Float64(x + z));
	else
		tmp = Float64(x + Float64(y * z));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((y <= -56.0) || ~((y <= 0.0004)))
		tmp = y * (x + z);
	else
		tmp = x + (y * z);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[y, -56.0], N[Not[LessEqual[y, 0.0004]], $MachinePrecision]], N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision], N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -56 \lor \neg \left(y \leq 0.0004\right):\\
\;\;\;\;y \cdot \left(x + z\right)\\

\mathbf{else}:\\
\;\;\;\;x + y \cdot z\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -56 or 4.00000000000000019e-4 < y

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in y around inf 99.7%

      \[\leadsto \color{blue}{y \cdot \left(z + x\right)} \]

    if -56 < y < 4.00000000000000019e-4

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in z around inf 99.2%

      \[\leadsto x + \color{blue}{y \cdot z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -56 \lor \neg \left(y \leq 0.0004\right):\\ \;\;\;\;y \cdot \left(x + z\right)\\ \mathbf{else}:\\ \;\;\;\;x + y \cdot z\\ \end{array} \]

Alternative 5: 61.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -56:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 0.0004:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -56.0) (* y x) (if (<= y 0.0004) x (* y x))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -56.0) {
		tmp = y * x;
	} else if (y <= 0.0004) {
		tmp = x;
	} else {
		tmp = y * x;
	}
	return tmp;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8) :: tmp
    if (y <= (-56.0d0)) then
        tmp = y * x
    else if (y <= 0.0004d0) then
        tmp = x
    else
        tmp = y * x
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -56.0) {
		tmp = y * x;
	} else if (y <= 0.0004) {
		tmp = x;
	} else {
		tmp = y * x;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= -56.0:
		tmp = y * x
	elif y <= 0.0004:
		tmp = x
	else:
		tmp = y * x
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= -56.0)
		tmp = Float64(y * x);
	elseif (y <= 0.0004)
		tmp = x;
	else
		tmp = Float64(y * x);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -56.0)
		tmp = y * x;
	elseif (y <= 0.0004)
		tmp = x;
	else
		tmp = y * x;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, -56.0], N[(y * x), $MachinePrecision], If[LessEqual[y, 0.0004], x, N[(y * x), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -56:\\
\;\;\;\;y \cdot x\\

\mathbf{elif}\;y \leq 0.0004:\\
\;\;\;\;x\\

\mathbf{else}:\\
\;\;\;\;y \cdot x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -56 or 4.00000000000000019e-4 < y

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in x around inf 52.9%

      \[\leadsto \color{blue}{\left(1 + y\right) \cdot x} \]
    3. Taylor expanded in y around inf 52.6%

      \[\leadsto \color{blue}{y \cdot x} \]

    if -56 < y < 4.00000000000000019e-4

    1. Initial program 100.0%

      \[x + y \cdot \left(z + x\right) \]
    2. Taylor expanded in y around 0 72.8%

      \[\leadsto \color{blue}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification63.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -56:\\ \;\;\;\;y \cdot x\\ \mathbf{elif}\;y \leq 0.0004:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot x\\ \end{array} \]

Alternative 6: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ x + y \cdot \left(x + z\right) \end{array} \]
(FPCore (x y z) :precision binary64 (+ x (* y (+ x z))))
double code(double x, double y, double z) {
	return x + (y * (x + z));
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x + (y * (x + z))
end function
public static double code(double x, double y, double z) {
	return x + (y * (x + z));
}
def code(x, y, z):
	return x + (y * (x + z))
function code(x, y, z)
	return Float64(x + Float64(y * Float64(x + z)))
end
function tmp = code(x, y, z)
	tmp = x + (y * (x + z));
end
code[x_, y_, z_] := N[(x + N[(y * N[(x + z), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
x + y \cdot \left(x + z\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[x + y \cdot \left(z + x\right) \]
  2. Final simplification100.0%

    \[\leadsto x + y \cdot \left(x + z\right) \]

Alternative 7: 37.3% accurate, 7.0× speedup?

\[\begin{array}{l} \\ x \end{array} \]
(FPCore (x y z) :precision binary64 x)
double code(double x, double y, double z) {
	return x;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = x
end function
public static double code(double x, double y, double z) {
	return x;
}
def code(x, y, z):
	return x
function code(x, y, z)
	return x
end
function tmp = code(x, y, z)
	tmp = x;
end
code[x_, y_, z_] := x
\begin{array}{l}

\\
x
\end{array}
Derivation
  1. Initial program 100.0%

    \[x + y \cdot \left(z + x\right) \]
  2. Taylor expanded in y around 0 38.9%

    \[\leadsto \color{blue}{x} \]
  3. Final simplification38.9%

    \[\leadsto x \]

Reproduce

?
herbie shell --seed 2023275 
(FPCore (x y z)
  :name "Main:bigenough2 from A"
  :precision binary64
  (+ x (* y (+ z x))))