2cbrt (problem 3.3.4)

Percentage Accurate: 53.4% → 99.2%
Time: 8.3s
Alternatives: 7
Speedup: 1.0×

Specification

?
\[\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 7 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: 53.4% 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: 99.2% accurate, 0.4× speedup?

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

\\
\begin{array}{l}
t_0 := \sqrt[3]{1 + x}\\
\frac{1}{{t_0}^{2} + \sqrt[3]{x} \cdot \left(t_0 + \sqrt[3]{x}\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 51.2%

    \[\sqrt[3]{x + 1} - \sqrt[3]{x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. expm1-log1p-u51.2%

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt[3]{x + 1} - \sqrt[3]{x}\right)\right)} \]
  4. Applied egg-rr51.2%

    \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sqrt[3]{x + 1} - \sqrt[3]{x}\right)\right)} \]
  5. Applied egg-rr51.6%

    \[\leadsto \color{blue}{\frac{\left(x + 1\right) - x}{{\left(\sqrt[3]{x + 1}\right)}^{2} + \sqrt[3]{x} \cdot \left(\sqrt[3]{x + 1} + \sqrt[3]{x}\right)}} \]
  6. Taylor expanded in x around 0 99.1%

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

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

Alternative 2: 53.3% accurate, 1.0× speedup?

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

\\
\frac{1}{\frac{1}{\sqrt[3]{1 + x}}} - \sqrt[3]{x}
\end{array}
Derivation
  1. Initial program 51.2%

    \[\sqrt[3]{x + 1} - \sqrt[3]{x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. pow1/349.0%

      \[\leadsto \color{blue}{{\left(x + 1\right)}^{0.3333333333333333}} - \sqrt[3]{x} \]
  4. Applied egg-rr49.0%

    \[\leadsto \color{blue}{{\left(x + 1\right)}^{0.3333333333333333}} - \sqrt[3]{x} \]
  5. Step-by-step derivation
    1. pow1/351.2%

      \[\leadsto \color{blue}{\sqrt[3]{x + 1}} - \sqrt[3]{x} \]
    2. metadata-eval51.2%

      \[\leadsto \sqrt[3]{x + \color{blue}{\left(--1\right)}} - \sqrt[3]{x} \]
    3. sub-neg51.2%

      \[\leadsto \sqrt[3]{\color{blue}{x - -1}} - \sqrt[3]{x} \]
    4. flip--50.3%

      \[\leadsto \sqrt[3]{\color{blue}{\frac{x \cdot x - -1 \cdot -1}{x + -1}}} - \sqrt[3]{x} \]
    5. metadata-eval50.3%

      \[\leadsto \sqrt[3]{\frac{x \cdot x - \color{blue}{1}}{x + -1}} - \sqrt[3]{x} \]
    6. fma-neg50.3%

      \[\leadsto \sqrt[3]{\frac{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}{x + -1}} - \sqrt[3]{x} \]
    7. metadata-eval50.3%

      \[\leadsto \sqrt[3]{\frac{\mathsf{fma}\left(x, x, \color{blue}{-1}\right)}{x + -1}} - \sqrt[3]{x} \]
    8. cbrt-undiv50.5%

      \[\leadsto \color{blue}{\frac{\sqrt[3]{\mathsf{fma}\left(x, x, -1\right)}}{\sqrt[3]{x + -1}}} - \sqrt[3]{x} \]
    9. clear-num50.4%

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt[3]{x + -1}}{\sqrt[3]{\mathsf{fma}\left(x, x, -1\right)}}}} - \sqrt[3]{x} \]
    10. clear-num50.5%

      \[\leadsto \frac{1}{\color{blue}{\frac{1}{\frac{\sqrt[3]{\mathsf{fma}\left(x, x, -1\right)}}{\sqrt[3]{x + -1}}}}} - \sqrt[3]{x} \]
    11. cbrt-undiv50.4%

      \[\leadsto \frac{1}{\frac{1}{\color{blue}{\sqrt[3]{\frac{\mathsf{fma}\left(x, x, -1\right)}{x + -1}}}}} - \sqrt[3]{x} \]
    12. metadata-eval50.4%

      \[\leadsto \frac{1}{\frac{1}{\sqrt[3]{\frac{\mathsf{fma}\left(x, x, \color{blue}{-1}\right)}{x + -1}}}} - \sqrt[3]{x} \]
    13. fma-neg50.4%

      \[\leadsto \frac{1}{\frac{1}{\sqrt[3]{\frac{\color{blue}{x \cdot x - 1}}{x + -1}}}} - \sqrt[3]{x} \]
    14. metadata-eval50.4%

      \[\leadsto \frac{1}{\frac{1}{\sqrt[3]{\frac{x \cdot x - \color{blue}{-1 \cdot -1}}{x + -1}}}} - \sqrt[3]{x} \]
    15. flip--51.2%

      \[\leadsto \frac{1}{\frac{1}{\sqrt[3]{\color{blue}{x - -1}}}} - \sqrt[3]{x} \]
    16. sub-neg51.2%

      \[\leadsto \frac{1}{\frac{1}{\sqrt[3]{\color{blue}{x + \left(--1\right)}}}} - \sqrt[3]{x} \]
    17. metadata-eval51.2%

      \[\leadsto \frac{1}{\frac{1}{\sqrt[3]{x + \color{blue}{1}}}} - \sqrt[3]{x} \]
  6. Applied egg-rr51.2%

    \[\leadsto \color{blue}{\frac{1}{\frac{1}{\sqrt[3]{x + 1}}}} - \sqrt[3]{x} \]
  7. Final simplification51.2%

    \[\leadsto \frac{1}{\frac{1}{\sqrt[3]{1 + x}}} - \sqrt[3]{x} \]
  8. Add Preprocessing

