2nthrt (problem 3.4.6)

?

Percentage Accurate: 53.4% → 91.3%
Time: 18.9s
Precision: binary64
Cost: 13188

?

\[{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)} \]
\[\begin{array}{l} \mathbf{if}\;x \leq 0.6:\\ \;\;\;\;-\mathsf{expm1}\left(\frac{\log x}{n}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{n} \cdot {x}^{\left(\frac{1}{n}\right)}}{x}\\ \end{array} \]
(FPCore (x n)
 :precision binary64
 (- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))
(FPCore (x n)
 :precision binary64
 (if (<= x 0.6)
   (- (expm1 (/ (log x) n)))
   (/ (* (/ 1.0 n) (pow x (/ 1.0 n))) x)))
double code(double x, double n) {
	return pow((x + 1.0), (1.0 / n)) - pow(x, (1.0 / n));
}
double code(double x, double n) {
	double tmp;
	if (x <= 0.6) {
		tmp = -expm1((log(x) / n));
	} else {
		tmp = ((1.0 / n) * pow(x, (1.0 / n))) / x;
	}
	return tmp;
}
public static double code(double x, double n) {
	return Math.pow((x + 1.0), (1.0 / n)) - Math.pow(x, (1.0 / n));
}
public static double code(double x, double n) {
	double tmp;
	if (x <= 0.6) {
		tmp = -Math.expm1((Math.log(x) / n));
	} else {
		tmp = ((1.0 / n) * Math.pow(x, (1.0 / n))) / x;
	}
	return tmp;
}
def code(x, n):
	return math.pow((x + 1.0), (1.0 / n)) - math.pow(x, (1.0 / n))
def code(x, n):
	tmp = 0
	if x <= 0.6:
		tmp = -math.expm1((math.log(x) / n))
	else:
		tmp = ((1.0 / n) * math.pow(x, (1.0 / n))) / x
	return tmp
function code(x, n)
	return Float64((Float64(x + 1.0) ^ Float64(1.0 / n)) - (x ^ Float64(1.0 / n)))
