Data.Metrics.Snapshot:quantile from metrics-0.3.0.2

Percentage Accurate: 100.0% → 100.0%
Time: 7.7s
Alternatives: 11
Speedup: 1.0×

Specification

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

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

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

Alternative 1: 100.0% accurate, 1.0× speedup?

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

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

    \[x + \left(y - z\right) \cdot \left(t - x\right) \]
  2. Add Preprocessing
  3. Final simplification99.9%

    \[\leadsto \left(t - x\right) \cdot \left(y - z\right) + x \]
  4. Add Preprocessing

Alternative 2: 65.5% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \left(t - x\right) \cdot y\\ \mathbf{if}\;y \leq -2.3 \cdot 10^{+26}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;y \leq 1.12 \cdot 10^{-149}:\\ \;\;\;\;\mathsf{fma}\left(z, x, x\right)\\ \mathbf{elif}\;y \leq 2.3 \cdot 10^{-17}:\\ \;\;\;\;\left(-z\right) \cdot t\\ \mathbf{elif}\;y \leq 7.6 \cdot 10^{+19}:\\ \;\;\;\;\mathsf{fma}\left(z, x, x\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (let* ((t_1 (* (- t x) y)))
   (if (<= y -2.3e+26)
     t_1
     (if (<= y 1.12e-149)
       (fma z x x)
       (if (<= y 2.3e-17) (* (- z) t) (if (<= y 7.6e+19) (fma z x x) t_1))))))
double code(double x, double y, double z, double t) {
	double t_1 = (t - x) * y;
	double tmp;
	if (y <= -2.3e+26) {
		tmp = t_1;
	} else if (y <= 1.12e-149) {
		tmp = fma(z, x, x);
	} else if (y <= 2.3e-17) {
		tmp = -z * t;
	} else if (y <= 7.6e+19) {
		tmp = fma(z, x, x);
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y, z, t)
	t_1 = Float64(Float64(t - x) * y)
	tmp = 0.0
	if (y <= -2.3e+26)
		tmp = t_1;
	elseif (y <= 1.12e-149)
		tmp = fma(z, x, x);
	elseif (y <= 2.3e-17)
		tmp = Float64(Float64(-z) * t);
	elseif (y <= 7.6e+19)
		tmp = fma(z, x, x);
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(N[(t - x), $MachinePrecision] * y), $MachinePrecision]}, If[LessEqual[y, -2.3e+26], t$95$1, If[LessEqual[y, 1.12e-149], N[(z * x + x), $MachinePrecision], If[LessEqual[y, 2.3e-17], N[((-z) * t), $MachinePrecision], If[LessEqual[y, 7.6e+19], N[(z * x + x), $MachinePrecision], t$95$1]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \left(t - x\right) \cdot y\\
\mathbf{if}\;y \leq -2.3 \cdot 10^{+26}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;y \leq 1.12 \cdot 10^{-149}:\\
\;\;\;\;\mathsf{fma}\left(z, x, x\right)\\

\mathbf{elif}\;y \leq 2.3 \cdot 10^{-17}:\\
\;\;\;\;\left(-z\right) \cdot t\\

\mathbf{elif}\;y \leq 7.6 \cdot 10^{+19}:\\
\;\;\;\;\mathsf{fma}\left(z, x, x\right)\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if y < -2.3000000000000001e26 or 7.6e19 < y

    1. Initial program 100.0%

      \[x + \left(y - z\right) \cdot \left(t - x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in y around inf

      \[\leadsto \color{blue}{y \cdot \left(t - x\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(t - x\right) \cdot y} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(t - x\right) \cdot y} \]
      3. lower--.f6481.4

        \[\leadsto \color{blue}{\left(t - x\right)} \cdot y \]
    5. Applied rewrites81.4%

      \[\leadsto \color{blue}{\left(t - x\right) \cdot y} \]

    if -2.3000000000000001e26 < y < 1.11999999999999999e-149 or 2.30000000000000009e-17 < y < 7.6e19

    1. Initial program 100.0%

      \[x + \left(y - z\right) \cdot \left(t - x\right) \]
    2. Add Preprocessing
    3. Taylor expanded in y around 0

      \[\leadsto \color{blue}{x + -1 \cdot \left(z \cdot \left(t - x\right)\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{-1 \cdot \left(z \cdot \left(t - x\right)\right) + x} \]
      2. *-commutativeN/A

        \[\leadsto -1 \cdot \color{blue}{\left(\left(t - x\right) \cdot z\right)} + x \]
      3. associate-*r*N/A

        \[\leadsto \color{blue}{\left(-1 \cdot \left(t - x\right)\right) \cdot z} + x \]
      4. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(-1 \cdot \left(t - x\right), z, x\right)} \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{neg}\left(\left(t - x\right)\right)}, z, x\right) \]
      6. sub-negN/A

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\color{blue}{\left(t + \left(\mathsf{neg}\left(x\right)\right)\right)}\right), z, x\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\color{blue}{\left(\left(\mathsf{neg}\left(x\right)\right) + t\right)}\right), z, x\right) \]
      8. distribute-neg-inN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(x\right)\right)\right)\right) + \left(\mathsf{neg}\left(t\right)\right)}, z, x\right) \]
      9. unsub-negN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(x\right)\right)\right)\right) - t}, z, x\right) \]
      10. remove-double-negN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{x} - t, z, x\right) \]
      11. lower--.f6489.2

        \[\leadsto \mathsf{fma}\left(\color{blue}{x - t}, z, x\right) \]
    5. Applied rewrites89.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x - t, z, x\right)} \]
    6. Taylor expanded in x around inf

      \[\leadsto x \cdot \color{blue}{\left(1 + z\right)} \]
    7. Step-by-step derivation
      1. Applied rewrites56.2%

        \[\leadsto \mathsf{fma}\left(z, \color{blue}{x}, x\right) \]

      if 1.11999999999999999e-149 < y < 2.30000000000000009e-17

      1. Initial program 100.0%

        \[x + \left(y - z\right) \cdot \left(t - x\right) \]
      2. Add Preprocessing
      3. Taylor expanded in x around 0

        \[\leadsto \color{blue}{t \cdot \left(y - z\right)} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{\left(y - z\right) \cdot t} \]
        2. lower-*.f64N/A

          \[\leadsto \color{blue}{\left(y - z\right) \cdot t} \]
        3. lower--.f6446.4

          \[\leadsto \color{blue}{\left(y - z\right)} \cdot t \]
      5. Applied rewrites46.4%

        \[\leadsto \color{blue}{\left(y - z\right) \cdot t} \]
      6. Taylor expanded in y around 0

        \[\leadsto \left(-1 \cdot z\right) \cdot t \]
      7. Step-by-step derivation
        1. Applied rewrites32.5%

          \[\leadsto \left(-z\right) \cdot t \]
      8. Recombined 3 regimes into one program.
      9. Add Preprocessing

      Developer Target 1: 96.3% accurate, 0.6× speedup?

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

      Reproduce

      ?
      herbie shell --seed 2024230 
      (FPCore (x y z t)
        :name "Data.Metrics.Snapshot:quantile from metrics-0.3.0.2"
        :precision binary64
      
        :alt
        (! :herbie-platform default (+ x (+ (* t (- y z)) (* (- x) (- y z)))))
      
        (+ x (* (- y z) (- t x))))