Data.Histogram.Bin.BinF:$cfromIndex from histogram-fill-0.8.4.1

Percentage Accurate: 100.0% → 100.0%
Time: 1.6s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(\frac{x}{2} + y \cdot x\right) + z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ (/ x 2.0) (* y x)) z))
double code(double x, double y, double z) {
	return ((x / 2.0) + (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 / 2.0d0) + (y * x)) + z
end function
public static double code(double x, double y, double z) {
	return ((x / 2.0) + (y * x)) + z;
}
def code(x, y, z):
	return ((x / 2.0) + (y * x)) + z
function code(x, y, z)
	return Float64(Float64(Float64(x / 2.0) + Float64(y * x)) + z)
end
function tmp = code(x, y, z)
	tmp = ((x / 2.0) + (y * x)) + z;
end
code[x_, y_, z_] := N[(N[(N[(x / 2.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}

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

\[\begin{array}{l} \\ \left(\frac{x}{2} + y \cdot x\right) + z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ (/ x 2.0) (* y x)) z))
double code(double x, double y, double z) {
	return ((x / 2.0) + (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 / 2.0d0) + (y * x)) + z
end function
public static double code(double x, double y, double z) {
	return ((x / 2.0) + (y * x)) + z;
}
def code(x, y, z):
	return ((x / 2.0) + (y * x)) + z
function code(x, y, z)
	return Float64(Float64(Float64(x / 2.0) + Float64(y * x)) + z)
end
function tmp = code(x, y, z)
	tmp = ((x / 2.0) + (y * x)) + z;
end
code[x_, y_, z_] := N[(N[(N[(x / 2.0), $MachinePrecision] + N[(y * x), $MachinePrecision]), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(\frac{x}{2} + x \cdot y\right) + z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (+ (/ x 2.0) (* x y)) z))
double code(double x, double y, double z) {
	return ((x / 2.0) + (x * y)) + 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 / 2.0d0) + (x * y)) + z
end function
public static double code(double x, double y, double z) {
	return ((x / 2.0) + (x * y)) + z;
}
def code(x, y, z):
	return ((x / 2.0) + (x * y)) + z
function code(x, y, z)
	return Float64(Float64(Float64(x / 2.0) + Float64(x * y)) + z)
end
function tmp = code(x, y, z)
	tmp = ((x / 2.0) + (x * y)) + z;
end
code[x_, y_, z_] := N[(N[(N[(x / 2.0), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}

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

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

    \[\leadsto \left(\frac{x}{2} + x \cdot y\right) + z \]

Alternative 2: 85.0% accurate, 0.4× speedup?

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

\\
\begin{array}{l}
t_0 := \frac{x}{2} + x \cdot y\\
\mathbf{if}\;t_0 \leq -1 \cdot 10^{+64} \lor \neg \left(t_0 \leq 10^{+158}\right):\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\frac{x}{2} + z\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 (/.f64 x 2) (*.f64 y x)) < -1.00000000000000002e64 or 9.99999999999999953e157 < (+.f64 (/.f64 x 2) (*.f64 y x))

    1. Initial program 100.0%

      \[\left(\frac{x}{2} + y \cdot x\right) + z \]
    2. Step-by-step derivation
      1. associate-+l+100.0%

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

        \[\leadsto \frac{x}{2} + \left(\color{blue}{x \cdot y} + z\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\frac{x}{2} + \left(x \cdot y + z\right)} \]
    4. Taylor expanded in x around inf 94.3%

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

    if -1.00000000000000002e64 < (+.f64 (/.f64 x 2) (*.f64 y x)) < 9.99999999999999953e157

    1. Initial program 100.0%

      \[\left(\frac{x}{2} + y \cdot x\right) + z \]
    2. Step-by-step derivation
      1. associate-+l+100.0%

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

        \[\leadsto \frac{x}{2} + \left(\color{blue}{x \cdot y} + z\right) \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{\frac{x}{2} + \left(x \cdot y + z\right)} \]
    4. Taylor expanded in x around 0 88.1%

      \[\leadsto \frac{x}{2} + \color{blue}{z} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification90.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{x}{2} + x \cdot y \leq -1 \cdot 10^{+64} \lor \neg \left(\frac{x}{2} + x \cdot y \leq 10^{+158}\right):\\ \;\;\;\;\frac{x}{2} + x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\frac{x}{2} + z\\ \end{array} \]

Alternative 3: 64.9% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \frac{x}{2} + z \end{array} \]
(FPCore (x y z) :precision binary64 (+ (/ x 2.0) z))
double code(double x, double y, double z) {
	return (x / 2.0) + 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 / 2.0d0) + z
end function
public static double code(double x, double y, double z) {
	return (x / 2.0) + z;
}
def code(x, y, z):
	return (x / 2.0) + z
function code(x, y, z)
	return Float64(Float64(x / 2.0) + z)
end
function tmp = code(x, y, z)
	tmp = (x / 2.0) + z;
end
code[x_, y_, z_] := N[(N[(x / 2.0), $MachinePrecision] + z), $MachinePrecision]
\begin{array}{l}

\\
\frac{x}{2} + z
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(\frac{x}{2} + y \cdot x\right) + z \]
  2. Step-by-step derivation
    1. associate-+l+100.0%

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

      \[\leadsto \frac{x}{2} + \left(\color{blue}{x \cdot y} + z\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\frac{x}{2} + \left(x \cdot y + z\right)} \]
  4. Taylor expanded in x around 0 68.4%

    \[\leadsto \frac{x}{2} + \color{blue}{z} \]
  5. Final simplification68.4%

    \[\leadsto \frac{x}{2} + z \]

Reproduce

?
herbie shell --seed 2023257 
(FPCore (x y z)
  :name "Data.Histogram.Bin.BinF:$cfromIndex from histogram-fill-0.8.4.1"
  :precision binary64
  (+ (+ (/ x 2.0) (* y x)) z))