end
function code(x, n)
	tmp = 0.0
	if (x <= 0.6)
		tmp = Float64(-expm1(Float64(log(x) / n)));
	else
		tmp = Float64(Float64(Float64(1.0 / n) * (x ^ Float64(1.0 / n))) / x);
	end
	return tmp
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]
code[x_, n_] := If[LessEqual[x, 0.6], (-N[(Exp[N[(N[Log[x], $MachinePrecision] / n), $MachinePrecision]] - 1), $MachinePrecision]), N[(N[(N[(1.0 / n), $MachinePrecision] * N[Power[x, N[(1.0 / n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)}
\begin{array}{l}
\mathbf{if}\;x \leq 0.6:\\
\;\;\;\;-\mathsf{expm1}\left(\frac{\log x}{n}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{n} \cdot {x}^{\left(\frac{1}{n}\right)}}{x}\\


\end{array}

Local Percentage Accuracy?

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.

Herbie found 10 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 2 regimes
  2. if x < 0.599999999999999978

    1. Initial program 45.9%

      \[{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)} \]
    2. Taylor expanded in x around 0 45.2%

      \[\leadsto \color{blue}{1 - e^{\frac{\log x}{n}}} \]
    3. Simplified45.2%

      \[\leadsto \color{blue}{1 - {x}^{\left(\frac{1}{n}\right)}} \]
      Step-by-step derivation

      [Start]45.2

      \[ 1 - e^{\frac{\log x}{n}} \]

      *-rgt-identity [<=]45.2

      \[ 1 - e^{\frac{\color{blue}{\log x \cdot 1}}{n}} \]

      associate-*r/ [<=]45.2

      \[ 1 - e^{\color{blue}{\log x \cdot \frac{1}{n}}} \]

      unpow-1 [<=]45.2

      \[ 1 - e^{\log x \cdot \color{blue}{{n}^{-1}}} \]

      exp-to-pow [=>]45.2

      \[ 1 - \color{blue}{{x}^{\left({n}^{-1}\right)}} \]

      unpow-1 [=>]45.2

      \[ 1 - {x}^{\color{blue}{\left(\frac{1}{n}\right)}} \]
    4. Taylor expanded in x around 0 45.2%

      \[\leadsto \color{blue}{1 - e^{\frac{\log x}{n}}} \]
    5. Simplified89.7%

      \[\leadsto \color{blue}{-\mathsf{expm1}\left(\frac{\log x}{n}\right)} \]
      Step-by-step derivation

      [Start]45.2

      \[ 1 - e^{\frac{\log x}{n}} \]

      sub-neg [=>]45.2

      \[ \color{blue}{1 + \left(-e^{\frac{\log x}{n}}\right)} \]

      +-commutative [=>]45.2

      \[ \color{blue}{\left(-e^{\frac{\log x}{n}}\right) + 1} \]

      neg-sub0 [=>]45.2

      \[ \color{blue}{\left(0 - e^{\frac{\log x}{n}}\right)} + 1 \]

      metadata-eval [<=]45.2

      \[ \left(\color{blue}{\log 1} - e^{\frac{\log x}{n}}\right) + 1 \]

      associate-+l- [=>]45.2

      \[ \color{blue}{\log 1 - \left(e^{\frac{\log x}{n}} - 1\right)} \]

      metadata-eval [=>]45.2

      \[ \color{blue}{0} - \left(e^{\frac{\log x}{n}} - 1\right) \]

      sub0-neg [=>]45.2

      \[ \color{blue}{-\left(e^{\frac{\log x}{n}} - 1\right)} \]

      expm1-def [=>]89.7

      \[ -\color{blue}{\mathsf{expm1}\left(\frac{\log x}{n}\right)} \]

    if 0.599999999999999978 < x

    1. Initial program 59.8%

      \[{\left(x + 1\right)}^{\left(\frac{1}{n}\right)} - {x}^{\left(\frac{1}{n}\right)} \]
    2. Taylor expanded in x around inf 97.3%

      \[\leadsto \color{blue}{\frac{e^{-1 \cdot \frac{\log \left(\frac{1}{x}\right)}{n}}}{n \cdot x}} \]
    3. Simplified97.3%

      \[\leadsto \color{blue}{\frac{{x}^{\left(\frac{1}{n}\right)}}{x \cdot n}} \]
      Step-by-step derivation

      [Start]97.3

      \[ \frac{e^{-1 \cdot \frac{\log \left(\frac{1}{x}\right)}{n}}}{n \cdot x} \]

      log-rec [=>]97.3

      \[ \frac{e^{-1 \cdot \frac{\color{blue}{-\log x}}{n}}}{n \cdot x} \]

      mul-1-neg [<=]97.3

      \[ \frac{e^{-1 \cdot \frac{\color{blue}{-1 \cdot \log x}}{n}}}{n \cdot x} \]

      mul-1-neg [=>]97.3

      \[ \frac{e^{-1 \cdot \frac{\color{blue}{-\log x}}{n}}}{n \cdot x} \]

      distribute-frac-neg [=>]97.3

      \[ \frac{e^{-1 \cdot \color{blue}{\left(-\frac{\log x}{n}\right)}}}{n \cdot x} \]

      neg-mul-1 [<=]97.3

      \[ \frac{e^{\color{blue}{-\left(-\frac{\log x}{n}\right)}}}{n \cdot x} \]

      remove-double-neg [=>]97.3

      \[ \frac{e^{\color{blue}{\frac{\log x}{n}}}}{n \cdot x} \]

      *-rgt-identity [<=]97.3

      \[ \frac{e^{\frac{\color{blue}{\log x \cdot 1}}{n}}}{n \cdot x} \]

      associate-*r/ [<=]97.3

      \[ \frac{e^{\color{blue}{\log x \cdot \frac{1}{n}}}}{n \cdot x} \]

      unpow-1 [<=]97.3

      \[ \frac{e^{\log x \cdot \color{blue}{{n}^{-1}}}}{n \cdot x} \]

      exp-to-pow [=>]97.3

      \[ \frac{\color{blue}{{x}^{\left({n}^{-1}\right)}}}{n \cdot x} \]

      unpow-1 [=>]97.3

      \[ \frac{{x}^{\color{blue}{\left(\frac{1}{n}\right)}}}{n \cdot x} \]

      *-commutative [=>]97.3

      \[ \frac{{x}^{\left(\frac{1}{n}\right)}}{\color{blue}{x \cdot n}} \]
    4. Applied egg-rr99.2%

      \[\leadsto \color{blue}{\frac{1}{x} \cdot \frac{{x}^{\left(\frac{1}{n}\right)}}{n}} \]
      Step-by-step derivation

      [Start]97.3

      \[ \frac{{x}^{\left(\frac{1}{n}\right)}}{x \cdot n} \]

      *-un-lft-identity [=>]97.3

      \[ \frac{\color{blue}{1 \cdot {x}^{\left(\frac{1}{n}\right)}}}{x \cdot n} \]

      times-frac [=>]99.2

      \[ \color{blue}{\frac{1}{x} \cdot \frac{{x}^{\left(\frac{1}{n}\right)}}{n}} \]
    5. Applied egg-rr99.3%

      \[\leadsto \color{blue}{\frac{\frac{{x}^{\left(\frac{1}{n}\right)}}{n}}{x}} \]
      Step-by-step derivation

      [Start]99.2

      \[ \frac{1}{x} \cdot \frac{{x}^{\left(\frac{1}{n}\right)}}{n} \]

      associate-*l/ [=>]99.3

      \[ \color{blue}{\frac{1 \cdot \frac{{x}^{\left(\frac{1}{n}\right)}}{n}}{x}} \]

      *-un-lft-identity [<=]99.3

      \[ \frac{\color{blue}{\frac{{x}^{\left(\frac{1}{n}\right)}}{n}}}{x} \]
    6. Applied egg-rr99.3%

      \[\leadsto \frac{\color{blue}{{x}^{\left(\frac{1}{n}\right)} \cdot \frac{1}{n}}}{x} \]
      Step-by-step derivation

      [Start]99.3

      \[ \frac{\frac{{x}^{\left(\frac{1}{n}\right)}}{n}}{x} \]

      div-inv [=>]99.3

      \[ \frac{\color{blue}{{x}^{\left(\frac{1}{n}\right)} \cdot \frac{1}{n}}}{x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification93.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 0.6:\\ \;\;\;\;-\mathsf{expm1}\left(\frac{\log x}{n}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{n} \cdot {x}^{\left(\frac{1}{n}\right)}}{x}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy69.9%
Cost7436
\[\begin{array}{l} t_0 := {x}^{\left(\frac{1}{n}\right)}\\ \mathbf{if}\;x \leq 8.8 \cdot 10^{-281}:\\ \;\;\;\;\frac{-\log x}{n}\\ \mathbf{elif}\;x \leq 1.15 \cdot 10^{-152}:\\ \;\;\;\;\left(1 + \frac{x}{n}\right) - t_0\\ \mathbf{elif}\;x \leq 0.007:\\ \;\;\;\;\frac{x - \log x}{n}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{1}{n} \cdot t_0}{x}\\ \end{array} \]
Alternative 2
Accuracy69.9%
Cost7308
\[\begin{array}{l} t_0 := {x}^{\left(\frac{1}{n}\right)}\\ \mathbf{if}\;x \leq 9 \cdot 10^{-281}:\\ \;\;\;\;\frac{-\log x}{n}\\ \mathbf{elif}\;x \leq 1.25 \cdot 10^{-152}:\\ \;\;\;\;1 - t_0\\ \mathbf{elif}\;x \leq 0.031:\\ \;\;\;\;\frac{x - \log x}{n}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{t_0}{n}}{x}\\ \end{array} \]
Alternative 3
Accuracy69.9%
Cost7308
\[\begin{array}{l} t_0 := {x}^{\left(\frac{1}{n}\right)}\\ \mathbf{if}\;x \leq 2.1 \cdot 10^{-281}:\\ \;\;\;\;\frac{-\log x}{n}\\ \mathbf{elif}\;x \leq 1.4 \cdot 10^{-152}:\\ \;\;\;\;\left(1 + \frac{x}{n}\right) - t_0\\ \mathbf{elif}\;x \leq 0.0048:\\ \;\;\;\;\frac{x - \log x}{n}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{t_0}{n}}{x}\\ \end{array} \]
Alternative 4
Accuracy56.8%
Cost7116
\[\begin{array}{l} \mathbf{if}\;x \leq 3.8 \cdot 10^{-281}:\\ \;\;\;\;\frac{-\log x}{n}\\ \mathbf{elif}\;x \leq 1.35 \cdot 10^{-152}:\\ \;\;\;\;1 - {x}^{\left(\frac{1}{n}\right)}\\ \mathbf{elif}\;x \leq 1:\\ \;\;\;\;\frac{x - \log x}{n}\\ \mathbf{elif}\;x \leq 6.5 \cdot 10^{+29}:\\ \;\;\;\;0\\ \mathbf{elif}\;x \leq 2.2 \cdot 10^{+252}:\\ \;\;\;\;\frac{\frac{1}{n}}{x}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 5
Accuracy58.1%
Cost6852
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;\frac{x - \log x}{n}\\ \mathbf{elif}\;x \leq 1.8 \cdot 10^{+29}:\\ \;\;\;\;0\\ \mathbf{elif}\;x \leq 5.8 \cdot 10^{+252}:\\ \;\;\;\;\frac{\frac{1}{n}}{x}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 6
Accuracy57.9%
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 1:\\ \;\;\;\;\frac{-\log x}{n}\\ \mathbf{elif}\;x \leq 2.35 \cdot 10^{+30}:\\ \;\;\;\;0\\ \mathbf{elif}\;x \leq 2.1 \cdot 10^{+252}:\\ \;\;\;\;\frac{\frac{1}{n}}{x}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 7
Accuracy45.3%
Cost585
\[\begin{array}{l} \mathbf{if}\;n \leq -3.2 \lor \neg \left(n \leq -8.6 \cdot 10^{-194}\right):\\ \;\;\;\;\frac{1}{x \cdot n}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 8
Accuracy45.9%
Cost585
\[\begin{array}{l} \mathbf{if}\;n \leq -2.8 \lor \neg \left(n \leq -1.5 \cdot 10^{-190}\right):\\ \;\;\;\;\frac{\frac{1}{n}}{x}\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]
Alternative 9
Accuracy30.4%
Cost64
\[0 \]

Reproduce?

herbie shell --seed 2023161 
(FPCore (x n)
  :name "2nthrt (problem 3.4.6)"
  :precision binary64
  (- (pow (+ x 1.0) (/ 1.0 n)) (pow x (/ 1.0 n))))