Hyperbolic arc-(co)secant

?

Percentage Accurate: 99.9% → 99.9%
Time: 3.5s
Precision: binary64
Cost: 19712

?

\[\log \left(\frac{1}{x} + \frac{\sqrt{1 - x \cdot x}}{x}\right) \]
\[\begin{array}{l} \\ \log \left(\frac{1 + \sqrt{\mathsf{fma}\left(x, -x, 1\right)}}{x}\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (log (+ (/ 1.0 x) (/ (sqrt (- 1.0 (* x x))) x))))
(FPCore (x) :precision binary64 (log (/ (+ 1.0 (sqrt (fma x (- x) 1.0))) x)))
double code(double x) {
	return log(((1.0 / x) + (sqrt((1.0 - (x * x))) / x)));
}
double code(double x) {
	return log(((1.0 + sqrt(fma(x, -x, 1.0))) / x));
}
function code(x)
	return log(Float64(Float64(1.0 / x) + Float64(sqrt(Float64(1.0 - Float64(x * x))) / x)))
end
function code(x)
	return log(Float64(Float64(1.0 + sqrt(fma(x, Float64(-x), 1.0))) / x))
end
code[x_] := N[Log[N[(N[(1.0 / x), $MachinePrecision] + N[(N[Sqrt[N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
code[x_] := N[Log[N[(N[(1.0 + N[Sqrt[N[(x * (-x) + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]], $MachinePrecision]
\log \left(\frac{1}{x} + \frac{\sqrt{1 - x \cdot x}}{x}\right)
\begin{array}{l}

\\
\log \left(\frac{1 + \sqrt{\mathsf{fma}\left(x, -x, 1\right)}}{x}\right)
\end{array}

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.

Herbie found 4 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Derivation?

  1. Initial program 100.0%

    \[\log \left(\frac{1}{x} + \frac{\sqrt{1 - x \cdot x}}{x}\right) \]
  2. Applied egg-rr100.0%

    \[\leadsto \log \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{1}{x} \cdot \left(\sqrt{\mathsf{fma}\left(x, -x, 1\right)} + 1\right)\right)} - 1\right)} \]
    Step-by-step derivation

    [Start]100.0%

    \[ \log \left(\frac{1}{x} + \frac{\sqrt{1 - x \cdot x}}{x}\right) \]

    expm1-log1p-u [=>]100.0%

    \[ \log \color{blue}{\left(\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{x} + \frac{\sqrt{1 - x \cdot x}}{x}\right)\right)\right)} \]

    expm1-udef [=>]100.0%

    \[ \log \color{blue}{\left(e^{\mathsf{log1p}\left(\frac{1}{x} + \frac{\sqrt{1 - x \cdot x}}{x}\right)} - 1\right)} \]

    +-commutative [=>]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{\sqrt{1 - x \cdot x}}{x} + \frac{1}{x}}\right)} - 1\right) \]

    div-inv [=>]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\color{blue}{\sqrt{1 - x \cdot x} \cdot \frac{1}{x}} + \frac{1}{x}\right)} - 1\right) \]

    *-un-lft-identity [=>]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\sqrt{1 - x \cdot x} \cdot \frac{1}{x} + \color{blue}{1 \cdot \frac{1}{x}}\right)} - 1\right) \]

    distribute-rgt-out [=>]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\color{blue}{\frac{1}{x} \cdot \left(\sqrt{1 - x \cdot x} + 1\right)}\right)} - 1\right) \]

    cancel-sign-sub-inv [=>]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\frac{1}{x} \cdot \left(\sqrt{\color{blue}{1 + \left(-x\right) \cdot x}} + 1\right)\right)} - 1\right) \]

    +-commutative [=>]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\frac{1}{x} \cdot \left(\sqrt{\color{blue}{\left(-x\right) \cdot x + 1}} + 1\right)\right)} - 1\right) \]

    *-commutative [=>]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\frac{1}{x} \cdot \left(\sqrt{\color{blue}{x \cdot \left(-x\right)} + 1} + 1\right)\right)} - 1\right) \]

    fma-def [=>]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\frac{1}{x} \cdot \left(\sqrt{\color{blue}{\mathsf{fma}\left(x, -x, 1\right)}} + 1\right)\right)} - 1\right) \]
  3. Simplified100.0%

    \[\leadsto \log \color{blue}{\left(\frac{1 + \sqrt{\mathsf{fma}\left(x, -x, 1\right)}}{x}\right)} \]
    Step-by-step derivation

    [Start]100.0%

    \[ \log \left(e^{\mathsf{log1p}\left(\frac{1}{x} \cdot \left(\sqrt{\mathsf{fma}\left(x, -x, 1\right)} + 1\right)\right)} - 1\right) \]

    expm1-def [=>]100.0%

    \[ \log \color{blue}{\left(\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{1}{x} \cdot \left(\sqrt{\mathsf{fma}\left(x, -x, 1\right)} + 1\right)\right)\right)\right)} \]

    expm1-log1p [=>]100.0%

    \[ \log \color{blue}{\left(\frac{1}{x} \cdot \left(\sqrt{\mathsf{fma}\left(x, -x, 1\right)} + 1\right)\right)} \]

    associate-*l/ [=>]100.0%

    \[ \log \color{blue}{\left(\frac{1 \cdot \left(\sqrt{\mathsf{fma}\left(x, -x, 1\right)} + 1\right)}{x}\right)} \]

    *-lft-identity [=>]100.0%

    \[ \log \left(\frac{\color{blue}{\sqrt{\mathsf{fma}\left(x, -x, 1\right)} + 1}}{x}\right) \]

    +-commutative [<=]100.0%

    \[ \log \left(\frac{\color{blue}{1 + \sqrt{\mathsf{fma}\left(x, -x, 1\right)}}}{x}\right) \]
  4. Final simplification100.0%

    \[\leadsto \log \left(\frac{1 + \sqrt{\mathsf{fma}\left(x, -x, 1\right)}}{x}\right) \]

Alternatives

Alternative 1
Accuracy99.9%
Cost19712
\[\log \left(\frac{1 + \sqrt{\mathsf{fma}\left(x, -x, 1\right)}}{x}\right) \]
Alternative 2
Accuracy99.9%
Cost13504
\[\log \left(\frac{1}{x} + \frac{\sqrt{1 - x \cdot x}}{x}\right) \]
Alternative 3
Accuracy99.5%
Cost6976
\[\log \left(x \cdot -0.5 + \frac{1}{x} \cdot 2\right) \]
Alternative 4
Accuracy99.1%
Cost6592
\[\log \left(\frac{2}{x}\right) \]

Reproduce?

herbie shell --seed 2023167 
(FPCore (x)
  :name "Hyperbolic arc-(co)secant"
  :precision binary64
  (log (+ (/ 1.0 x) (/ (sqrt (- 1.0 (* x x))) x))))