Graphics.Rendering.Chart.SparkLine:renderSparkLine from Chart-1.5.3

Percentage Accurate: 97.2% → 99.6%
Time: 10.6s
Alternatives: 12
Speedup: 1.3×

Specification

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

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

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

\\
x - \frac{y - z}{\frac{\left(t - z\right) + 1}{a}}
\end{array}

Alternative 1: 99.6% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\frac{y - z}{-1 + \left(z - t\right)}, a, x\right) \end{array} \]
(FPCore (x y z t a) :precision binary64 (fma (/ (- y z) (+ -1.0 (- z t))) a x))
double code(double x, double y, double z, double t, double a) {
	return fma(((y - z) / (-1.0 + (z - t))), a, x);
}
function code(x, y, z, t, a)
	return fma(Float64(Float64(y - z) / Float64(-1.0 + Float64(z - t))), a, x)
end
code[x_, y_, z_, t_, a_] := N[(N[(N[(y - z), $MachinePrecision] / N[(-1.0 + N[(z - t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * a + x), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\frac{y - z}{-1 + \left(z - t\right)}, a, x\right)
\end{array}
Derivation
  1. Initial program 96.9%

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

      \[\leadsto \color{blue}{x - \frac{y - z}{\frac{\left(t - z\right) + 1}{a}}} \]
    2. sub-negN/A

      \[\leadsto \color{blue}{x + \left(\mathsf{neg}\left(\frac{y - z}{\frac{\left(t - z\right) + 1}{a}}\right)\right)} \]
    3. +-commutativeN/A

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

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\frac{y - z}{\frac{\left(t - z\right) + 1}{a}}}\right)\right) + x \]
    5. lift-/.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\frac{y - z}{\color{blue}{\frac{\left(t - z\right) + 1}{a}}}\right)\right) + x \]
    6. associate-/r/N/A

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\frac{y - z}{\color{blue}{\left(\mathsf{neg}\left(1\right)\right) + \left(\mathsf{neg}\left(\left(t - z\right)\right)\right)}}, a, x\right) \]
    14. metadata-evalN/A

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

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

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y - z}{-1 - \left(t - z\right)}, a, x\right)} \]
  5. Final simplification99.6%

    \[\leadsto \mathsf{fma}\left(\frac{y - z}{-1 + \left(z - t\right)}, a, x\right) \]
  6. Add Preprocessing

Alternative 2: 63.4% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := y \cdot \left(-a\right)\\ t_2 := \frac{y - z}{\frac{\left(t - z\right) + 1}{a}}\\ \mathbf{if}\;t\_2 \leq -1 \cdot 10^{+286}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_2 \leq 5 \cdot 10^{+288}:\\ \;\;\;\;x - a\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y z t a)
 :precision binary64
 (let* ((t_1 (* y (- a))) (t_2 (/ (- y z) (/ (+ (- t z) 1.0) a))))
   (if (<= t_2 -1e+286) t_1 (if (<= t_2 5e+288) (- x a) t_1))))
