Data.Metrics.Snapshot:quantile from metrics-0.3.0.2

Percentage Accurate: 100.0% → 100.0%
Time: 10.8s
Alternatives: 12
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 12 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} \\ 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}
Derivation
  1. Initial program 100.0%

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

Alternative 2: 49.1% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := z \cdot \left(-t\right)\\ \mathbf{if}\;y \leq -3.5 \cdot 10^{+132}:\\ \;\;\;\;y \cdot t\\ \mathbf{elif}\;y \leq -3.9 \cdot 10^{+49}:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{elif}\;y \leq -5.5 \cdot 10^{-7}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;y \leq 1.32 \cdot 10^{-204}:\\ \;\;\;\;\mathsf{fma}\left(x, z, x\right)\\ \mathbf{elif}\;y \leq 2.1 \cdot 10^{-137}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;y \leq 1.22 \cdot 10^{-23}:\\ \;\;\;\;\mathsf{fma}\left(x, z, x\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot t\\ \end{array} \end{array} \]
(FPCore (x y z t)
 :precision binary64
 (let* ((t_1 (* z (- t))))
   (if (<= y -3.5e+132)
     (* y t)
     (if (<= y -3.9e+49)
       (* y (- x))
       (if (<= y -5.5e-7)
         t_1
         (if (<= y 1.32e-204)
           (fma x z x)
           (if (<= y 2.1e-137)
             t_1
             (if (<= y 1.22e-23) (fma x z x) (* y t)))))))))
double code(double x, double y, double z, double t) {
	double t_1 = z * -t;
	double tmp;
	if (y <= -3.5e+132) {
		tmp = y * t;
	} else if (y <= -3.9e+49) {
		tmp = y * -x;
	} else if (y <= -5.5e-7) {
		tmp = t_1;
	} else if (y <= 1.32e-204) {
		tmp = fma(x, z, x);
	} else if (y <= 2.1e-137) {
		tmp = t_1;
	} else if (y <= 1.22e-23) {
		tmp = fma(x, z, x);
	} else {
		tmp = y * t;
	}
	return tmp;
}
function code(x, y, z, t)
	t_1 = Float64(z * Float64(-t))
	tmp = 0.0
	if (y <= -3.5e+132)
		tmp = Float64(y * t);
	elseif (y <= -3.9e+49)
		tmp = Float64(y * Float64(-x));
	elseif (y <= -5.5e-7)
		tmp = t_1;
	elseif (y <= 1.32e-204)
		tmp = fma(x, z, x);
	elseif (y <= 2.1e-137)
		tmp = t_1;
	elseif (y <= 1.22e-23)
		tmp = fma(x, z, x);
	else
		tmp = Float64(y * t);
	end
	return tmp
