2cbrt (problem 3.3.4)

Percentage Accurate: 6.9% → 98.0%
Time: 10.3s
Alternatives: 13
Speedup: 1.9×

Specification

?
\[x > 1 \land x < 10^{+308}\]
\[\begin{array}{l} \\ \sqrt[3]{x + 1} - \sqrt[3]{x} \end{array} \]
(FPCore (x) :precision binary64 (- (cbrt (+ x 1.0)) (cbrt x)))
double code(double x) {
	return cbrt((x + 1.0)) - cbrt(x);
}
public static double code(double x) {
	return Math.cbrt((x + 1.0)) - Math.cbrt(x);
}
function code(x)
	return Float64(cbrt(Float64(x + 1.0)) - cbrt(x))
end
code[x_] := N[(N[Power[N[(x + 1.0), $MachinePrecision], 1/3], $MachinePrecision] - N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\sqrt[3]{x + 1} - \sqrt[3]{x}
\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 13 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: 6.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \sqrt[3]{x + 1} - \sqrt[3]{x} \end{array} \]
(FPCore (x) :precision binary64 (- (cbrt (+ x 1.0)) (cbrt x)))
double code(double x) {
	return cbrt((x + 1.0)) - cbrt(x);
}
public static double code(double x) {
	return Math.cbrt((x + 1.0)) - Math.cbrt(x);
}
function code(x)
	return Float64(cbrt(Float64(x + 1.0)) - cbrt(x))
end
code[x_] := N[(N[Power[N[(x + 1.0), $MachinePrecision], 1/3], $MachinePrecision] - N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\sqrt[3]{x + 1} - \sqrt[3]{x}
\end{array}

Alternative 1: 98.0% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \frac{2}{x \cdot x}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{x \cdot x}} + \sqrt[3]{\frac{1}{x}}\right)\right)} \end{array} \]
(FPCore (x)
 :precision binary64
 (/
  1.0
  (*
   x
   (+
    (cbrt (+ (/ 1.0 x) (/ 2.0 (* x x))))
    (+ (cbrt (+ (/ 1.0 x) (/ 1.0 (* x x)))) (cbrt (/ 1.0 x)))))))
double code(double x) {
	return 1.0 / (x * (cbrt(((1.0 / x) + (2.0 / (x * x)))) + (cbrt(((1.0 / x) + (1.0 / (x * x)))) + cbrt((1.0 / x)))));
}
public static double code(double x) {
	return 1.0 / (x * (Math.cbrt(((1.0 / x) + (2.0 / (x * x)))) + (Math.cbrt(((1.0 / x) + (1.0 / (x * x)))) + Math.cbrt((1.0 / x)))));
}
function code(x)
	return Float64(1.0 / Float64(x * Float64(cbrt(Float64(Float64(1.0 / x) + Float64(2.0 / Float64(x * x)))) + Float64(cbrt(Float64(Float64(1.0 / x) + Float64(1.0 / Float64(x * x)))) + cbrt(Float64(1.0 / x))))))