double code(double x, double y, double z, double t, double a) {
	double t_1 = y * -a;
	double t_2 = (y - z) / (((t - z) + 1.0) / a);
	double tmp;
	if (t_2 <= -1e+286) {
		tmp = t_1;
	} else if (t_2 <= 5e+288) {
		tmp = x - a;
	} else {
		tmp = t_1;
	}
	return tmp;
}
real(8) function code(x, y, z, t, a)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    real(8), intent (in) :: t
    real(8), intent (in) :: a
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_1 = y * -a
    t_2 = (y - z) / (((t - z) + 1.0d0) / a)
    if (t_2 <= (-1d+286)) then
        tmp = t_1
    else if (t_2 <= 5d+288) then
        tmp = x - a
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double x, double y, double z, double t, double a) {
	double t_1 = y * -a;
	double t_2 = (y - z) / (((t - z) + 1.0) / a);
	double tmp;
	if (t_2 <= -1e+286) {
		tmp = t_1;
	} else if (t_2 <= 5e+288) {
		tmp = x - a;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(x, y, z, t, a):
	t_1 = y * -a
	t_2 = (y - z) / (((t - z) + 1.0) / a)
	tmp = 0
	if t_2 <= -1e+286:
		tmp = t_1
	elif t_2 <= 5e+288:
		tmp = x - a
	else:
		tmp = t_1
	return tmp
function code(x, y, z, t, a)
	t_1 = Float64(y * Float64(-a))
	t_2 = Float64(Float64(y - z) / Float64(Float64(Float64(t - z) + 1.0) / a))
	tmp = 0.0
	if (t_2 <= -1e+286)
		tmp = t_1;
	elseif (t_2 <= 5e+288)
		tmp = Float64(x - a);
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(x, y, z, t, a)
	t_1 = y * -a;
	t_2 = (y - z) / (((t - z) + 1.0) / a);
	tmp = 0.0;
	if (t_2 <= -1e+286)
		tmp = t_1;
	elseif (t_2 <= 5e+288)
		tmp = x - a;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[x_, y_, z_, t_, a_] := Block[{t$95$1 = N[(y * (-a)), $MachinePrecision]}, Block[{t$95$2 = N[(N[(y - z), $MachinePrecision] / N[(N[(N[(t - z), $MachinePrecision] + 1.0), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$2, -1e+286], t$95$1, If[LessEqual[t$95$2, 5e+288], N[(x - a), $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := y \cdot \left(-a\right)\\
t_2 := \frac{y - z}{\frac{\left(t - z\right) + 1}{a}}\\
\mathbf{if}\;t\_2 \leq -1 \cdot 10^{+286}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_2 \leq 5 \cdot 10^{+288}:\\
\;\;\;\;x - a\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (-.f64 y z) (/.f64 (+.f64 (-.f64 t z) #s(literal 1 binary64)) a)) < -1.00000000000000003e286 or 5.0000000000000003e288 < (/.f64 (-.f64 y z) (/.f64 (+.f64 (-.f64 t z) #s(literal 1 binary64)) a))

    1. Initial program 99.9%

      \[x - \frac{y - z}{\frac{\left(t - z\right) + 1}{a}} \]
    2. Add Preprocessing
    3. Taylor expanded in t around 0

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

        \[\leadsto \color{blue}{x + \left(\mathsf{neg}\left(\frac{a \cdot \left(y - z\right)}{1 - z}\right)\right)} \]
      2. +-commutativeN/A

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

        \[\leadsto \left(\mathsf{neg}\left(\frac{\color{blue}{\left(y - z\right) \cdot a}}{1 - z}\right)\right) + x \]
      4. associate-/l*N/A

        \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(y - z\right) \cdot \frac{a}{1 - z}}\right)\right) + x \]
      5. distribute-rgt-neg-inN/A

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(y - z, \mathsf{neg}\left(\frac{a}{1 - z}\right), x\right)} \]
      7. lower--.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{y - z}, \mathsf{neg}\left(\frac{a}{1 - z}\right), x\right) \]
      8. distribute-frac-negN/A

        \[\leadsto \mathsf{fma}\left(y - z, \color{blue}{\frac{\mathsf{neg}\left(a\right)}{1 - z}}, x\right) \]
      9. lower-/.f64N/A

        \[\leadsto \mathsf{fma}\left(y - z, \color{blue}{\frac{\mathsf{neg}\left(a\right)}{1 - z}}, x\right) \]
      10. lower-neg.f64N/A

        \[\leadsto \mathsf{fma}\left(y - z, \frac{\color{blue}{\mathsf{neg}\left(a\right)}}{1 - z}, x\right) \]
      11. lower--.f6486.8

        \[\leadsto \mathsf{fma}\left(y - z, \frac{-a}{\color{blue}{1 - z}}, x\right) \]
    5. Applied rewrites86.8%

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

      \[\leadsto -1 \cdot \color{blue}{\frac{a \cdot y}{1 - z}} \]
    7. Step-by-step derivation
      1. Applied rewrites71.5%

        \[\leadsto \frac{a \cdot y}{\color{blue}{-\left(1 - z\right)}} \]
      2. Taylor expanded in z around 0

        \[\leadsto -1 \cdot \left(a \cdot \color{blue}{y}\right) \]
      3. Step-by-step derivation
        1. Applied rewrites61.8%

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

        if -1.00000000000000003e286 < (/.f64 (-.f64 y z) (/.f64 (+.f64 (-.f64 t z) #s(literal 1 binary64)) a)) < 5.0000000000000003e288

        1. Initial program 97.0%

          \[x - \frac{y - z}{\frac{\left(t - z\right) + 1}{a}} \]
        2. Add Preprocessing
        3. Taylor expanded in z around inf

          \[\leadsto \color{blue}{x - a} \]
        4. Step-by-step derivation
          1. lower--.f6463.6

            \[\leadsto \color{blue}{x - a} \]
        5. Applied rewrites63.6%

          \[\leadsto \color{blue}{x - a} \]
      4. Recombined 2 regimes into one program.
      5. Add Preprocessing

      Developer Target 1: 99.6% accurate, 1.2× speedup?

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

      Reproduce

      ?
      herbie shell --seed 2024228 
      (FPCore (x y z t a)
        :name "Graphics.Rendering.Chart.SparkLine:renderSparkLine from Chart-1.5.3"
        :precision binary64
      
        :alt
        (! :herbie-platform default (- x (* (/ (- y z) (+ (- t z) 1)) a)))
      
        (- x (/ (- y z) (/ (+ (- t z) 1.0) a))))