4/(k*4 + 1) - 4/(k*4 + 3)

Percentage Accurate: 98.1% → 100.0%
Time: 3.1s
Alternatives: 4
Speedup: 1.4×

Specification

?
\[0 \leq k \land k \leq 16777216\]
\[\begin{array}{l} \\ \frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3} \end{array} \]
(FPCore (k)
 :precision binary64
 (- (/ 4.0 (+ (* k 4.0) 1.0)) (/ 4.0 (+ (* k 4.0) 3.0))))
double code(double k) {
	return (4.0 / ((k * 4.0) + 1.0)) - (4.0 / ((k * 4.0) + 3.0));
}
real(8) function code(k)
    real(8), intent (in) :: k
    code = (4.0d0 / ((k * 4.0d0) + 1.0d0)) - (4.0d0 / ((k * 4.0d0) + 3.0d0))
end function
public static double code(double k) {
	return (4.0 / ((k * 4.0) + 1.0)) - (4.0 / ((k * 4.0) + 3.0));
}
def code(k):
	return (4.0 / ((k * 4.0) + 1.0)) - (4.0 / ((k * 4.0) + 3.0))
function code(k)
	return Float64(Float64(4.0 / Float64(Float64(k * 4.0) + 1.0)) - Float64(4.0 / Float64(Float64(k * 4.0) + 3.0)))
end
function tmp = code(k)
	tmp = (4.0 / ((k * 4.0) + 1.0)) - (4.0 / ((k * 4.0) + 3.0));