end
code[x_] := N[(1.0 / N[(x * N[(N[Power[N[(N[(1.0 / x), $MachinePrecision] + N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[(N[Power[N[(N[(1.0 / x), $MachinePrecision] + N[(1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[N[(1.0 / x), $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \frac{2}{x \cdot x}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{x \cdot x}} + \sqrt[3]{\frac{1}{x}}\right)\right)}
\end{array}
Derivation
  1. Initial program 5.9%

    \[\sqrt[3]{x + 1} - \sqrt[3]{x} \]
  2. Add Preprocessing
  3. Applied rewrites7.5%

    \[\leadsto \color{blue}{\frac{\left(-\left(x + 1\right)\right) + x}{-\left({\left(x + 1\right)}^{0.6666666666666666} + \left({x}^{0.6666666666666666} + \sqrt[3]{\mathsf{fma}\left(x, x, x\right)}\right)\right)}} \]
  4. Taylor expanded in x around inf

    \[\leadsto \color{blue}{\frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + 2 \cdot \frac{1}{{x}^{2}}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)\right)}} \]
  5. Step-by-step derivation
    1. lower-/.f64N/A

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

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

      \[\leadsto \frac{1}{x \cdot \color{blue}{\left(\sqrt[3]{\frac{1}{x} + 2 \cdot \frac{1}{{x}^{2}}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)\right)}} \]
    4. lower-cbrt.f64N/A

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

      \[\leadsto \frac{1}{x \cdot \left(\sqrt[3]{\color{blue}{\frac{1}{x} + 2 \cdot \frac{1}{{x}^{2}}}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)\right)} \]
    6. lower-/.f64N/A

      \[\leadsto \frac{1}{x \cdot \left(\sqrt[3]{\color{blue}{\frac{1}{x}} + 2 \cdot \frac{1}{{x}^{2}}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)\right)} \]
    7. associate-*r/N/A

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

      \[\leadsto \frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \frac{\color{blue}{2}}{{x}^{2}}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)\right)} \]
    9. lower-/.f64N/A

      \[\leadsto \frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \color{blue}{\frac{2}{{x}^{2}}}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)\right)} \]
    10. unpow2N/A

      \[\leadsto \frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \frac{2}{\color{blue}{x \cdot x}}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)\right)} \]
    11. lower-*.f64N/A

      \[\leadsto \frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \frac{2}{\color{blue}{x \cdot x}}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)\right)} \]
    12. lower-+.f64N/A

      \[\leadsto \frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \frac{2}{x \cdot x}} + \color{blue}{\left(\sqrt[3]{\frac{1}{x} + \frac{1}{{x}^{2}}} + \sqrt[3]{\frac{1}{x}}\right)}\right)} \]
  6. Applied rewrites98.6%

    \[\leadsto \color{blue}{\frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \frac{2}{x \cdot x}} + \left(\sqrt[3]{\frac{1}{x \cdot x} + \frac{1}{x}} + \sqrt[3]{\frac{1}{x}}\right)\right)}} \]
  7. Final simplification98.6%

    \[\leadsto \frac{1}{x \cdot \left(\sqrt[3]{\frac{1}{x} + \frac{2}{x \cdot x}} + \left(\sqrt[3]{\frac{1}{x} + \frac{1}{x \cdot x}} + \sqrt[3]{\frac{1}{x}}\right)\right)} \]
  8. Add Preprocessing

Alternative 2: 96.6% accurate, 0.9× speedup?

\[\begin{array}{l} \\ 0.3333333333333333 \cdot \frac{\sqrt[3]{\frac{1}{x}}}{\sqrt[3]{x}} \end{array} \]
(FPCore (x)
 :precision binary64
 (* 0.3333333333333333 (/ (cbrt (/ 1.0 x)) (cbrt x))))
double code(double x) {
	return 0.3333333333333333 * (cbrt((1.0 / x)) / cbrt(x));
}
public static double code(double x) {
	return 0.3333333333333333 * (Math.cbrt((1.0 / x)) / Math.cbrt(x));
}
function code(x)
	return Float64(0.3333333333333333 * Float64(cbrt(Float64(1.0 / x)) / cbrt(x)))
