Main:bigenough2 from A

Percentage Accurate: 100.0% → 100.0%
Time: 2.8s
Alternatives: 5
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 5 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, 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 2: 85.7% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -2.1 \cdot 10^{+22} \lor \neg \left(z \leq 10^{-46}\right):\\ \;\;\;\;x + y \cdot z\\ \mathbf{else}:\\ \;\;\;\;x + x \cdot y\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (or (<= z -2.1e+22) (not (<= z 1e-46))) (+ x (* y z)) (+ x (* x y))))
double code(double x, double y, double z) {
	double tmp;
	if ((z <= -2.1e+22) || !(z <= 1e-46)) {
		tmp = x + (y * z);
	} else {
		tmp = x + (x * y);
	}
	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 ((z <= (-2.1d+22)) .or. (.not. (z <= 1d-46))) then
        tmp = x + (y * z)
    else
        tmp = x + (x * y)
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if ((z <= -2.1e+22) || !(z <= 1e-46)) {
		tmp = x + (y * z);
	} else {
		tmp = x + (x * y);
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if (z <= -2.1e+22) or not (z <= 1e-46):
		tmp = x + (y * z)
	else:
		tmp = x + (x * y)
	return tmp
function code(x, y, z)
	tmp = 0.0
	if ((z <= -2.1e+22) || !(z <= 1e-46))
		tmp = Float64(x + Float64(y * z));
	else
		tmp = Float64(x + Float64(x * y));
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if ((z <= -2.1e+22) || ~((z <= 1e-46)))
		tmp = x + (y * z);
	else
		tmp = x + (x * y);
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[Or[LessEqual[z, -2.1e+22], N[Not[LessEqual[z, 1e-46]], $MachinePrecision]], N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision], N[(x + N[(x * y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;z \leq -2.1 \cdot 10^{+22} \lor \neg \left(z \leq 10^{-46}\right):\\
\;\;\;\;x + y \cdot z\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -2.0999999999999998e22 or 1.00000000000000002e-46 < z

    1. Initial program 100.0%

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

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

    if -2.0999999999999998e22 < z < 1.00000000000000002e-46

    1. Initial program 100.0%

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

      \[\leadsto x + \color{blue}{x \cdot y} \]
    3. Step-by-step derivation
      1. *-commutative84.8%

        \[\leadsto x + \color{blue}{y \cdot x} \]
    4. Simplified84.8%

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

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

Alternative 3: 74.2% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;z \leq -5.8 \cdot 10^{+56}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq 3.8 \cdot 10^{-13}:\\ \;\;\;\;x + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= z -5.8e+56) (* y z) (if (<= z 3.8e-13) (+ x (* x y)) (* y z))))
double code(double x, double y, double z) {
	double tmp;
	if (z <= -5.8e+56) {
		tmp = y * z;
	} else if (z <= 3.8e-13) {
		tmp = x + (x * y);
	} else {
		tmp = 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 (z <= (-5.8d+56)) then
        tmp = y * z
    else if (z <= 3.8d-13) then
        tmp = x + (x * y)
    else
        tmp = y * z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (z <= -5.8e+56) {
		tmp = y * z;
	} else if (z <= 3.8e-13) {
		tmp = x + (x * y);
	} else {
		tmp = y * z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if z <= -5.8e+56:
		tmp = y * z
	elif z <= 3.8e-13:
		tmp = x + (x * y)
	else:
		tmp = y * z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (z <= -5.8e+56)
		tmp = Float64(y * z);
	elseif (z <= 3.8e-13)
		tmp = Float64(x + Float64(x * y));
	else
		tmp = Float64(y * z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (z <= -5.8e+56)
		tmp = y * z;
	elseif (z <= 3.8e-13)
		tmp = x + (x * y);
	else
		tmp = y * z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[z, -5.8e+56], N[(y * z), $MachinePrecision], If[LessEqual[z, 3.8e-13], N[(x + N[(x * y), $MachinePrecision]), $MachinePrecision], N[(y * z), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;z \leq 3.8 \cdot 10^{-13}:\\
\;\;\;\;x + x \cdot y\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -5.80000000000000014e56 or 3.8e-13 < z

    1. Initial program 100.0%

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

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

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

    if -5.80000000000000014e56 < z < 3.8e-13

    1. Initial program 100.0%

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

      \[\leadsto x + \color{blue}{x \cdot y} \]
    3. Step-by-step derivation
      1. *-commutative83.7%

        \[\leadsto x + \color{blue}{y \cdot x} \]
    4. Simplified83.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;z \leq -5.8 \cdot 10^{+56}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;z \leq 3.8 \cdot 10^{-13}:\\ \;\;\;\;x + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \]

Alternative 4: 59.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;y \leq -4.7 \cdot 10^{-107}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;y \leq 2.2 \cdot 10^{-82}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (if (<= y -4.7e-107) (* y z) (if (<= y 2.2e-82) x (* y z))))
double code(double x, double y, double z) {
	double tmp;
	if (y <= -4.7e-107) {
		tmp = y * z;
	} else if (y <= 2.2e-82) {
		tmp = x;
	} else {
		tmp = 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 <= (-4.7d-107)) then
        tmp = y * z
    else if (y <= 2.2d-82) then
        tmp = x
    else
        tmp = y * z
    end if
    code = tmp
end function
public static double code(double x, double y, double z) {
	double tmp;
	if (y <= -4.7e-107) {
		tmp = y * z;
	} else if (y <= 2.2e-82) {
		tmp = x;
	} else {
		tmp = y * z;
	}
	return tmp;
}
def code(x, y, z):
	tmp = 0
	if y <= -4.7e-107:
		tmp = y * z
	elif y <= 2.2e-82:
		tmp = x
	else:
		tmp = y * z
	return tmp
function code(x, y, z)
	tmp = 0.0
	if (y <= -4.7e-107)
		tmp = Float64(y * z);
	elseif (y <= 2.2e-82)
		tmp = x;
	else
		tmp = Float64(y * z);
	end
	return tmp
end
function tmp_2 = code(x, y, z)
	tmp = 0.0;
	if (y <= -4.7e-107)
		tmp = y * z;
	elseif (y <= 2.2e-82)
		tmp = x;
	else
		tmp = y * z;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_] := If[LessEqual[y, -4.7e-107], N[(y * z), $MachinePrecision], If[LessEqual[y, 2.2e-82], x, N[(y * z), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.7 \cdot 10^{-107}:\\
\;\;\;\;y \cdot z\\

\mathbf{elif}\;y \leq 2.2 \cdot 10^{-82}:\\
\;\;\;\;x\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < -4.69999999999999998e-107 or 2.19999999999999986e-82 < y

    1. Initial program 100.0%

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

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

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

    if -4.69999999999999998e-107 < y < 2.19999999999999986e-82

    1. Initial program 100.0%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -4.7 \cdot 10^{-107}:\\ \;\;\;\;y \cdot z\\ \mathbf{elif}\;y \leq 2.2 \cdot 10^{-82}:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;y \cdot z\\ \end{array} \]

Alternative 5: 36.0% 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 z around inf 79.0%

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

    \[\leadsto \color{blue}{x} \]
  4. Final simplification36.3%

    \[\leadsto x \]

Reproduce

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