Rust f64::acosh

Percentage Accurate: 51.1% → 99.8%
Time: 5.2s
Alternatives: 4
Speedup: 2.0×

Specification

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

\\
\cosh^{-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 4 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: 51.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(x + \sqrt{x \cdot x - 1}\right) \end{array} \]
(FPCore (x) :precision binary64 (log (+ x (sqrt (- (* x x) 1.0)))))
double code(double x) {
	return log((x + sqrt(((x * x) - 1.0))));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = log((x + sqrt(((x * x) - 1.0d0))))
end function
public static double code(double x) {
	return Math.log((x + Math.sqrt(((x * x) - 1.0))));
}
def code(x):
	return math.log((x + math.sqrt(((x * x) - 1.0))))
function code(x)
	return log(Float64(x + sqrt(Float64(Float64(x * x) - 1.0))))
end
function tmp = code(x)
	tmp = log((x + sqrt(((x * x) - 1.0))));
end
code[x_] := N[Log[N[(x + N[Sqrt[N[(N[(x * x), $MachinePrecision] - 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(x + \sqrt{x \cdot x - 1}\right)
\end{array}

Alternative 1: 99.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x + \sqrt{x \cdot x + -1}\\ \mathbf{if}\;t\_0 \leq 10^{+92}:\\ \;\;\;\;\log t\_0\\ \mathbf{else}:\\ \;\;\;\;\log 2 + \log x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (let* ((t_0 (+ x (sqrt (+ (* x x) -1.0)))))
   (if (<= t_0 1e+92) (log t_0) (+ (log 2.0) (log x)))))
double code(double x) {
	double t_0 = x + sqrt(((x * x) + -1.0));
	double tmp;
	if (t_0 <= 1e+92) {
		tmp = log(t_0);
	} else {
		tmp = log(2.0) + log(x);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = x + sqrt(((x * x) + (-1.0d0)))
    if (t_0 <= 1d+92) then
        tmp = log(t_0)
    else
        tmp = log(2.0d0) + log(x)
    end if
    code = tmp
end function
public static double code(double x) {
	double t_0 = x + Math.sqrt(((x * x) + -1.0));
	double tmp;
	if (t_0 <= 1e+92) {
		tmp = Math.log(t_0);
	} else {
		tmp = Math.log(2.0) + Math.log(x);
	}
	return tmp;
}
def code(x):
	t_0 = x + math.sqrt(((x * x) + -1.0))
	tmp = 0
	if t_0 <= 1e+92:
		tmp = math.log(t_0)
	else:
		tmp = math.log(2.0) + math.log(x)
	return tmp
function code(x)
	t_0 = Float64(x + sqrt(Float64(Float64(x * x) + -1.0)))
	tmp = 0.0
	if (t_0 <= 1e+92)
		tmp = log(t_0);
	else
		tmp = Float64(log(2.0) + log(x));
	end
	return tmp
end
function tmp_2 = code(x)
	t_0 = x + sqrt(((x * x) + -1.0));
	tmp = 0.0;
	if (t_0 <= 1e+92)
		tmp = log(t_0);
	else
		tmp = log(2.0) + log(x);
	end
	tmp_2 = tmp;
end
code[x_] := Block[{t$95$0 = N[(x + N[Sqrt[N[(N[(x * x), $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 1e+92], N[Log[t$95$0], $MachinePrecision], N[(N[Log[2.0], $MachinePrecision] + N[Log[x], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := x + \sqrt{x \cdot x + -1}\\
\mathbf{if}\;t\_0 \leq 10^{+92}:\\
\;\;\;\;\log t\_0\\

\mathbf{else}:\\
\;\;\;\;\log 2 + \log x\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f64 x (sqrt.f64 (-.f64 (*.f64 x x) 1))) < 1e92

    1. Initial program 100.0%

      \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
    2. Add Preprocessing

    if 1e92 < (+.f64 x (sqrt.f64 (-.f64 (*.f64 x x) 1)))

    1. Initial program 30.9%

      \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 99.7%

      \[\leadsto \color{blue}{\log 2 + -1 \cdot \log \left(\frac{1}{x}\right)} \]
    4. Step-by-step derivation
      1. mul-1-neg99.7%

        \[\leadsto \log 2 + \color{blue}{\left(-\log \left(\frac{1}{x}\right)\right)} \]
      2. log-rec99.7%

        \[\leadsto \log 2 + \left(-\color{blue}{\left(-\log x\right)}\right) \]
      3. remove-double-neg99.7%

        \[\leadsto \log 2 + \color{blue}{\log x} \]
    5. Simplified99.7%

      \[\leadsto \color{blue}{\log 2 + \log x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x + \sqrt{x \cdot x + -1} \leq 10^{+92}:\\ \;\;\;\;\log \left(x + \sqrt{x \cdot x + -1}\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 + \log x\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log 2 + \log x \end{array} \]
(FPCore (x) :precision binary64 (+ (log 2.0) (log x)))
double code(double x) {
	return log(2.0) + log(x);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = log(2.0d0) + log(x)
end function
public static double code(double x) {
	return Math.log(2.0) + Math.log(x);
}
def code(x):
	return math.log(2.0) + math.log(x)
function code(x)
	return Float64(log(2.0) + log(x))
end
function tmp = code(x)
	tmp = log(2.0) + log(x);
end
code[x_] := N[(N[Log[2.0], $MachinePrecision] + N[Log[x], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\log 2 + \log x
\end{array}
Derivation
  1. Initial program 51.9%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf 98.1%

    \[\leadsto \color{blue}{\log 2 + -1 \cdot \log \left(\frac{1}{x}\right)} \]
  4. Step-by-step derivation
    1. mul-1-neg98.1%

      \[\leadsto \log 2 + \color{blue}{\left(-\log \left(\frac{1}{x}\right)\right)} \]
    2. log-rec98.1%

      \[\leadsto \log 2 + \left(-\color{blue}{\left(-\log x\right)}\right) \]
    3. remove-double-neg98.1%

      \[\leadsto \log 2 + \color{blue}{\log x} \]
  5. Simplified98.1%

    \[\leadsto \color{blue}{\log 2 + \log x} \]
  6. Final simplification98.1%

    \[\leadsto \log 2 + \log x \]
  7. Add Preprocessing

Alternative 3: 99.2% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \log \left(x + x\right) \end{array} \]
(FPCore (x) :precision binary64 (log (+ x x)))
double code(double x) {
	return log((x + x));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = log((x + x))
end function
public static double code(double x) {
	return Math.log((x + x));
}
def code(x):
	return math.log((x + x))
function code(x)
	return log(Float64(x + x))
end
function tmp = code(x)
	tmp = log((x + x));
end
code[x_] := N[Log[N[(x + x), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(x + x\right)
\end{array}
Derivation
  1. Initial program 51.9%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf 98.1%

    \[\leadsto \log \left(x + \color{blue}{x}\right) \]
  4. Final simplification98.1%

    \[\leadsto \log \left(x + x\right) \]
  5. Add Preprocessing

Alternative 4: 3.1% accurate, 207.0× speedup?

\[\begin{array}{l} \\ 0 \end{array} \]
(FPCore (x) :precision binary64 0.0)
double code(double x) {
	return 0.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 0.0d0
end function
public static double code(double x) {
	return 0.0;
}
def code(x):
	return 0.0
function code(x)
	return 0.0
end
function tmp = code(x)
	tmp = 0.0;
end
code[x_] := 0.0
\begin{array}{l}

\\
0
\end{array}
Derivation
  1. Initial program 51.9%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf 98.1%

    \[\leadsto \log \left(x + \color{blue}{x}\right) \]
  4. Step-by-step derivation
    1. add-sqr-sqrt98.0%

      \[\leadsto \log \color{blue}{\left(\sqrt{x + x} \cdot \sqrt{x + x}\right)} \]
    2. log-prod98.0%

      \[\leadsto \color{blue}{\log \left(\sqrt{x + x}\right) + \log \left(\sqrt{x + x}\right)} \]
  5. Applied egg-rr0.0%

    \[\leadsto \color{blue}{\log \left(\frac{0}{0}\right) + \log \left(\frac{0}{0}\right)} \]
  6. Simplified3.1%

    \[\leadsto \color{blue}{0} \]
  7. Final simplification3.1%

    \[\leadsto 0 \]
  8. Add Preprocessing

Developer target: 99.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \log \left(x + \sqrt{x - 1} \cdot \sqrt{x + 1}\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (log (+ x (* (sqrt (- x 1.0)) (sqrt (+ x 1.0))))))
double code(double x) {
	return log((x + (sqrt((x - 1.0)) * sqrt((x + 1.0)))));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = log((x + (sqrt((x - 1.0d0)) * sqrt((x + 1.0d0)))))
end function
public static double code(double x) {
	return Math.log((x + (Math.sqrt((x - 1.0)) * Math.sqrt((x + 1.0)))));
}
def code(x):
	return math.log((x + (math.sqrt((x - 1.0)) * math.sqrt((x + 1.0)))))
function code(x)
	return log(Float64(x + Float64(sqrt(Float64(x - 1.0)) * sqrt(Float64(x + 1.0)))))
end
function tmp = code(x)
	tmp = log((x + (sqrt((x - 1.0)) * sqrt((x + 1.0)))));
end
code[x_] := N[Log[N[(x + N[(N[Sqrt[N[(x - 1.0), $MachinePrecision]], $MachinePrecision] * N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(x + \sqrt{x - 1} \cdot \sqrt{x + 1}\right)
\end{array}

Reproduce

?
herbie shell --seed 2024076 
(FPCore (x)
  :name "Rust f64::acosh"
  :precision binary64
  :pre (>= x 1.0)

  :alt
  (log (+ x (* (sqrt (- x 1.0)) (sqrt (+ x 1.0)))))

  (log (+ x (sqrt (- (* x x) 1.0)))))