(sqrt(1 + 8 * ij) - 1) / 2

Percentage Accurate: 12.9% → 99.9%
Time: 9.5s
Alternatives: 8
Speedup: 5.5×

Specification

?
\[0 \leq ij \land ij \leq 10^{+15}\]
\[\begin{array}{l} \\ \frac{\sqrt{1 + 8 \cdot ij} - 1}{2} \end{array} \]
(FPCore (ij) :precision binary64 (/ (- (sqrt (+ 1.0 (* 8.0 ij))) 1.0) 2.0))
double code(double ij) {
	return (sqrt((1.0 + (8.0 * ij))) - 1.0) / 2.0;
}
real(8) function code(ij)
    real(8), intent (in) :: ij
    code = (sqrt((1.0d0 + (8.0d0 * ij))) - 1.0d0) / 2.0d0
end function
public static double code(double ij) {
	return (Math.sqrt((1.0 + (8.0 * ij))) - 1.0) / 2.0;
}
def code(ij):
	return (math.sqrt((1.0 + (8.0 * ij))) - 1.0) / 2.0
function code(ij)
	return Float64(Float64(sqrt(Float64(1.0 + Float64(8.0 * ij))) - 1.0) / 2.0)
end
function tmp = code(ij)
	tmp = (sqrt((1.0 + (8.0 * ij))) - 1.0) / 2.0;