end
code[k_] := N[(N[(4.0 / N[(N[(k * 4.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] - N[(4.0 / N[(N[(k * 4.0), $MachinePrecision] + 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3}
\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 4 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: 98.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3} \end{array} \]
(FPCore (k)
 :precision binary64
 (- (/ 4.0 (+ (* k 4.0) 1.0)) (/ 4.0 (+ (* k 4.0) 3.0))))
double code(double k) {
	return (4.0 / ((k * 4.0) + 1.0)) - (4.0 / ((k * 4.0) + 3.0));
}
real(8) function code(k)
    real(8), intent (in) :: k
    code = (4.0d0 / ((k * 4.0d0) + 1.0d0)) - (4.0d0 / ((k * 4.0d0) + 3.0d0))
end function
public static double code(double k) {
	return (4.0 / ((k * 4.0) + 1.0)) - (4.0 / ((k * 4.0) + 3.0));
}
def code(k):
	return (4.0 / ((k * 4.0) + 1.0)) - (4.0 / ((k * 4.0) + 3.0))
function code(k)
	return Float64(Float64(4.0 / Float64(Float64(k * 4.0) + 1.0)) - Float64(4.0 / Float64(Float64(k * 4.0) + 3.0)))
end
function tmp = code(k)
	tmp = (4.0 / ((k * 4.0) + 1.0)) - (4.0 / ((k * 4.0) + 3.0));
end
code[k_] := N[(N[(4.0 / N[(N[(k * 4.0), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision] - N[(4.0 / N[(N[(k * 4.0), $MachinePrecision] + 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3}
\end{array}

Alternative 1: 100.0% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \frac{8}{\mathsf{fma}\left(4, k, 3\right) \cdot \mathsf{fma}\left(4, k, 1\right)} \end{array} \]
(FPCore (k) :precision binary64 (/ 8.0 (* (fma 4.0 k 3.0) (fma 4.0 k 1.0))))
double code(double k) {
	return 8.0 / (fma(4.0, k, 3.0) * fma(4.0, k, 1.0));
}
function code(k)
	return Float64(8.0 / Float64(fma(4.0, k, 3.0) * fma(4.0, k, 1.0)))
end
code[k_] := N[(8.0 / N[(N[(4.0 * k + 3.0), $MachinePrecision] * N[(4.0 * k + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{8}{\mathsf{fma}\left(4, k, 3\right) \cdot \mathsf{fma}\left(4, k, 1\right)}
\end{array}
Derivation
  1. Initial program 98.0%

    \[\frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{\frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3}} \]
    2. sub-negN/A

      \[\leadsto \color{blue}{\frac{4}{k \cdot 4 + 1} + \left(\mathsf{neg}\left(\frac{4}{k \cdot 4 + 3}\right)\right)} \]
    3. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{4}{k \cdot 4 + 1}} + \left(\mathsf{neg}\left(\frac{4}{k \cdot 4 + 3}\right)\right) \]
    4. lift-/.f64N/A

      \[\leadsto \frac{4}{k \cdot 4 + 1} + \left(\mathsf{neg}\left(\color{blue}{\frac{4}{k \cdot 4 + 3}}\right)\right) \]
    5. distribute-neg-fracN/A

      \[\leadsto \frac{4}{k \cdot 4 + 1} + \color{blue}{\frac{\mathsf{neg}\left(4\right)}{k \cdot 4 + 3}} \]
    6. frac-addN/A

      \[\leadsto \color{blue}{\frac{4 \cdot \left(k \cdot 4 + 3\right) + \left(k \cdot 4 + 1\right) \cdot \left(\mathsf{neg}\left(4\right)\right)}{\left(k \cdot 4 + 1\right) \cdot \left(k \cdot 4 + 3\right)}} \]
    7. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{4 \cdot \left(k \cdot 4 + 3\right) + \left(k \cdot 4 + 1\right) \cdot \left(\mathsf{neg}\left(4\right)\right)}{\left(k \cdot 4 + 1\right) \cdot \left(k \cdot 4 + 3\right)}} \]
  4. Applied rewrites99.9%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(\mathsf{fma}\left(4, k, 3\right), 4, \mathsf{fma}\left(4, k, 1\right) \cdot -4\right)}{\mathsf{fma}\left(4, k, 3\right) \cdot \mathsf{fma}\left(4, k, 1\right)}} \]
  5. Taylor expanded in k around 0

    \[\leadsto \frac{\color{blue}{8}}{\mathsf{fma}\left(4, k, 3\right) \cdot \mathsf{fma}\left(4, k, 1\right)} \]
  6. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \frac{\color{blue}{8}}{\mathsf{fma}\left(4, k, 3\right) \cdot \mathsf{fma}\left(4, k, 1\right)} \]
    2. Add Preprocessing

    Alternative 2: 96.9% accurate, 3.2× speedup?

    \[\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(61.629629629629626, k, -14.222222222222221\right), k, 2.6666666666666665\right) \end{array} \]
    (FPCore (k)
     :precision binary64
     (fma (fma 61.629629629629626 k -14.222222222222221) k 2.6666666666666665))
    double code(double k) {
    	return fma(fma(61.629629629629626, k, -14.222222222222221), k, 2.6666666666666665);
    }
    
    function code(k)
    	return fma(fma(61.629629629629626, k, -14.222222222222221), k, 2.6666666666666665)
    end
    
    code[k_] := N[(N[(61.629629629629626 * k + -14.222222222222221), $MachinePrecision] * k + 2.6666666666666665), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \mathsf{fma}\left(\mathsf{fma}\left(61.629629629629626, k, -14.222222222222221\right), k, 2.6666666666666665\right)
    \end{array}
    
    Derivation
    1. Initial program 98.0%

      \[\frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3} \]
    2. Add Preprocessing
    3. Taylor expanded in k around 0

      \[\leadsto \color{blue}{\frac{8}{3} + k \cdot \left(\frac{1664}{27} \cdot k - \frac{128}{9}\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{k \cdot \left(\frac{1664}{27} \cdot k - \frac{128}{9}\right) + \frac{8}{3}} \]
      2. *-commutativeN/A

        \[\leadsto \color{blue}{\left(\frac{1664}{27} \cdot k - \frac{128}{9}\right) \cdot k} + \frac{8}{3} \]
      3. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{1664}{27} \cdot k - \frac{128}{9}, k, \frac{8}{3}\right)} \]
      4. sub-negN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{1664}{27} \cdot k + \left(\mathsf{neg}\left(\frac{128}{9}\right)\right)}, k, \frac{8}{3}\right) \]
      5. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(\frac{1664}{27} \cdot k + \color{blue}{\frac{-128}{9}}, k, \frac{8}{3}\right) \]
      6. lower-fma.f6496.8

        \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(61.629629629629626, k, -14.222222222222221\right)}, k, 2.6666666666666665\right) \]
    5. Applied rewrites96.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(61.629629629629626, k, -14.222222222222221\right), k, 2.6666666666666665\right)} \]
    6. Add Preprocessing

    Alternative 3: 96.4% accurate, 6.0× speedup?

    \[\begin{array}{l} \\ \mathsf{fma}\left(-14.222222222222221, k, 2.6666666666666665\right) \end{array} \]
    (FPCore (k) :precision binary64 (fma -14.222222222222221 k 2.6666666666666665))
    double code(double k) {
    	return fma(-14.222222222222221, k, 2.6666666666666665);
    }
    
    function code(k)
    	return fma(-14.222222222222221, k, 2.6666666666666665)
    end
    
    code[k_] := N[(-14.222222222222221 * k + 2.6666666666666665), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \mathsf{fma}\left(-14.222222222222221, k, 2.6666666666666665\right)
    \end{array}
    
    Derivation
    1. Initial program 98.0%

      \[\frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3} \]
    2. Add Preprocessing
    3. Taylor expanded in k around 0

      \[\leadsto \color{blue}{\frac{8}{3} + \frac{-128}{9} \cdot k} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{\frac{-128}{9} \cdot k + \frac{8}{3}} \]
      2. lower-fma.f6496.2

        \[\leadsto \color{blue}{\mathsf{fma}\left(-14.222222222222221, k, 2.6666666666666665\right)} \]
    5. Applied rewrites96.2%

      \[\leadsto \color{blue}{\mathsf{fma}\left(-14.222222222222221, k, 2.6666666666666665\right)} \]
    6. Add Preprocessing

    Alternative 4: 95.7% accurate, 42.0× speedup?

    \[\begin{array}{l} \\ 2.6666666666666665 \end{array} \]
    (FPCore (k) :precision binary64 2.6666666666666665)
    double code(double k) {
    	return 2.6666666666666665;
    }
    
    real(8) function code(k)
        real(8), intent (in) :: k
        code = 2.6666666666666665d0
    end function
    
    public static double code(double k) {
    	return 2.6666666666666665;
    }
    
    def code(k):
    	return 2.6666666666666665
    
    function code(k)
    	return 2.6666666666666665
    end
    
    function tmp = code(k)
    	tmp = 2.6666666666666665;
    end
    
    code[k_] := 2.6666666666666665
    
    \begin{array}{l}
    
    \\
    2.6666666666666665
    \end{array}
    
    Derivation
    1. Initial program 98.0%

      \[\frac{4}{k \cdot 4 + 1} - \frac{4}{k \cdot 4 + 3} \]
    2. Add Preprocessing
    3. Taylor expanded in k around 0

      \[\leadsto \color{blue}{\frac{8}{3}} \]
    4. Step-by-step derivation
      1. Applied rewrites95.3%

        \[\leadsto \color{blue}{2.6666666666666665} \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 1 
      (FPCore (k)
        :name "4/(k*4 + 1) - 4/(k*4 + 3)"
        :precision binary64
        :pre (and (<= 0.0 k) (<= k 16777216.0))
        (- (/ 4.0 (+ (* k 4.0) 1.0)) (/ 4.0 (+ (* k 4.0) 3.0))))