Alternative 3: 53.4% accurate, 1.0× speedup?

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

\\
\sqrt[3]{1 + x} - \sqrt[3]{x}
\end{array}
Derivation
  1. Initial program 51.2%

    \[\sqrt[3]{x + 1} - \sqrt[3]{x} \]
  2. Add Preprocessing
  3. Final simplification51.2%

    \[\leadsto \sqrt[3]{1 + x} - \sqrt[3]{x} \]
  4. Add Preprocessing

Alternative 4: 50.8% accurate, 1.9× speedup?

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

\\
1 + \left(x \cdot 0.3333333333333333 - \sqrt[3]{x}\right)
\end{array}
Derivation
  1. Initial program 51.2%

    \[\sqrt[3]{x + 1} - \sqrt[3]{x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-sqr-sqrt23.4%

      \[\leadsto \sqrt[3]{x + 1} - \color{blue}{\sqrt{\sqrt[3]{x}} \cdot \sqrt{\sqrt[3]{x}}} \]
    2. pow223.4%

      \[\leadsto \sqrt[3]{x + 1} - \color{blue}{{\left(\sqrt{\sqrt[3]{x}}\right)}^{2}} \]
    3. pow1/323.8%

      \[\leadsto \sqrt[3]{x + 1} - {\left(\sqrt{\color{blue}{{x}^{0.3333333333333333}}}\right)}^{2} \]
    4. sqrt-pow123.8%

      \[\leadsto \sqrt[3]{x + 1} - {\color{blue}{\left({x}^{\left(\frac{0.3333333333333333}{2}\right)}\right)}}^{2} \]
    5. metadata-eval23.8%

      \[\leadsto \sqrt[3]{x + 1} - {\left({x}^{\color{blue}{0.16666666666666666}}\right)}^{2} \]
  4. Applied egg-rr23.8%

    \[\leadsto \sqrt[3]{x + 1} - \color{blue}{{\left({x}^{0.16666666666666666}\right)}^{2}} \]
  5. Taylor expanded in x around 0 22.6%

    \[\leadsto \color{blue}{\left(1 + 0.3333333333333333 \cdot x\right) - {x}^{0.3333333333333333}} \]
  6. Step-by-step derivation
    1. unpow1/349.0%

      \[\leadsto \left(1 + 0.3333333333333333 \cdot x\right) - \color{blue}{\sqrt[3]{x}} \]
    2. associate--l+49.0%

      \[\leadsto \color{blue}{1 + \left(0.3333333333333333 \cdot x - \sqrt[3]{x}\right)} \]
    3. *-commutative49.0%

      \[\leadsto 1 + \left(\color{blue}{x \cdot 0.3333333333333333} - \sqrt[3]{x}\right) \]
  7. Simplified49.0%

    \[\leadsto \color{blue}{1 + \left(x \cdot 0.3333333333333333 - \sqrt[3]{x}\right)} \]
  8. Final simplification49.0%

    \[\leadsto 1 + \left(x \cdot 0.3333333333333333 - \sqrt[3]{x}\right) \]
  9. Add Preprocessing

Alternative 5: 50.5% accurate, 2.0× speedup?

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

\\
1 - \sqrt[3]{x}
\end{array}
Derivation
  1. Initial program 51.2%

    \[\sqrt[3]{x + 1} - \sqrt[3]{x} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. add-sqr-sqrt23.4%

      \[\leadsto \sqrt[3]{x + 1} - \color{blue}{\sqrt{\sqrt[3]{x}} \cdot \sqrt{\sqrt[3]{x}}} \]
    2. pow223.4%

      \[\leadsto \sqrt[3]{x + 1} - \color{blue}{{\left(\sqrt{\sqrt[3]{x}}\right)}^{2}} \]
    3. pow1/323.8%

      \[\leadsto \sqrt[3]{x + 1} - {\left(\sqrt{\color{blue}{{x}^{0.3333333333333333}}}\right)}^{2} \]
    4. sqrt-pow123.8%

      \[\leadsto \sqrt[3]{x + 1} - {\color{blue}{\left({x}^{\left(\frac{0.3333333333333333}{2}\right)}\right)}}^{2} \]
    5. metadata-eval23.8%

      \[\leadsto \sqrt[3]{x + 1} - {\left({x}^{\color{blue}{0.16666666666666666}}\right)}^{2} \]
  4. Applied egg-rr23.8%

    \[\leadsto \sqrt[3]{x + 1} - \color{blue}{{\left({x}^{0.16666666666666666}\right)}^{2}} \]
  5. Taylor expanded in x around 0 21.5%

    \[\leadsto \color{blue}{1 - {x}^{0.3333333333333333}} \]
  6. Step-by-step derivation
    1. unpow1/348.7%

      \[\leadsto 1 - \color{blue}{\sqrt[3]{x}} \]
  7. Simplified48.7%

    \[\leadsto \color{blue}{1 - \sqrt[3]{x}} \]
  8. Final simplification48.7%

    \[\leadsto 1 - \sqrt[3]{x} \]
  9. Add Preprocessing

Alternative 6: 3.6% accurate, 205.0× speedup?

\[\begin{array}{l} \\ 0 \end{array} \]
(FPCore (x) :precision binary64 0.0)
double code(double x) {
	return 0.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 0.0d0
end function
public static double code(double x) {
	return 0.0;
}
def code(x):
	return 0.0
function code(x)
	return 0.0
end
function tmp = code(x)
	tmp = 0.0;
end
code[x_] := 0.0
\begin{array}{l}

\\
0
\end{array}
Derivation
  1. Initial program 51.2%

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

    \[\leadsto \color{blue}{0} \]
  4. Final simplification3.7%

    \[\leadsto 0 \]
  5. Add Preprocessing

Alternative 7: 49.6% accurate, 205.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (x) :precision binary64 1.0)
double code(double x) {
	return 1.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0
end function
public static double code(double x) {
	return 1.0;
}
def code(x):
	return 1.0
function code(x)
	return 1.0
end
function tmp = code(x)
	tmp = 1.0;
end
code[x_] := 1.0
\begin{array}{l}

\\
1
\end{array}
Derivation
  1. Initial program 51.2%

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

    \[\leadsto \color{blue}{1} \]
  4. Final simplification48.1%

    \[\leadsto 1 \]
  5. Add Preprocessing

Reproduce

?
herbie shell --seed 2024013 
(FPCore (x)
  :name "2cbrt (problem 3.3.4)"
  :precision binary64
  (- (cbrt (+ x 1.0)) (cbrt x)))