Data.Metrics.Snapshot:quantile from metrics-0.3.0.2

Percentage Accurate: 100.0% → 100.0%
Time: 8.9s
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} \\ 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: 68.4% accurate, 0.5× speedup?

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

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

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

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

\mathbf{elif}\;y \leq 4800000:\\
\;\;\;\;z \cdot \left(x - t\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if y < -4.20000000000000003e43 or 4.8e6 < 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. lower-*.f64N/A

        \[\leadsto \color{blue}{y \cdot \left(t - x\right)} \]
      2. lower--.f6480.0

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

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

    if -4.20000000000000003e43 < y < -2.6e-36

    1. Initial program 99.9%

      \[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. lower-*.f64N/A

        \[\leadsto \color{blue}{t \cdot \left(y - z\right)} \]
      2. lower--.f6453.6

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

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

    if -2.6e-36 < y < 2.79999999999999993e-182

    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. mul-1-negN/A

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(z, x - t, 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 rewrites60.1%

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

      if 2.79999999999999993e-182 < y < 4.8e6

      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--.f6457.6

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

        \[\leadsto \color{blue}{z \cdot \left(x - t\right)} \]
    8. Recombined 4 regimes into one program.
    9. Final simplification68.4%

      \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -4.2 \cdot 10^{+43}:\\ \;\;\;\;y \cdot \left(t - x\right)\\ \mathbf{elif}\;y \leq -2.6 \cdot 10^{-36}:\\ \;\;\;\;\left(y - z\right) \cdot t\\ \mathbf{elif}\;y \leq 2.8 \cdot 10^{-182}:\\ \;\;\;\;\mathsf{fma}\left(x, z, x\right)\\ \mathbf{elif}\;y \leq 4800000:\\ \;\;\;\;z \cdot \left(x - t\right)\\ \mathbf{else}:\\ \;\;\;\;y \cdot \left(t - x\right)\\ \end{array} \]
    10. Add Preprocessing

    Developer Target 1: 96.0% 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 2024228 
    (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))))