
(FPCore (x n) :precision binary64 (- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))
double code(double x, double n) {
return pow((x + 1.0), (1.0 / n)) - pow(x, (1.0 / n));
}
real(8) function code(x, n)
real(8), intent (in) :: x
real(8), intent (in) :: n
code = ((x + 1.0d0) ** (1.0d0 / n)) - (x ** (1.0d0 / n))
end function
public static double code(double x, double n) {
return Math.pow((x + 1.0), (1.0 / n)) - Math.pow(x, (1.0 / n));
}
def code(x, n): return math.pow((x + 1.0), (1.0 / n)) - math.pow(x, (1.0 / n))
function code(x, n) return Float64((Float64(x + 1.0) ^ Float64(1.0 / n)) - (x ^ Float64(1.0 / n))) end
function tmp = code(x, n) tmp = ((x + 1.0) ^ (1.0 / n)) - (x ^ (1.0 / n)); end
code[x_, n_] := N[(N[Power[N[(x + 1.0), $MachinePrecision], N[(1.0 / n), $MachinePrecision]], $MachinePrecision] - N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 17 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x n) :precision binary64 (- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))
double code(double x, double n) {
return pow((x + 1.0), (1.0 / n)) - pow(x, (1.0 / n));
}
real(8) function code(x, n)
real(8), intent (in) :: x
real(8), intent (in) :: n
code = ((x + 1.0d0) ** (1.0d0 / n)) - (x ** (1.0d0 / n))
end function
public static double code(double x, double n) {
return Math.pow((x + 1.0), (1.0 / n)) - Math.pow(x, (1.0 / n));
}
def code(x, n): return math.pow((x + 1.0), (1.0 / n)) - math.pow(x, (1.0 / n))
function code(x, n) return Float64((Float64(x + 1.0) ^ Float64(1.0 / n)) - (x ^ Float64(1.0 / n))) end
function tmp = code(x, n) tmp = ((x + 1.0) ^ (1.0 / n)) - (x ^ (1.0 / n)); end
code[x_, n_] := N[(N[Power[N[(x + 1.0), $MachinePrecision], N[(1.0 / n), $MachinePrecision]], $MachinePrecision] - N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)}
\end{array}
(FPCore (x n)
:precision binary64
(let* ((t_0 (pow x (/ 1.0 n))))
(if (<= (/ 1.0 n) -1e-104)
(/ t_0 (* n x))
(if (<= (/ 1.0 n) 4.0)
(/ (log (/ x (+ x 1.0))) (- n))
(- (exp (/ (log1p x) n)) t_0)))))
double code(double x, double n) {
double t_0 = pow(x, (1.0 / n));
double tmp;
if ((1.0 / n) <= -1e-104) {
tmp = t_0 / (n * x);
} else if ((1.0 / n) <= 4.0) {
tmp = log((x / (x + 1.0))) / -n;
} else {
tmp = exp((log1p(x) / n)) - t_0;
}
return tmp;
}
public static double code(double x, double n) {
double t_0 = Math.pow(x, (1.0 / n));
double tmp;
if ((1.0 / n) <= -1e-104) {
tmp = t_0 / (n * x);
} else if ((1.0 / n) <= 4.0) {
tmp = Math.log((x / (x + 1.0))) / -n;
} else {
tmp = Math.exp((Math.log1p(x) / n)) - t_0;
}
return tmp;
}
def code(x, n): t_0 = math.pow(x, (1.0 / n)) tmp = 0 if (1.0 / n) <= -1e-104: tmp = t_0 / (n * x) elif (1.0 / n) <= 4.0: tmp = math.log((x / (x + 1.0))) / -n else: tmp = math.exp((math.log1p(x) / n)) - t_0 return tmp
function code(x, n) t_0 = x ^ Float64(1.0 / n) tmp = 0.0 if (Float64(1.0 / n) <= -1e-104) tmp = Float64(t_0 / Float64(n * x)); elseif (Float64(1.0 / n) <= 4.0) tmp = Float64(log(Float64(x / Float64(x + 1.0))) / Float64(-n)); else tmp = Float64(exp(Float64(log1p(x) / n)) - t_0); end return tmp end
code[x_, n_] := Block[{t$95$0 = N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(1.0 / n), $MachinePrecision], -1e-104], N[(t$95$0 / N[(n * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[N[(1.0 / n), $MachinePrecision], 4.0], N[(N[Log[N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / (-n)), $MachinePrecision], N[(N[Exp[N[(N[Log[1 + x], $MachinePrecision] / n), $MachinePrecision]], $MachinePrecision] - t$95$0), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {x}^{\left(\frac{1}{n}\right)}\\
\mathbf{if}\;\frac{1}{n} \leq -1 \cdot 10^{-104}:\\
\;\;\;\;\frac{t\_0}{n \cdot x}\\
\mathbf{elif}\;\frac{1}{n} \leq 4:\\
\;\;\;\;\frac{\log \left(\frac{x}{x + 1}\right)}{-n}\\
\mathbf{else}:\\
\;\;\;\;e^{\frac{\mathsf{log1p}\left(x\right)}{n}} - t\_0\\
\end{array}
\end{array}
if (/.f64 #s(literal 1 binary64) n) < -9.99999999999999927e-105Initial program 76.0%
Taylor expanded in x around inf
lower-/.f64N/A
log-recN/A
mul-1-negN/A
associate-*r/N/A
associate-*r*N/A
metadata-evalN/A
*-commutativeN/A
associate-/l*N/A
exp-to-powN/A
lower-pow.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6488.2
Applied rewrites88.2%
if -9.99999999999999927e-105 < (/.f64 #s(literal 1 binary64) n) < 4Initial program 32.5%
Taylor expanded in n around inf
lower-/.f64N/A
lower--.f64N/A
lower-log1p.f64N/A
lower-log.f6486.6
Applied rewrites86.6%
Applied rewrites86.7%
if 4 < (/.f64 #s(literal 1 binary64) n) Initial program 56.2%
lift-pow.f64N/A
pow-to-expN/A
lower-exp.f64N/A
lift-/.f64N/A
un-div-invN/A
lower-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-log1p.f64100.0
Applied rewrites100.0%
Final simplification89.1%
(FPCore (x n)
:precision binary64
(let* ((t_0 (pow x (/ 1.0 n))) (t_1 (- (pow (+ x 1.0) (/ 1.0 n)) t_0)))
(if (<= t_1 -5e-7)
(- 1.0 t_0)
(if (<= t_1 2e-16)
(/ (log (/ x (+ x 1.0))) (- n))
(/
(+
(/ (fma x (* n -0.5) (* n 0.3333333333333333)) (* x (* x (* n n))))
(/ 1.0 n))
x)))))
double code(double x, double n) {
double t_0 = pow(x, (1.0 / n));
double t_1 = pow((x + 1.0), (1.0 / n)) - t_0;
double tmp;
if (t_1 <= -5e-7) {
tmp = 1.0 - t_0;
} else if (t_1 <= 2e-16) {
tmp = log((x / (x + 1.0))) / -n;
} else {
tmp = ((fma(x, (n * -0.5), (n * 0.3333333333333333)) / (x * (x * (n * n)))) + (1.0 / n)) / x;
}
return tmp;
}
function code(x, n) t_0 = x ^ Float64(1.0 / n) t_1 = Float64((Float64(x + 1.0) ^ Float64(1.0 / n)) - t_0) tmp = 0.0 if (t_1 <= -5e-7) tmp = Float64(1.0 - t_0); elseif (t_1 <= 2e-16) tmp = Float64(log(Float64(x / Float64(x + 1.0))) / Float64(-n)); else tmp = Float64(Float64(Float64(fma(x, Float64(n * -0.5), Float64(n * 0.3333333333333333)) / Float64(x * Float64(x * Float64(n * n)))) + Float64(1.0 / n)) / x); end return tmp end
code[x_, n_] := Block[{t$95$0 = N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Power[N[(x + 1.0), $MachinePrecision], N[(1.0 / n), $MachinePrecision]], $MachinePrecision] - t$95$0), $MachinePrecision]}, If[LessEqual[t$95$1, -5e-7], N[(1.0 - t$95$0), $MachinePrecision], If[LessEqual[t$95$1, 2e-16], N[(N[Log[N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / (-n)), $MachinePrecision], N[(N[(N[(N[(x * N[(n * -0.5), $MachinePrecision] + N[(n * 0.3333333333333333), $MachinePrecision]), $MachinePrecision] / N[(x * N[(x * N[(n * n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.0 / n), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := {x}^{\left(\frac{1}{n}\right)}\\
t_1 := {\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - t\_0\\
\mathbf{if}\;t\_1 \leq -5 \cdot 10^{-7}:\\
\;\;\;\;1 - t\_0\\
\mathbf{elif}\;t\_1 \leq 2 \cdot 10^{-16}:\\
\;\;\;\;\frac{\log \left(\frac{x}{x + 1}\right)}{-n}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(x, n \cdot -0.5, n \cdot 0.3333333333333333\right)}{x \cdot \left(x \cdot \left(n \cdot n\right)\right)} + \frac{1}{n}}{x}\\
\end{array}
\end{array}
if (-.f64 (pow.f64 (+.f64 x #s(literal 1 binary64)) (/.f64 #s(literal 1 binary64) n)) (pow.f64 x (/.f64 #s(literal 1 binary64) n))) < -4.99999999999999977e-7Initial program 99.6%
Taylor expanded in x around 0
Applied rewrites99.5%
if -4.99999999999999977e-7 < (-.f64 (pow.f64 (+.f64 x #s(literal 1 binary64)) (/.f64 #s(literal 1 binary64) n)) (pow.f64 x (/.f64 #s(literal 1 binary64) n))) < 2e-16Initial program 42.5%
Taylor expanded in n around inf
lower-/.f64N/A
lower--.f64N/A
lower-log1p.f64N/A
lower-log.f6479.9
Applied rewrites79.9%
Applied rewrites80.1%
if 2e-16 < (-.f64 (pow.f64 (+.f64 x #s(literal 1 binary64)) (/.f64 #s(literal 1 binary64) n)) (pow.f64 x (/.f64 #s(literal 1 binary64) n))) Initial program 54.5%
Taylor expanded in n around inf
lower-/.f64N/A
lower--.f64N/A
lower-log1p.f64N/A
lower-log.f649.7
Applied rewrites9.7%
Taylor expanded in x around -inf
Applied rewrites37.4%
Applied rewrites41.7%
Final simplification77.0%
herbie shell --seed 2024230
(FPCore (x n)
:name "2nthrt (problem 3.4.6)"
:precision binary64
(- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))