Rust f64::atanh

Percentage Accurate: 100.0% → 100.0%
Time: 1.9s
Alternatives: 5
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \tanh^{-1} x \end{array} \]
(FPCore (x) :precision binary64 (atanh x))
double code(double x) {
	return atanh(x);
}
def code(x):
	return math.atanh(x)
function code(x)
	return atanh(x)
end
function tmp = code(x)
	tmp = atanh(x);
end
code[x_] := N[ArcTanh[x], $MachinePrecision]
\begin{array}{l}

\\
\tanh^{-1} 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 5 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \end{array} \]
(FPCore (x) :precision binary64 (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))
double code(double x) {
	return 0.5 * log1p(((2.0 * x) / (1.0 - x)));
}
public static double code(double x) {
	return 0.5 * Math.log1p(((2.0 * x) / (1.0 - x)));
}
def code(x):
	return 0.5 * math.log1p(((2.0 * x) / (1.0 - x)))
function code(x)
	return Float64(0.5 * log1p(Float64(Float64(2.0 * x) / Float64(1.0 - x))))
end
code[x_] := N[(0.5 * N[Log[1 + N[(N[(2.0 * x), $MachinePrecision] / N[(1.0 - x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right)
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \end{array} \]
(FPCore (x) :precision binary64 (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))
double code(double x) {
	return 0.5 * log1p(((2.0 * x) / (1.0 - x)));
}
public static double code(double x) {
	return 0.5 * Math.log1p(((2.0 * x) / (1.0 - x)));
}
def code(x):
	return 0.5 * math.log1p(((2.0 * x) / (1.0 - x)))
function code(x)
	return Float64(0.5 * log1p(Float64(Float64(2.0 * x) / Float64(1.0 - x))))
end
code[x_] := N[(0.5 * N[Log[1 + N[(N[(2.0 * x), $MachinePrecision] / N[(1.0 - x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Final simplification100.0%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]

Alternative 2: 98.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(2 \cdot \left(x + x \cdot x\right)\right) \end{array} \]
(FPCore (x) :precision binary64 (* 0.5 (log1p (* 2.0 (+ x (* x x))))))
double code(double x) {
	return 0.5 * log1p((2.0 * (x + (x * x))));
}
public static double code(double x) {
	return 0.5 * Math.log1p((2.0 * (x + (x * x))));
}
def code(x):
	return 0.5 * math.log1p((2.0 * (x + (x * x))))
function code(x)
	return Float64(0.5 * log1p(Float64(2.0 * Float64(x + Float64(x * x)))))
end
code[x_] := N[(0.5 * N[Log[1 + N[(2.0 * N[(x + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(2 \cdot \left(x + x \cdot x\right)\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Taylor expanded in x around 0 98.3%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot {x}^{2} + 2 \cdot x}\right) \]
  3. Step-by-step derivation
    1. distribute-lft-out98.3%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot \left({x}^{2} + x\right)}\right) \]
    2. unpow298.3%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \left(\color{blue}{x \cdot x} + x\right)\right) \]
  4. Simplified98.3%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot \left(x \cdot x + x\right)}\right) \]
  5. Final simplification98.3%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \left(x + x \cdot x\right)\right) \]

Alternative 3: 99.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(\frac{2}{\frac{1}{x} + -1}\right) \end{array} \]
(FPCore (x) :precision binary64 (* 0.5 (log1p (/ 2.0 (+ (/ 1.0 x) -1.0)))))
double code(double x) {
	return 0.5 * log1p((2.0 / ((1.0 / x) + -1.0)));
}
public static double code(double x) {
	return 0.5 * Math.log1p((2.0 / ((1.0 / x) + -1.0)));
}
def code(x):
	return 0.5 * math.log1p((2.0 / ((1.0 / x) + -1.0)))
function code(x)
	return Float64(0.5 * log1p(Float64(2.0 / Float64(Float64(1.0 / x) + -1.0))))
end
code[x_] := N[(0.5 * N[Log[1 + N[(2.0 / N[(N[(1.0 / x), $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(\frac{2}{\frac{1}{x} + -1}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Step-by-step derivation
    1. associate-/l*99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{2}{\frac{1 - x}{x}}}\right) \]
    2. div-inv99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot \frac{1}{\frac{1 - x}{x}}}\right) \]
    3. div-sub99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \frac{1}{\color{blue}{\frac{1}{x} - \frac{x}{x}}}\right) \]
    4. pow199.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \frac{1}{\frac{1}{x} - \frac{\color{blue}{{x}^{1}}}{x}}\right) \]
    5. pow199.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \frac{1}{\frac{1}{x} - \frac{{x}^{1}}{\color{blue}{{x}^{1}}}}\right) \]
    6. pow-div99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \frac{1}{\frac{1}{x} - \color{blue}{{x}^{\left(1 - 1\right)}}}\right) \]
    7. metadata-eval99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \frac{1}{\frac{1}{x} - {x}^{\color{blue}{0}}}\right) \]
    8. metadata-eval99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \frac{1}{\frac{1}{x} - \color{blue}{1}}\right) \]
  3. Applied egg-rr99.7%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot \frac{1}{\frac{1}{x} - 1}}\right) \]
  4. Step-by-step derivation
    1. associate-*r/99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{2 \cdot 1}{\frac{1}{x} - 1}}\right) \]
    2. metadata-eval99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{\color{blue}{2}}{\frac{1}{x} - 1}\right) \]
    3. sub-neg99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{2}{\color{blue}{\frac{1}{x} + \left(-1\right)}}\right) \]
    4. metadata-eval99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{2}{\frac{1}{x} + \color{blue}{-1}}\right) \]
  5. Simplified99.7%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{2}{\frac{1}{x} + -1}}\right) \]
  6. Final simplification99.7%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{2}{\frac{1}{x} + -1}\right) \]

Alternative 4: 97.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(x + x\right) \end{array} \]
(FPCore (x) :precision binary64 (* 0.5 (log1p (+ x x))))
double code(double x) {
	return 0.5 * log1p((x + x));
}
public static double code(double x) {
	return 0.5 * Math.log1p((x + x));
}
def code(x):
	return 0.5 * math.log1p((x + x))
function code(x)
	return Float64(0.5 * log1p(Float64(x + x)))
end
code[x_] := N[(0.5 * N[Log[1 + N[(x + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(x + x\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Taylor expanded in x around 0 97.2%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot x}\right) \]
  3. Step-by-step derivation
    1. count-297.2%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{x + x}\right) \]
  4. Simplified97.2%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{x + x}\right) \]
  5. Final simplification97.2%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(x + x\right) \]

Alternative 5: 0.0% accurate, 1.1× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(-2\right) \end{array} \]
(FPCore (x) :precision binary64 (* 0.5 (log1p -2.0)))
double code(double x) {
	return 0.5 * log1p(-2.0);
}
public static double code(double x) {
	return 0.5 * Math.log1p(-2.0);
}
def code(x):
	return 0.5 * math.log1p(-2.0)
function code(x)
	return Float64(0.5 * log1p(-2.0))
end
code[x_] := N[(0.5 * N[Log[1 + -2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(-2\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Taylor expanded in x around inf 0.0%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{-2}\right) \]
  3. Final simplification0.0%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(-2\right) \]

Reproduce

?
herbie shell --seed 2023189 
(FPCore (x)
  :name "Rust f64::atanh"
  :precision binary64
  (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))