Rust f32::acosh

Percentage Accurate: 52.3% → 99.6%
Time: 7.5s
Alternatives: 5
Speedup: 2.0×

Specification

?
\[x \geq 1\]
\[\begin{array}{l} \\ \cosh^{-1} x \end{array} \]
(FPCore (x) :precision binary32 (acosh x))
float code(float x) {
	return acoshf(x);
}
function code(x)
	return acosh(x)
end
function tmp = code(x)
	tmp = acosh(x);
end
\begin{array}{l}

\\
\cosh^{-1} x
\end{array}

Sampling outcomes in binary32 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: 52.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(x + \sqrt{x \cdot x - 1}\right) \end{array} \]
(FPCore (x) :precision binary32 (log (+ x (sqrt (- (* x x) 1.0)))))
float code(float x) {
	return logf((x + sqrtf(((x * x) - 1.0f))));
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = log((x + sqrt(((x * x) - 1.0e0))))
end function
function code(x)
	return log(Float32(x + sqrt(Float32(Float32(x * x) - Float32(1.0)))))
end
function tmp = code(x)
	tmp = log((x + sqrt(((x * x) - single(1.0)))));
end
\begin{array}{l}

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

Alternative 1: 99.6% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := x + \sqrt{x \cdot x + -1}\\ \mathbf{if}\;t\_0 \leq 5.000000100204387 \cdot 10^{+19}:\\ \;\;\;\;\log t\_0\\ \mathbf{else}:\\ \;\;\;\;\log 2 + \log x\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary32
 (let* ((t_0 (+ x (sqrt (+ (* x x) -1.0)))))
   (if (<= t_0 5.000000100204387e+19) (log t_0) (+ (log 2.0) (log x)))))
float code(float x) {
	float t_0 = x + sqrtf(((x * x) + -1.0f));
	float tmp;
	if (t_0 <= 5.000000100204387e+19f) {
		tmp = logf(t_0);
	} else {
		tmp = logf(2.0f) + logf(x);
	}
	return tmp;
}
real(4) function code(x)
    real(4), intent (in) :: x
    real(4) :: t_0
    real(4) :: tmp
    t_0 = x + sqrt(((x * x) + (-1.0e0)))
    if (t_0 <= 5.000000100204387e+19) then
        tmp = log(t_0)
    else
        tmp = log(2.0e0) + log(x)
    end if
    code = tmp
end function
function code(x)
	t_0 = Float32(x + sqrt(Float32(Float32(x * x) + Float32(-1.0))))
	tmp = Float32(0.0)
	if (t_0 <= Float32(5.000000100204387e+19))
		tmp = log(t_0);
	else
		tmp = Float32(log(Float32(2.0)) + log(x));
	end
	return tmp
end
function tmp_2 = code(x)
	t_0 = x + sqrt(((x * x) + single(-1.0)));
	tmp = single(0.0);
	if (t_0 <= single(5.000000100204387e+19))
		tmp = log(t_0);
	else
		tmp = log(single(2.0)) + log(x);
	end
	tmp_2 = tmp;
end
\begin{array}{l}

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (+.f32 x (sqrt.f32 (-.f32 (*.f32 x x) #s(literal 1 binary32)))) < 5.0000001e19

    1. Initial program 99.9%

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

    if 5.0000001e19 < (+.f32 x (sqrt.f32 (-.f32 (*.f32 x x) #s(literal 1 binary32))))

    1. Initial program 6.5%

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

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

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

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

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

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

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

Alternative 2: 98.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(x \cdot \left(2 - \frac{0.5}{{x}^{2}}\right)\right) \end{array} \]
(FPCore (x) :precision binary32 (log (* x (- 2.0 (/ 0.5 (pow x 2.0))))))
float code(float x) {
	return logf((x * (2.0f - (0.5f / powf(x, 2.0f)))));
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = log((x * (2.0e0 - (0.5e0 / (x ** 2.0e0)))))
end function
function code(x)
	return log(Float32(x * Float32(Float32(2.0) - Float32(Float32(0.5) / (x ^ Float32(2.0))))))
end
function tmp = code(x)
	tmp = log((x * (single(2.0) - (single(0.5) / (x ^ single(2.0))))));
end
\begin{array}{l}

\\
\log \left(x \cdot \left(2 - \frac{0.5}{{x}^{2}}\right)\right)
\end{array}
Derivation
  1. Initial program 53.2%

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

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

      \[\leadsto \log \left(x \cdot \left(2 - \color{blue}{\frac{0.5 \cdot 1}{{x}^{2}}}\right)\right) \]
    2. metadata-eval97.4%

      \[\leadsto \log \left(x \cdot \left(2 - \frac{\color{blue}{0.5}}{{x}^{2}}\right)\right) \]
  5. Simplified97.4%

    \[\leadsto \log \color{blue}{\left(x \cdot \left(2 - \frac{0.5}{{x}^{2}}\right)\right)} \]
  6. Final simplification97.4%

    \[\leadsto \log \left(x \cdot \left(2 - \frac{0.5}{{x}^{2}}\right)\right) \]
  7. Add Preprocessing

Alternative 3: 96.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log 2 + \log x \end{array} \]
(FPCore (x) :precision binary32 (+ (log 2.0) (log x)))
float code(float x) {
	return logf(2.0f) + logf(x);
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = log(2.0e0) + log(x)
end function
function code(x)
	return Float32(log(Float32(2.0)) + log(x))
end
function tmp = code(x)
	tmp = log(single(2.0)) + log(x);
end
\begin{array}{l}

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

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

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

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

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

      \[\leadsto \log 2 + \color{blue}{\log x} \]
  5. Simplified95.9%

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

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

Alternative 4: 97.0% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \log \left(x + x\right) \end{array} \]
(FPCore (x) :precision binary32 (log (+ x x)))
float code(float x) {
	return logf((x + x));
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = log((x + x))
end function
function code(x)
	return log(Float32(x + x))
end
function tmp = code(x)
	tmp = log((x + x));
end
\begin{array}{l}

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

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

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

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

Alternative 5: 6.1% accurate, 207.0× speedup?

\[\begin{array}{l} \\ 0 \end{array} \]
(FPCore (x) :precision binary32 0.0)
float code(float x) {
	return 0.0f;
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = 0.0e0
end function
function code(x)
	return Float32(0.0)
end
function tmp = code(x)
	tmp = single(0.0);
end
\begin{array}{l}

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

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

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

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

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

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

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

    \[\leadsto 0 \]
  8. Add Preprocessing

Developer target: 99.2% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \log \left(x + \sqrt{x - 1} \cdot \sqrt{x + 1}\right) \end{array} \]
(FPCore (x)
 :precision binary32
 (log (+ x (* (sqrt (- x 1.0)) (sqrt (+ x 1.0))))))
float code(float x) {
	return logf((x + (sqrtf((x - 1.0f)) * sqrtf((x + 1.0f)))));
}
real(4) function code(x)
    real(4), intent (in) :: x
    code = log((x + (sqrt((x - 1.0e0)) * sqrt((x + 1.0e0)))))
end function
function code(x)
	return log(Float32(x + Float32(sqrt(Float32(x - Float32(1.0))) * sqrt(Float32(x + Float32(1.0))))))
end
function tmp = code(x)
	tmp = log((x + (sqrt((x - single(1.0))) * sqrt((x + single(1.0))))));
end
\begin{array}{l}

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

Reproduce

?
herbie shell --seed 2024100 
(FPCore (x)
  :name "Rust f32::acosh"
  :precision binary32
  :pre (>= x 1.0)

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

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