end
code[ij_] := N[(N[(N[Sqrt[N[(1.0 + N[(8.0 * ij), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sqrt{1 + 8 \cdot ij} - 1}{2}
\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 8 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: 12.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\sqrt{1 + 8 \cdot ij} - 1}{2} \end{array} \]
(FPCore (ij) :precision binary64 (/ (- (sqrt (+ 1.0 (* 8.0 ij))) 1.0) 2.0))
double code(double ij) {
	return (sqrt((1.0 + (8.0 * ij))) - 1.0) / 2.0;
}
real(8) function code(ij)
    real(8), intent (in) :: ij
    code = (sqrt((1.0d0 + (8.0d0 * ij))) - 1.0d0) / 2.0d0
end function
public static double code(double ij) {
	return (Math.sqrt((1.0 + (8.0 * ij))) - 1.0) / 2.0;
}
def code(ij):
	return (math.sqrt((1.0 + (8.0 * ij))) - 1.0) / 2.0
function code(ij)
	return Float64(Float64(sqrt(Float64(1.0 + Float64(8.0 * ij))) - 1.0) / 2.0)
end
function tmp = code(ij)
	tmp = (sqrt((1.0 + (8.0 * ij))) - 1.0) / 2.0;
end
code[ij_] := N[(N[(N[Sqrt[N[(1.0 + N[(8.0 * ij), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - 1.0), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sqrt{1 + 8 \cdot ij} - 1}{2}
\end{array}

Alternative 1: 99.9% accurate, 0.9× speedup?

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

\\
\frac{ij}{\sqrt{\mathsf{fma}\left(8, ij, 1\right)} + 1} \cdot 4
\end{array}
Derivation
  1. Initial program 14.1%

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

      \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij} - 1}{2}} \]
    2. lift--.f64N/A

      \[\leadsto \frac{\color{blue}{\sqrt{1 + 8 \cdot ij} - 1}}{2} \]
    3. flip--N/A

      \[\leadsto \frac{\color{blue}{\frac{\sqrt{1 + 8 \cdot ij} \cdot \sqrt{1 + 8 \cdot ij} - 1 \cdot 1}{\sqrt{1 + 8 \cdot ij} + 1}}}{2} \]
    4. associate-/l/N/A

      \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij} \cdot \sqrt{1 + 8 \cdot ij} - 1 \cdot 1}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)}} \]
    5. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij} \cdot \sqrt{1 + 8 \cdot ij} - 1 \cdot 1}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)}} \]
    6. lift-sqrt.f64N/A

      \[\leadsto \frac{\color{blue}{\sqrt{1 + 8 \cdot ij}} \cdot \sqrt{1 + 8 \cdot ij} - 1 \cdot 1}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    7. lift-sqrt.f64N/A

      \[\leadsto \frac{\sqrt{1 + 8 \cdot ij} \cdot \color{blue}{\sqrt{1 + 8 \cdot ij}} - 1 \cdot 1}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    8. rem-square-sqrtN/A

      \[\leadsto \frac{\color{blue}{\left(1 + 8 \cdot ij\right)} - 1 \cdot 1}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    9. lift-+.f64N/A

      \[\leadsto \frac{\color{blue}{\left(1 + 8 \cdot ij\right)} - 1 \cdot 1}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    10. +-commutativeN/A

      \[\leadsto \frac{\color{blue}{\left(8 \cdot ij + 1\right)} - 1 \cdot 1}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    11. metadata-evalN/A

      \[\leadsto \frac{\left(8 \cdot ij + 1\right) - \color{blue}{1}}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    12. associate--l+N/A

      \[\leadsto \frac{\color{blue}{8 \cdot ij + \left(1 - 1\right)}}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    13. metadata-evalN/A

      \[\leadsto \frac{8 \cdot ij + \color{blue}{0}}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    14. lift-*.f64N/A

      \[\leadsto \frac{\color{blue}{8 \cdot ij} + 0}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    15. *-commutativeN/A

      \[\leadsto \frac{\color{blue}{ij \cdot 8} + 0}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    16. lower-fma.f64N/A

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(ij, 8, 0\right)}}{2 \cdot \left(\sqrt{1 + 8 \cdot ij} + 1\right)} \]
    17. distribute-lft-inN/A

      \[\leadsto \frac{\mathsf{fma}\left(ij, 8, 0\right)}{\color{blue}{2 \cdot \sqrt{1 + 8 \cdot ij} + 2 \cdot 1}} \]
    18. metadata-evalN/A

      \[\leadsto \frac{\mathsf{fma}\left(ij, 8, 0\right)}{2 \cdot \sqrt{1 + 8 \cdot ij} + \color{blue}{2}} \]
    19. lower-fma.f64100.0

      \[\leadsto \frac{\mathsf{fma}\left(ij, 8, 0\right)}{\color{blue}{\mathsf{fma}\left(2, \sqrt{1 + 8 \cdot ij}, 2\right)}} \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(ij, 8, 0\right)}{\mathsf{fma}\left(2, \sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 2\right)}} \]
  5. Step-by-step derivation
    1. lift-fma.f64N/A

      \[\leadsto \frac{\color{blue}{ij \cdot 8 + 0}}{\mathsf{fma}\left(2, \sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 2\right)} \]
    2. +-rgt-identityN/A

      \[\leadsto \frac{\color{blue}{ij \cdot 8}}{\mathsf{fma}\left(2, \sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 2\right)} \]
    3. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{ij \cdot 8}{\mathsf{fma}\left(2, \sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 2\right)}} \]
    4. lift-fma.f64N/A

      \[\leadsto \frac{ij \cdot 8}{\color{blue}{2 \cdot \sqrt{\mathsf{fma}\left(ij, 8, 1\right)} + 2}} \]
    5. *-commutativeN/A

      \[\leadsto \frac{ij \cdot 8}{\color{blue}{\sqrt{\mathsf{fma}\left(ij, 8, 1\right)} \cdot 2} + 2} \]
    6. lift-sqrt.f64N/A

      \[\leadsto \frac{ij \cdot 8}{\color{blue}{\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}} \cdot 2 + 2} \]
    7. lift-fma.f64N/A

      \[\leadsto \frac{ij \cdot 8}{\sqrt{\color{blue}{ij \cdot 8 + 1}} \cdot 2 + 2} \]
    8. *-commutativeN/A

      \[\leadsto \frac{ij \cdot 8}{\sqrt{\color{blue}{8 \cdot ij} + 1} \cdot 2 + 2} \]
    9. +-commutativeN/A

      \[\leadsto \frac{ij \cdot 8}{\sqrt{\color{blue}{1 + 8 \cdot ij}} \cdot 2 + 2} \]
    10. distribute-lft1-inN/A

      \[\leadsto \frac{ij \cdot 8}{\color{blue}{\left(\sqrt{1 + 8 \cdot ij} + 1\right) \cdot 2}} \]
    11. times-fracN/A

      \[\leadsto \color{blue}{\frac{ij}{\sqrt{1 + 8 \cdot ij} + 1} \cdot \frac{8}{2}} \]
    12. metadata-evalN/A

      \[\leadsto \frac{ij}{\sqrt{1 + 8 \cdot ij} + 1} \cdot \color{blue}{4} \]
    13. metadata-evalN/A

      \[\leadsto \frac{ij}{\sqrt{1 + 8 \cdot ij} + 1} \cdot \color{blue}{\left(2 \cdot 2\right)} \]
    14. lower-*.f64N/A

      \[\leadsto \color{blue}{\frac{ij}{\sqrt{1 + 8 \cdot ij} + 1} \cdot \left(2 \cdot 2\right)} \]
  6. Applied rewrites100.0%

    \[\leadsto \color{blue}{\frac{ij}{\sqrt{\mathsf{fma}\left(8, ij, 1\right)} + 1} \cdot 4} \]
  7. Add Preprocessing

Alternative 2: 99.9% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;8 \cdot ij \leq 0.0005:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-80, ij, 16\right), ij, -4\right), ij, 2\right) \cdot ij\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 0.5, -0.5\right)\\ \end{array} \end{array} \]
(FPCore (ij)
 :precision binary64
 (if (<= (* 8.0 ij) 0.0005)
   (* (fma (fma (fma -80.0 ij 16.0) ij -4.0) ij 2.0) ij)
   (fma (sqrt (fma ij 8.0 1.0)) 0.5 -0.5)))
