
(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:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (x)
:precision binary64
(if (<= (- (cbrt (+ x 1.0)) (cbrt x)) 0.0)
(/ (* 0.3333333333333333 (cbrt x)) x)
(/
(- (+ 1.0 x) x)
(fma
(cbrt x)
(+ (cbrt (+ 1.0 x)) (cbrt x))
(exp (* (log1p x) 0.6666666666666666))))))
double code(double x) {
double tmp;
if ((cbrt((x + 1.0)) - cbrt(x)) <= 0.0) {
tmp = (0.3333333333333333 * cbrt(x)) / x;
} else {
tmp = ((1.0 + x) - x) / fma(cbrt(x), (cbrt((1.0 + x)) + cbrt(x)), exp((log1p(x) * 0.6666666666666666)));
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(cbrt(Float64(x + 1.0)) - cbrt(x)) <= 0.0) tmp = Float64(Float64(0.3333333333333333 * cbrt(x)) / x); else tmp = Float64(Float64(Float64(1.0 + x) - x) / fma(cbrt(x), Float64(cbrt(Float64(1.0 + x)) + cbrt(x)), exp(Float64(log1p(x) * 0.6666666666666666)))); end return tmp end
code[x_] := If[LessEqual[N[(N[Power[N[(x + 1.0), $MachinePrecision], 1/3], $MachinePrecision] - N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(0.3333333333333333 * N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(N[(1.0 + x), $MachinePrecision] - x), $MachinePrecision] / N[(N[Power[x, 1/3], $MachinePrecision] * N[(N[Power[N[(1.0 + x), $MachinePrecision], 1/3], $MachinePrecision] + N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision] + N[Exp[N[(N[Log[1 + x], $MachinePrecision] * 0.6666666666666666), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\sqrt[3]{x + 1} - \sqrt[3]{x} \leq 0:\\
\;\;\;\;\frac{0.3333333333333333 \cdot \sqrt[3]{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(1 + x\right) - x}{\mathsf{fma}\left(\sqrt[3]{x}, \sqrt[3]{1 + x} + \sqrt[3]{x}, e^{\mathsf{log1p}\left(x\right) \cdot 0.6666666666666666}\right)}\\
\end{array}
\end{array}
if (-.f64 (cbrt.f64 (+.f64 x #s(literal 1 binary64))) (cbrt.f64 x)) < 0.0Initial program 4.2%
Taylor expanded in x around inf
lower-/.f64N/A
Applied rewrites22.5%
Applied rewrites73.1%
Taylor expanded in x around inf
Applied rewrites99.1%
if 0.0 < (-.f64 (cbrt.f64 (+.f64 x #s(literal 1 binary64))) (cbrt.f64 x)) Initial program 55.9%
lift-+.f64N/A
rem-cube-cbrtN/A
lift-cbrt.f64N/A
sqr-powN/A
lower-fma.f64N/A
lift-cbrt.f64N/A
pow1/3N/A
pow-powN/A
metadata-evalN/A
metadata-evalN/A
unpow1/2N/A
lower-sqrt.f64N/A
lift-cbrt.f64N/A
pow1/3N/A
pow-powN/A
metadata-evalN/A
metadata-evalN/A
unpow1/2N/A
lower-sqrt.f6454.6
Applied rewrites54.6%
Applied rewrites98.0%
(FPCore (x)
:precision binary64
(if (<= x 2.5e+17)
(pow
(fma
(cbrt (* x x))
(- 3.0 (/ 0.2222222222222222 (* x x)))
(cbrt (pow x -1.0)))
-1.0)
(/ (* 0.3333333333333333 (cbrt x)) x)))
double code(double x) {
double tmp;
if (x <= 2.5e+17) {
tmp = pow(fma(cbrt((x * x)), (3.0 - (0.2222222222222222 / (x * x))), cbrt(pow(x, -1.0))), -1.0);
} else {
tmp = (0.3333333333333333 * cbrt(x)) / x;
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 2.5e+17) tmp = fma(cbrt(Float64(x * x)), Float64(3.0 - Float64(0.2222222222222222 / Float64(x * x))), cbrt((x ^ -1.0))) ^ -1.0; else tmp = Float64(Float64(0.3333333333333333 * cbrt(x)) / x); end return tmp end
code[x_] := If[LessEqual[x, 2.5e+17], N[Power[N[(N[Power[N[(x * x), $MachinePrecision], 1/3], $MachinePrecision] * N[(3.0 - N[(0.2222222222222222 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[Power[N[Power[x, -1.0], $MachinePrecision], 1/3], $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision], N[(N[(0.3333333333333333 * N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.5 \cdot 10^{+17}:\\
\;\;\;\;{\left(\mathsf{fma}\left(\sqrt[3]{x \cdot x}, 3 - \frac{0.2222222222222222}{x \cdot x}, \sqrt[3]{{x}^{-1}}\right)\right)}^{-1}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.3333333333333333 \cdot \sqrt[3]{x}}{x}\\
\end{array}
\end{array}
if x < 2.5e17Initial program 50.0%
Taylor expanded in x around inf
lower-/.f64N/A
Applied rewrites93.4%
Taylor expanded in x around inf
Applied rewrites68.8%
Applied rewrites68.9%
Taylor expanded in x around inf
Applied rewrites94.1%
if 2.5e17 < x Initial program 4.2%
Taylor expanded in x around inf
lower-/.f64N/A
Applied rewrites21.9%
Applied rewrites72.9%
Taylor expanded in x around inf
Applied rewrites99.1%
Final simplification98.8%
(FPCore (x) :precision binary64 (/ (* 0.3333333333333333 (cbrt x)) x))
double code(double x) {
return (0.3333333333333333 * cbrt(x)) / x;
}
public static double code(double x) {
return (0.3333333333333333 * Math.cbrt(x)) / x;
}
function code(x) return Float64(Float64(0.3333333333333333 * cbrt(x)) / x) end
code[x_] := N[(N[(0.3333333333333333 * N[Power[x, 1/3], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.3333333333333333 \cdot \sqrt[3]{x}}{x}
\end{array}
Initial program 7.4%
Taylor expanded in x around inf
lower-/.f64N/A
Applied rewrites26.9%
Applied rewrites74.4%
Taylor expanded in x around inf
Applied rewrites97.0%
(FPCore (x) :precision binary64 (* (pow x -0.6666666666666666) 0.3333333333333333))
double code(double x) {
return pow(x, -0.6666666666666666) * 0.3333333333333333;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x ** (-0.6666666666666666d0)) * 0.3333333333333333d0
end function
public static double code(double x) {
return Math.pow(x, -0.6666666666666666) * 0.3333333333333333;
}
def code(x): return math.pow(x, -0.6666666666666666) * 0.3333333333333333
function code(x) return Float64((x ^ -0.6666666666666666) * 0.3333333333333333) end
function tmp = code(x) tmp = (x ^ -0.6666666666666666) * 0.3333333333333333; end
code[x_] := N[(N[Power[x, -0.6666666666666666], $MachinePrecision] * 0.3333333333333333), $MachinePrecision]
\begin{array}{l}
\\
{x}^{-0.6666666666666666} \cdot 0.3333333333333333
\end{array}
Initial program 7.4%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
metadata-evalN/A
associate-*r/N/A
lower-cbrt.f64N/A
unpow2N/A
associate-/r*N/A
associate-*r/N/A
lower-/.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6451.9
Applied rewrites51.9%
Applied rewrites88.7%
(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(Float64(-x))) end
code[x_] := N[(1.0 - N[Power[(-x), 1/3], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 - \sqrt[3]{-x}
\end{array}
Initial program 7.4%
Taylor expanded in x around 0
Applied rewrites1.8%
lift-cbrt.f64N/A
pow1/3N/A
lower-pow.f641.8
Applied rewrites1.8%
lift-pow.f64N/A
metadata-evalN/A
pow-powN/A
pow2N/A
sqr-negN/A
lift-neg.f64N/A
lift-neg.f64N/A
unpow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
pow1/3N/A
lift-cbrt.f645.4
Applied rewrites5.4%
(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}
Initial program 7.4%
Taylor expanded in x around 0
Applied rewrites1.8%
(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}
herbie shell --seed 2024342
(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)))