end
code[x_, y_, z_, t_] := Block[{t$95$1 = N[(z * (-t)), $MachinePrecision]}, If[LessEqual[y, -3.5e+132], N[(y * t), $MachinePrecision], If[LessEqual[y, -3.9e+49], N[(y * (-x)), $MachinePrecision], If[LessEqual[y, -5.5e-7], t$95$1, If[LessEqual[y, 1.32e-204], N[(x * z + x), $MachinePrecision], If[LessEqual[y, 2.1e-137], t$95$1, If[LessEqual[y, 1.22e-23], N[(x * z + x), $MachinePrecision], N[(y * t), $MachinePrecision]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := z \cdot \left(-t\right)\\
\mathbf{if}\;y \leq -3.5 \cdot 10^{+132}:\\
\;\;\;\;y \cdot t\\

\mathbf{elif}\;y \leq -3.9 \cdot 10^{+49}:\\
\;\;\;\;y \cdot \left(-x\right)\\

\mathbf{elif}\;y \leq -5.5 \cdot 10^{-7}:\\
\;\;\;\;t\_1\\

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

\mathbf{elif}\;y \leq 2.1 \cdot 10^{-137}:\\
\;\;\;\;t\_1\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if y < -3.5000000000000002e132 or 1.22000000000000007e-23 < y

    1. Initial program 100.0%

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

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(y, t - x, x\right)} \]
      3. lower--.f6480.7

        \[\leadsto \mathsf{fma}\left(y, \color{blue}{t - x}, x\right) \]
    5. Applied rewrites80.7%

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

      \[\leadsto t \cdot \color{blue}{y} \]
    7. Step-by-step derivation
      1. Applied rewrites43.3%

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

      if -3.5000000000000002e132 < y < -3.9000000000000001e49

      1. Initial program 100.0%

        \[x + \left(y - z\right) \cdot \left(t - x\right) \]
      2. Add Preprocessing
      3. Step-by-step derivation
        1. lift-+.f64N/A

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

          \[\leadsto \color{blue}{\left(y - z\right) \cdot \left(t - x\right) + x} \]
        3. lift-*.f64N/A

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

          \[\leadsto \left(y - z\right) \cdot \color{blue}{\left(t - x\right)} + x \]
        5. sub-negN/A

          \[\leadsto \left(y - z\right) \cdot \color{blue}{\left(t + \left(\mathsf{neg}\left(x\right)\right)\right)} + x \]
        6. distribute-lft-inN/A

          \[\leadsto \color{blue}{\left(\left(y - z\right) \cdot t + \left(y - z\right) \cdot \left(\mathsf{neg}\left(x\right)\right)\right)} + x \]
        7. associate-+l+N/A

          \[\leadsto \color{blue}{\left(y - z\right) \cdot t + \left(\left(y - z\right) \cdot \left(\mathsf{neg}\left(x\right)\right) + x\right)} \]
        8. *-commutativeN/A

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

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

          \[\leadsto \mathsf{fma}\left(y - z, t, \color{blue}{\left(y - z\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} + x\right) \]
        11. lower-fma.f64N/A

          \[\leadsto \mathsf{fma}\left(y - z, t, \color{blue}{\mathsf{fma}\left(y - z, \mathsf{neg}\left(x\right), x\right)}\right) \]
        12. lower-neg.f6499.0

          \[\leadsto \mathsf{fma}\left(y - z, t, \mathsf{fma}\left(y - z, \color{blue}{-x}, x\right)\right) \]
      4. Applied rewrites99.0%

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

        \[\leadsto \color{blue}{y \cdot \left(t + -1 \cdot x\right)} \]
      6. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto y \cdot \left(t + \color{blue}{\left(\mathsf{neg}\left(x\right)\right)}\right) \]
        2. sub-negN/A

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

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

          \[\leadsto y \cdot \color{blue}{\left(t - x\right)} \]
      7. Applied rewrites72.1%

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

        \[\leadsto y \cdot \left(-1 \cdot \color{blue}{x}\right) \]
      9. Step-by-step derivation
        1. Applied rewrites39.1%

          \[\leadsto y \cdot \left(-x\right) \]

        if -3.9000000000000001e49 < y < -5.5000000000000003e-7 or 1.32e-204 < y < 2.09999999999999992e-137

        1. Initial program 100.0%

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

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

            \[\leadsto \color{blue}{\mathsf{neg}\left(z \cdot \left(t - x\right)\right)} \]
          2. distribute-rgt-neg-inN/A

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

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

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

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

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

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

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

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

            \[\leadsto z \cdot \left(\color{blue}{x} - t\right) \]
          11. lower--.f6455.2

            \[\leadsto z \cdot \color{blue}{\left(x - t\right)} \]
        5. Applied rewrites55.2%

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

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

            \[\leadsto z \cdot \left(-t\right) \]

          if -5.5000000000000003e-7 < y < 1.32e-204 or 2.09999999999999992e-137 < y < 1.22000000000000007e-23

          1. Initial program 100.0%

            \[x + \left(y - z\right) \cdot \left(t - x\right) \]
          2. Add Preprocessing
          3. Step-by-step derivation
            1. lift-+.f64N/A

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

              \[\leadsto \color{blue}{\left(y - z\right) \cdot \left(t - x\right) + x} \]
            3. lift-*.f64N/A

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

              \[\leadsto \left(y - z\right) \cdot \color{blue}{\left(t - x\right)} + x \]
            5. sub-negN/A

              \[\leadsto \left(y - z\right) \cdot \color{blue}{\left(t + \left(\mathsf{neg}\left(x\right)\right)\right)} + x \]
            6. distribute-lft-inN/A

              \[\leadsto \color{blue}{\left(\left(y - z\right) \cdot t + \left(y - z\right) \cdot \left(\mathsf{neg}\left(x\right)\right)\right)} + x \]
            7. associate-+l+N/A

              \[\leadsto \color{blue}{\left(y - z\right) \cdot t + \left(\left(y - z\right) \cdot \left(\mathsf{neg}\left(x\right)\right) + x\right)} \]
            8. *-commutativeN/A

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

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

              \[\leadsto \mathsf{fma}\left(y - z, t, \color{blue}{\left(y - z\right) \cdot \left(\mathsf{neg}\left(x\right)\right)} + x\right) \]
            11. lower-fma.f64N/A

              \[\leadsto \mathsf{fma}\left(y - z, t, \color{blue}{\mathsf{fma}\left(y - z, \mathsf{neg}\left(x\right), x\right)}\right) \]
            12. lower-neg.f6499.1

              \[\leadsto \mathsf{fma}\left(y - z, t, \mathsf{fma}\left(y - z, \color{blue}{-x}, x\right)\right) \]
          4. Applied rewrites99.1%

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

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

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

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

              \[\leadsto \left(x \cdot z + \color{blue}{\left(-1 \cdot t\right) \cdot z}\right) + x \]
            4. distribute-rgt-inN/A

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

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

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

              \[\leadsto \mathsf{fma}\left(z, \color{blue}{x - t}, x\right) \]
            8. lower--.f6491.4

              \[\leadsto \mathsf{fma}\left(z, \color{blue}{x - t}, x\right) \]
          7. Applied rewrites91.4%

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

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

              \[\leadsto \mathsf{fma}\left(x, \color{blue}{z}, x\right) \]
          10. Recombined 4 regimes into one program.
          11. 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 2024223 
          (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))))