double code(double ij) {
	double tmp;
	if ((8.0 * ij) <= 0.0005) {
		tmp = fma(fma(fma(-80.0, ij, 16.0), ij, -4.0), ij, 2.0) * ij;
	} else {
		tmp = fma(sqrt(fma(ij, 8.0, 1.0)), 0.5, -0.5);
	}
	return tmp;
}
function code(ij)
	tmp = 0.0
	if (Float64(8.0 * ij) <= 0.0005)
		tmp = Float64(fma(fma(fma(-80.0, ij, 16.0), ij, -4.0), ij, 2.0) * ij);
	else
		tmp = fma(sqrt(fma(ij, 8.0, 1.0)), 0.5, -0.5);
	end
	return tmp
end
code[ij_] := If[LessEqual[N[(8.0 * ij), $MachinePrecision], 0.0005], N[(N[(N[(N[(-80.0 * ij + 16.0), $MachinePrecision] * ij + -4.0), $MachinePrecision] * ij + 2.0), $MachinePrecision] * ij), $MachinePrecision], N[(N[Sqrt[N[(ij * 8.0 + 1.0), $MachinePrecision]], $MachinePrecision] * 0.5 + -0.5), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;8 \cdot ij \leq 0.0005:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-80, ij, 16\right), ij, -4\right), ij, 2\right) \cdot ij\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 0.5, -0.5\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 8 binary64) ij) < 5.0000000000000001e-4

    1. Initial program 8.5%

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

      \[\leadsto \color{blue}{ij \cdot \left(2 + ij \cdot \left(ij \cdot \left(16 + -80 \cdot ij\right) - 4\right)\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(2 + ij \cdot \left(ij \cdot \left(16 + -80 \cdot ij\right) - 4\right)\right) \cdot ij} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(2 + ij \cdot \left(ij \cdot \left(16 + -80 \cdot ij\right) - 4\right)\right) \cdot ij} \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{\left(ij \cdot \left(ij \cdot \left(16 + -80 \cdot ij\right) - 4\right) + 2\right)} \cdot ij \]
      4. *-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(ij \cdot \left(16 + -80 \cdot ij\right) - 4\right) \cdot ij} + 2\right) \cdot ij \]
      5. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(ij \cdot \left(16 + -80 \cdot ij\right) - 4, ij, 2\right)} \cdot ij \]
      6. sub-negN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{ij \cdot \left(16 + -80 \cdot ij\right) + \left(\mathsf{neg}\left(4\right)\right)}, ij, 2\right) \cdot ij \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\left(16 + -80 \cdot ij\right) \cdot ij} + \left(\mathsf{neg}\left(4\right)\right), ij, 2\right) \cdot ij \]
      8. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(\left(16 + -80 \cdot ij\right) \cdot ij + \color{blue}{-4}, ij, 2\right) \cdot ij \]
      9. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(16 + -80 \cdot ij, ij, -4\right)}, ij, 2\right) \cdot ij \]
      10. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{-80 \cdot ij + 16}, ij, -4\right), ij, 2\right) \cdot ij \]
      11. lower-fma.f64100.0

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(-80, ij, 16\right)}, ij, -4\right), ij, 2\right) \cdot ij \]
    5. Applied rewrites100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(-80, ij, 16\right), ij, -4\right), ij, 2\right) \cdot ij} \]

    if 5.0000000000000001e-4 < (*.f64 #s(literal 8 binary64) ij)

    1. Initial program 98.7%

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

        \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij} - 1}{2}} \]
      2. lift--.f64N/A

        \[\leadsto \frac{\color{blue}{\sqrt{1 + 8 \cdot ij} - 1}}{2} \]
      3. div-subN/A

        \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij}}{2} - \frac{1}{2}} \]
      4. metadata-evalN/A

        \[\leadsto \frac{\sqrt{1 + 8 \cdot ij}}{2} - \color{blue}{\frac{1}{2}} \]
      5. sub-negN/A

        \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij}}{2} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)} \]
      6. metadata-evalN/A

        \[\leadsto \frac{\sqrt{1 + 8 \cdot ij}}{2} + \color{blue}{\frac{-1}{2}} \]
      7. metadata-evalN/A

        \[\leadsto \frac{\sqrt{1 + 8 \cdot ij}}{2} + \color{blue}{\frac{1}{-2}} \]
      8. metadata-evalN/A

        \[\leadsto \frac{\sqrt{1 + 8 \cdot ij}}{2} + \frac{1}{\color{blue}{\mathsf{neg}\left(2\right)}} \]
      9. div-invN/A

        \[\leadsto \color{blue}{\sqrt{1 + 8 \cdot ij} \cdot \frac{1}{2}} + \frac{1}{\mathsf{neg}\left(2\right)} \]
      10. metadata-evalN/A

        \[\leadsto \sqrt{1 + 8 \cdot ij} \cdot \color{blue}{\frac{1}{2}} + \frac{1}{\mathsf{neg}\left(2\right)} \]
      11. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{1 + 8 \cdot ij}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right)} \]
      12. lift-+.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{1 + 8 \cdot ij}}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
      13. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{8 \cdot ij + 1}}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
      14. lift-*.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{8 \cdot ij} + 1}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
      15. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{ij \cdot 8} + 1}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
      16. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{\mathsf{fma}\left(ij, 8, 1\right)}}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
      17. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, \frac{1}{2}, \frac{1}{\color{blue}{-2}}\right) \]
      18. metadata-eval98.7

        \[\leadsto \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 0.5, \color{blue}{-0.5}\right) \]
    4. Applied rewrites98.7%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 0.5, -0.5\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 99.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;8 \cdot ij \leq 0.0001:\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right) \cdot ij, ij, 2 \cdot ij\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 0.5, -0.5\right)\\ \end{array} \end{array} \]