end
code[x_] := N[(0.3333333333333333 * N[(N[Power[N[(1.0 / x), $MachinePrecision], 1/3], $MachinePrecision] / N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.3333333333333333 \cdot \frac{\sqrt[3]{\frac{1}{x}}}{\sqrt[3]{x}}
\end{array}
Derivation
  1. Initial program 6.9%

    \[\sqrt[3]{x + 1} - \sqrt[3]{x} \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf

    \[\leadsto \color{blue}{\frac{1}{3} \cdot \sqrt[3]{\frac{1}{{x}^{2}}}} \]
  4. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \color{blue}{\frac{1}{3} \cdot \sqrt[3]{\frac{1}{{x}^{2}}}} \]
    2. metadata-evalN/A

      \[\leadsto \frac{1}{3} \cdot \sqrt[3]{\frac{\color{blue}{-1 \cdot -1}}{{x}^{2}}} \]
    3. associate-*r/N/A

      \[\leadsto \frac{1}{3} \cdot \sqrt[3]{\color{blue}{-1 \cdot \frac{-1}{{x}^{2}}}} \]
    4. lower-cbrt.f64N/A

      \[\leadsto \frac{1}{3} \cdot \color{blue}{\sqrt[3]{-1 \cdot \frac{-1}{{x}^{2}}}} \]
    5. associate-*r/N/A

      \[\leadsto \frac{1}{3} \cdot \sqrt[3]{\color{blue}{\frac{-1 \cdot -1}{{x}^{2}}}} \]
    6. metadata-evalN/A

      \[\leadsto \frac{1}{3} \cdot \sqrt[3]{\frac{\color{blue}{1}}{{x}^{2}}} \]
    7. lower-/.f64N/A

      \[\leadsto \frac{1}{3} \cdot \sqrt[3]{\color{blue}{\frac{1}{{x}^{2}}}} \]
    8. unpow2N/A

      \[\leadsto \frac{1}{3} \cdot \sqrt[3]{\frac{1}{\color{blue}{x \cdot x}}} \]
    9. lower-*.f6449.4

      \[\leadsto 0.3333333333333333 \cdot \sqrt[3]{\frac{1}{\color{blue}{x \cdot x}}} \]
  5. Applied rewrites49.4%

    \[\leadsto \color{blue}{0.3333333333333333 \cdot \sqrt[3]{\frac{1}{x \cdot x}}} \]
  6. Step-by-step derivation
    1. Applied rewrites96.5%

      \[\leadsto 0.3333333333333333 \cdot \frac{\frac{1}{\sqrt[3]{x}}}{\color{blue}{\sqrt[3]{x}}} \]
    2. Step-by-step derivation
      1. Applied rewrites96.6%

        \[\leadsto 0.3333333333333333 \cdot \frac{\sqrt[3]{\frac{1}{x}}}{\sqrt[3]{\color{blue}{x}}} \]
      2. Add Preprocessing

      Developer Target 1: 98.5% accurate, 0.3× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt[3]{x + 1}\\ \frac{1}{\left(t\_0 \cdot t\_0 + \sqrt[3]{x} \cdot t\_0\right) + \sqrt[3]{x} \cdot \sqrt[3]{x}} \end{array} \end{array} \]
      (FPCore (x)
       :precision binary64
       (let* ((t_0 (cbrt (+ x 1.0))))
         (/ 1.0 (+ (+ (* t_0 t_0) (* (cbrt x) t_0)) (* (cbrt x) (cbrt x))))))
      double code(double x) {
      	double t_0 = cbrt((x + 1.0));
      	return 1.0 / (((t_0 * t_0) + (cbrt(x) * t_0)) + (cbrt(x) * cbrt(x)));
      }
      
      public static double code(double x) {
      	double t_0 = Math.cbrt((x + 1.0));
      	return 1.0 / (((t_0 * t_0) + (Math.cbrt(x) * t_0)) + (Math.cbrt(x) * Math.cbrt(x)));
      }
      
      function code(x)
      	t_0 = cbrt(Float64(x + 1.0))
      	return Float64(1.0 / Float64(Float64(Float64(t_0 * t_0) + Float64(cbrt(x) * t_0)) + Float64(cbrt(x) * cbrt(x))))
      end
      
      code[x_] := Block[{t$95$0 = N[Power[N[(x + 1.0), $MachinePrecision], 1/3], $MachinePrecision]}, N[(1.0 / N[(N[(N[(t$95$0 * t$95$0), $MachinePrecision] + N[(N[Power[x, 1/3], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision] + N[(N[Power[x, 1/3], $MachinePrecision] * N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      t_0 := \sqrt[3]{x + 1}\\
      \frac{1}{\left(t\_0 \cdot t\_0 + \sqrt[3]{x} \cdot t\_0\right) + \sqrt[3]{x} \cdot \sqrt[3]{x}}
      \end{array}
      \end{array}
      

      Reproduce

      ?
      herbie shell --seed 2024223 
      (FPCore (x)
        :name "2cbrt (problem 3.3.4)"
        :precision binary64
        :pre (and (> x 1.0) (< x 1e+308))
      
        :alt
        (! :herbie-platform default (/ 1 (+ (* (cbrt (+ x 1)) (cbrt (+ x 1))) (* (cbrt x) (cbrt (+ x 1))) (* (cbrt x) (cbrt x)))))
      
        (- (cbrt (+ x 1.0)) (cbrt x)))