(FPCore (ij)
 :precision binary64
 (if (<= (* 8.0 ij) 0.0001)
   (fma (* (fma 16.0 ij -4.0) ij) ij (* 2.0 ij))
   (fma (sqrt (fma ij 8.0 1.0)) 0.5 -0.5)))
double code(double ij) {
	double tmp;
	if ((8.0 * ij) <= 0.0001) {
		tmp = fma((fma(16.0, ij, -4.0) * ij), ij, (2.0 * ij));
	} else {
		tmp = fma(sqrt(fma(ij, 8.0, 1.0)), 0.5, -0.5);
	}
	return tmp;
}
function code(ij)
	tmp = 0.0
	if (Float64(8.0 * ij) <= 0.0001)
		tmp = fma(Float64(fma(16.0, ij, -4.0) * ij), ij, Float64(2.0 * ij));
	else
		tmp = fma(sqrt(fma(ij, 8.0, 1.0)), 0.5, -0.5);
	end
	return tmp
end
code[ij_] := If[LessEqual[N[(8.0 * ij), $MachinePrecision], 0.0001], N[(N[(N[(16.0 * ij + -4.0), $MachinePrecision] * ij), $MachinePrecision] * ij + N[(2.0 * ij), $MachinePrecision]), $MachinePrecision], N[(N[Sqrt[N[(ij * 8.0 + 1.0), $MachinePrecision]], $MachinePrecision] * 0.5 + -0.5), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;8 \cdot ij \leq 0.0001:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right) \cdot ij, ij, 2 \cdot ij\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 0.5, -0.5\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 #s(literal 8 binary64) ij) < 1.00000000000000005e-4

    1. Initial program 7.9%

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

      \[\leadsto \color{blue}{ij \cdot \left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right) \cdot ij} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right) \cdot ij} \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{\left(ij \cdot \left(16 \cdot ij - 4\right) + 2\right)} \cdot ij \]
      4. *-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(16 \cdot ij - 4\right) \cdot ij} + 2\right) \cdot ij \]
      5. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(16 \cdot ij - 4, ij, 2\right)} \cdot ij \]
      6. sub-negN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{16 \cdot ij + \left(\mathsf{neg}\left(4\right)\right)}, ij, 2\right) \cdot ij \]
      7. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(16 \cdot ij + \color{blue}{-4}, ij, 2\right) \cdot ij \]
      8. lower-fma.f64100.0

        \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(16, ij, -4\right)}, ij, 2\right) \cdot ij \]
    5. Applied rewrites100.0%

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

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right) \cdot ij, \color{blue}{ij}, 2 \cdot ij\right) \]

      if 1.00000000000000005e-4 < (*.f64 #s(literal 8 binary64) ij)

      1. Initial program 96.8%

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

          \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij} - 1}{2}} \]
        2. lift--.f64N/A

          \[\leadsto \frac{\color{blue}{\sqrt{1 + 8 \cdot ij} - 1}}{2} \]
        3. div-subN/A

          \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij}}{2} - \frac{1}{2}} \]
        4. metadata-evalN/A

          \[\leadsto \frac{\sqrt{1 + 8 \cdot ij}}{2} - \color{blue}{\frac{1}{2}} \]
        5. sub-negN/A

          \[\leadsto \color{blue}{\frac{\sqrt{1 + 8 \cdot ij}}{2} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)} \]
        6. metadata-evalN/A

          \[\leadsto \frac{\sqrt{1 + 8 \cdot ij}}{2} + \color{blue}{\frac{-1}{2}} \]
        7. metadata-evalN/A

          \[\leadsto \frac{\sqrt{1 + 8 \cdot ij}}{2} + \color{blue}{\frac{1}{-2}} \]
        8. metadata-evalN/A

          \[\leadsto \frac{\sqrt{1 + 8 \cdot ij}}{2} + \frac{1}{\color{blue}{\mathsf{neg}\left(2\right)}} \]
        9. div-invN/A

          \[\leadsto \color{blue}{\sqrt{1 + 8 \cdot ij} \cdot \frac{1}{2}} + \frac{1}{\mathsf{neg}\left(2\right)} \]
        10. metadata-evalN/A

          \[\leadsto \sqrt{1 + 8 \cdot ij} \cdot \color{blue}{\frac{1}{2}} + \frac{1}{\mathsf{neg}\left(2\right)} \]
        11. lower-fma.f64N/A

          \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{1 + 8 \cdot ij}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right)} \]
        12. lift-+.f64N/A

          \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{1 + 8 \cdot ij}}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
        13. +-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{8 \cdot ij + 1}}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
        14. lift-*.f64N/A

          \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{8 \cdot ij} + 1}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
        15. *-commutativeN/A

          \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{ij \cdot 8} + 1}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
        16. lower-fma.f64N/A

          \[\leadsto \mathsf{fma}\left(\sqrt{\color{blue}{\mathsf{fma}\left(ij, 8, 1\right)}}, \frac{1}{2}, \frac{1}{\mathsf{neg}\left(2\right)}\right) \]
        17. metadata-evalN/A

          \[\leadsto \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, \frac{1}{2}, \frac{1}{\color{blue}{-2}}\right) \]
        18. metadata-eval96.8

          \[\leadsto \mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 0.5, \color{blue}{-0.5}\right) \]
      4. Applied rewrites96.8%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt{\mathsf{fma}\left(ij, 8, 1\right)}, 0.5, -0.5\right)} \]
    7. Recombined 2 regimes into one program.
    8. Add Preprocessing

    Alternative 4: 95.0% accurate, 1.4× speedup?

    \[\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right) \cdot ij, ij, 2 \cdot ij\right) \end{array} \]
    (FPCore (ij) :precision binary64 (fma (* (fma 16.0 ij -4.0) ij) ij (* 2.0 ij)))
    double code(double ij) {
    	return fma((fma(16.0, ij, -4.0) * ij), ij, (2.0 * ij));
    }
    
    function code(ij)
    	return fma(Float64(fma(16.0, ij, -4.0) * ij), ij, Float64(2.0 * ij))
    end
    
    code[ij_] := N[(N[(N[(16.0 * ij + -4.0), $MachinePrecision] * ij), $MachinePrecision] * ij + N[(2.0 * ij), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    
    \\
    \mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right) \cdot ij, ij, 2 \cdot ij\right)
    \end{array}
    
    Derivation
    1. Initial program 14.1%

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

      \[\leadsto \color{blue}{ij \cdot \left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right) \cdot ij} \]
      2. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right) \cdot ij} \]
      3. +-commutativeN/A

        \[\leadsto \color{blue}{\left(ij \cdot \left(16 \cdot ij - 4\right) + 2\right)} \cdot ij \]
      4. *-commutativeN/A

        \[\leadsto \left(\color{blue}{\left(16 \cdot ij - 4\right) \cdot ij} + 2\right) \cdot ij \]
      5. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(16 \cdot ij - 4, ij, 2\right)} \cdot ij \]
      6. sub-negN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{16 \cdot ij + \left(\mathsf{neg}\left(4\right)\right)}, ij, 2\right) \cdot ij \]
      7. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left(16 \cdot ij + \color{blue}{-4}, ij, 2\right) \cdot ij \]
      8. lower-fma.f6494.6

        \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(16, ij, -4\right)}, ij, 2\right) \cdot ij \]
    5. Applied rewrites94.6%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right), ij, 2\right) \cdot ij} \]
    6. Step-by-step derivation
      1. Applied rewrites94.6%

        \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right) \cdot ij, \color{blue}{ij}, 2 \cdot ij\right) \]
      2. Add Preprocessing

      Alternative 5: 95.0% accurate, 1.8× speedup?

      \[\begin{array}{l} \\ \mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right), ij, 2\right) \cdot ij \end{array} \]
      (FPCore (ij) :precision binary64 (* (fma (fma 16.0 ij -4.0) ij 2.0) ij))
      double code(double ij) {
      	return fma(fma(16.0, ij, -4.0), ij, 2.0) * ij;
      }
      
      function code(ij)
      	return Float64(fma(fma(16.0, ij, -4.0), ij, 2.0) * ij)
      end
      
      code[ij_] := N[(N[(N[(16.0 * ij + -4.0), $MachinePrecision] * ij + 2.0), $MachinePrecision] * ij), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right), ij, 2\right) \cdot ij
      \end{array}
      
      Derivation
      1. Initial program 14.1%

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

        \[\leadsto \color{blue}{ij \cdot \left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right)} \]
      4. Step-by-step derivation
        1. *-commutativeN/A

          \[\leadsto \color{blue}{\left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right) \cdot ij} \]
        2. lower-*.f64N/A

          \[\leadsto \color{blue}{\left(2 + ij \cdot \left(16 \cdot ij - 4\right)\right) \cdot ij} \]
        3. +-commutativeN/A

          \[\leadsto \color{blue}{\left(ij \cdot \left(16 \cdot ij - 4\right) + 2\right)} \cdot ij \]
        4. *-commutativeN/A

          \[\leadsto \left(\color{blue}{\left(16 \cdot ij - 4\right) \cdot ij} + 2\right) \cdot ij \]
        5. lower-fma.f64N/A

          \[\leadsto \color{blue}{\mathsf{fma}\left(16 \cdot ij - 4, ij, 2\right)} \cdot ij \]
        6. sub-negN/A

          \[\leadsto \mathsf{fma}\left(\color{blue}{16 \cdot ij + \left(\mathsf{neg}\left(4\right)\right)}, ij, 2\right) \cdot ij \]
        7. metadata-evalN/A

          \[\leadsto \mathsf{fma}\left(16 \cdot ij + \color{blue}{-4}, ij, 2\right) \cdot ij \]
        8. lower-fma.f6494.6

          \[\leadsto \mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(16, ij, -4\right)}, ij, 2\right) \cdot ij \]
      5. Applied rewrites94.6%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left(16, ij, -4\right), ij, 2\right) \cdot ij} \]
      6. Add Preprocessing

      Alternative 6: 94.3% accurate, 1.9× speedup?

      \[\begin{array}{l} \\ \mathsf{fma}\left(-4 \cdot ij, ij, 2 \cdot ij\right) \end{array} \]
      (FPCore (ij) :precision binary64 (fma (* -4.0 ij) ij (* 2.0 ij)))
      double code(double ij) {
      	return fma((-4.0 * ij), ij, (2.0 * ij));
      }
      
      function code(ij)
      	return fma(Float64(-4.0 * ij), ij, Float64(2.0 * ij))
      end
      
      code[ij_] := N[(N[(-4.0 * ij), $MachinePrecision] * ij + N[(2.0 * ij), $MachinePrecision]), $MachinePrecision]
      
      \begin{array}{l}
      
      \\
      \mathsf{fma}\left(-4 \cdot ij, ij, 2 \cdot ij\right)
      \end{array}
      
      Derivation
      1. Initial program 14.1%

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

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

          \[\leadsto \color{blue}{\left(2 + -4 \cdot ij\right) \cdot ij} \]
        2. lower-*.f64N/A

          \[\leadsto \color{blue}{\left(2 + -4 \cdot ij\right) \cdot ij} \]
        3. +-commutativeN/A

          \[\leadsto \color{blue}{\left(-4 \cdot ij + 2\right)} \cdot ij \]
        4. lower-fma.f6493.7

          \[\leadsto \color{blue}{\mathsf{fma}\left(-4, ij, 2\right)} \cdot ij \]
      5. Applied rewrites93.7%

        \[\leadsto \color{blue}{\mathsf{fma}\left(-4, ij, 2\right) \cdot ij} \]
      6. Step-by-step derivation
        1. Applied rewrites93.7%

          \[\leadsto \mathsf{fma}\left(-4 \cdot ij, \color{blue}{ij}, 2 \cdot ij\right) \]
        2. Add Preprocessing

        Alternative 7: 94.3% accurate, 2.8× speedup?

        \[\begin{array}{l} \\ \mathsf{fma}\left(-4, ij, 2\right) \cdot ij \end{array} \]
        (FPCore (ij) :precision binary64 (* (fma -4.0 ij 2.0) ij))
        double code(double ij) {
        	return fma(-4.0, ij, 2.0) * ij;
        }
        
        function code(ij)
        	return Float64(fma(-4.0, ij, 2.0) * ij)
        end
        
        code[ij_] := N[(N[(-4.0 * ij + 2.0), $MachinePrecision] * ij), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        \mathsf{fma}\left(-4, ij, 2\right) \cdot ij
        \end{array}
        
        Derivation
        1. Initial program 14.1%

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

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

            \[\leadsto \color{blue}{\left(2 + -4 \cdot ij\right) \cdot ij} \]
          2. lower-*.f64N/A

            \[\leadsto \color{blue}{\left(2 + -4 \cdot ij\right) \cdot ij} \]
          3. +-commutativeN/A

            \[\leadsto \color{blue}{\left(-4 \cdot ij + 2\right)} \cdot ij \]
          4. lower-fma.f6493.7

            \[\leadsto \color{blue}{\mathsf{fma}\left(-4, ij, 2\right)} \cdot ij \]
        5. Applied rewrites93.7%

          \[\leadsto \color{blue}{\mathsf{fma}\left(-4, ij, 2\right) \cdot ij} \]
        6. Add Preprocessing

        Alternative 8: 93.8% accurate, 5.5× speedup?

        \[\begin{array}{l} \\ 2 \cdot ij \end{array} \]
        (FPCore (ij) :precision binary64 (* 2.0 ij))
        double code(double ij) {
        	return 2.0 * ij;
        }
        
        real(8) function code(ij)
            real(8), intent (in) :: ij
            code = 2.0d0 * ij
        end function
        
        public static double code(double ij) {
        	return 2.0 * ij;
        }
        
        def code(ij):
        	return 2.0 * ij
        
        function code(ij)
        	return Float64(2.0 * ij)
        end
        
        function tmp = code(ij)
        	tmp = 2.0 * ij;
        end
        
        code[ij_] := N[(2.0 * ij), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        2 \cdot ij
        \end{array}
        
        Derivation
        1. Initial program 14.1%

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

          \[\leadsto \color{blue}{2 \cdot ij} \]
        4. Step-by-step derivation
          1. lower-*.f6492.8

            \[\leadsto \color{blue}{2 \cdot ij} \]
        5. Applied rewrites92.8%

          \[\leadsto \color{blue}{2 \cdot ij} \]
        6. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 1 
        (FPCore (ij)
          :name "(sqrt(1 + 8 * ij) - 1) / 2"
          :precision binary64
          :pre (and (<= 0.0 ij) (<= ij 1e+15))
          (/ (- (sqrt (+ 1.0 (* 8.0 ij))) 1.0) 2.0))