Rust f32::acosh

Percentage Accurate: 52.7% → 98.6%
Time: 5.8s
Alternatives: 7
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 7 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.7% 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: 98.6% accurate, 1.0× speedup?

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

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

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

    \[\leadsto \log \color{blue}{\left(2 \cdot x - \left(0.5 \cdot \frac{1}{x} + 0.125 \cdot \frac{1}{{x}^{3}}\right)\right)} \]
  3. Step-by-step derivation
    1. *-commutative98.5%

      \[\leadsto \log \left(\color{blue}{x \cdot 2} - \left(0.5 \cdot \frac{1}{x} + 0.125 \cdot \frac{1}{{x}^{3}}\right)\right) \]
    2. associate-*r/98.5%

      \[\leadsto \log \left(x \cdot 2 - \left(\color{blue}{\frac{0.5 \cdot 1}{x}} + 0.125 \cdot \frac{1}{{x}^{3}}\right)\right) \]
    3. metadata-eval98.5%

      \[\leadsto \log \left(x \cdot 2 - \left(\frac{\color{blue}{0.5}}{x} + 0.125 \cdot \frac{1}{{x}^{3}}\right)\right) \]
    4. associate-*r/98.5%

      \[\leadsto \log \left(x \cdot 2 - \left(\frac{0.5}{x} + \color{blue}{\frac{0.125 \cdot 1}{{x}^{3}}}\right)\right) \]
    5. metadata-eval98.5%

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

    \[\leadsto \log \color{blue}{\left(x \cdot 2 - \left(\frac{0.5}{x} + \frac{0.125}{{x}^{3}}\right)\right)} \]
  5. Final simplification98.5%

    \[\leadsto \log \left(x \cdot 2 - \left(\frac{0.5}{x} + \frac{0.125}{{x}^{3}}\right)\right) \]

Alternative 2: 98.2% accurate, 1.9× speedup?

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

\\
\log \left(x + \left(x - \frac{0.5}{x}\right)\right)
\end{array}
Derivation
  1. Initial program 48.1%

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

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

      \[\leadsto \log \left(x + \left(x - \color{blue}{\frac{0.5 \cdot 1}{x}}\right)\right) \]
    2. metadata-eval98.0%

      \[\leadsto \log \left(x + \left(x - \frac{\color{blue}{0.5}}{x}\right)\right) \]
  4. Simplified98.0%

    \[\leadsto \log \left(x + \color{blue}{\left(x - \frac{0.5}{x}\right)}\right) \]
  5. Final simplification98.0%

    \[\leadsto \log \left(x + \left(x - \frac{0.5}{x}\right)\right) \]

Alternative 3: 96.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \mathsf{log1p}\left(\left(x + x\right) + -1\right) \end{array} \]
(FPCore (x) :precision binary32 (log1p (+ (+ x x) -1.0)))
float code(float x) {
	return log1pf(((x + x) + -1.0f));
}
function code(x)
	return log1p(Float32(Float32(x + x) + Float32(-1.0)))
end
\begin{array}{l}

\\
\mathsf{log1p}\left(\left(x + x\right) + -1\right)
\end{array}
Derivation
  1. Initial program 48.1%

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

    \[\leadsto \log \left(x + \color{blue}{x}\right) \]
  3. Step-by-step derivation
    1. count-296.6%

      \[\leadsto \log \color{blue}{\left(2 \cdot x\right)} \]
    2. sum-log96.4%

      \[\leadsto \color{blue}{\log 2 + \log x} \]
    3. log1p-expm1-u95.7%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log 2 + \log x\right)\right)} \]
    4. expm1-udef95.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{e^{\log 2 + \log x} - 1}\right) \]
    5. exp-sum95.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{e^{\log 2} \cdot e^{\log x}} - 1\right) \]
    6. add-exp-log95.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{2} \cdot e^{\log x} - 1\right) \]
    7. add-exp-log96.6%

      \[\leadsto \mathsf{log1p}\left(2 \cdot \color{blue}{x} - 1\right) \]
    8. count-296.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\left(x + x\right)} - 1\right) \]
  4. Applied egg-rr96.6%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\left(x + x\right) - 1\right)} \]
  5. Final simplification96.6%

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

Alternative 4: 96.9% 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 48.1%

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

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

    \[\leadsto \log \left(x + x\right) \]

Alternative 5: 44.1% accurate, 2.0× speedup?

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

\\
\log x
\end{array}
Derivation
  1. Initial program 48.1%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. pow1/248.1%

      \[\leadsto \log \left(x + \color{blue}{{\left(x \cdot x - 1\right)}^{0.5}}\right) \]
    2. add-cube-cbrt48.1%

      \[\leadsto \log \left(x + {\color{blue}{\left(\left(\sqrt[3]{x \cdot x - 1} \cdot \sqrt[3]{x \cdot x - 1}\right) \cdot \sqrt[3]{x \cdot x - 1}\right)}}^{0.5}\right) \]
    3. pow348.1%

      \[\leadsto \log \left(x + {\color{blue}{\left({\left(\sqrt[3]{x \cdot x - 1}\right)}^{3}\right)}}^{0.5}\right) \]
    4. pow-pow48.1%

      \[\leadsto \log \left(x + \color{blue}{{\left(\sqrt[3]{x \cdot x - 1}\right)}^{\left(3 \cdot 0.5\right)}}\right) \]
    5. fma-neg48.1%

      \[\leadsto \log \left(x + {\left(\sqrt[3]{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right)}^{\left(3 \cdot 0.5\right)}\right) \]
    6. metadata-eval48.1%

      \[\leadsto \log \left(x + {\left(\sqrt[3]{\mathsf{fma}\left(x, x, \color{blue}{-1}\right)}\right)}^{\left(3 \cdot 0.5\right)}\right) \]
    7. metadata-eval48.1%

      \[\leadsto \log \left(x + {\left(\sqrt[3]{\mathsf{fma}\left(x, x, -1\right)}\right)}^{\color{blue}{1.5}}\right) \]
  3. Applied egg-rr48.1%

    \[\leadsto \log \left(x + \color{blue}{{\left(\sqrt[3]{\mathsf{fma}\left(x, x, -1\right)}\right)}^{1.5}}\right) \]
  4. Taylor expanded in x around inf 44.2%

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

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

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

      \[\leadsto \color{blue}{\log x} \]
  6. Simplified44.2%

    \[\leadsto \color{blue}{\log x} \]
  7. Final simplification44.2%

    \[\leadsto \log x \]

Alternative 6: 3.1% accurate, 207.0× speedup?

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

\\
-1
\end{array}
Derivation
  1. Initial program 48.1%

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

    \[\leadsto \log \color{blue}{\left(2 \cdot x - \left(0.5 \cdot \frac{1}{x} + 0.125 \cdot \frac{1}{{x}^{3}}\right)\right)} \]
  3. Step-by-step derivation
    1. *-commutative98.5%

      \[\leadsto \log \left(\color{blue}{x \cdot 2} - \left(0.5 \cdot \frac{1}{x} + 0.125 \cdot \frac{1}{{x}^{3}}\right)\right) \]
    2. associate-*r/98.5%

      \[\leadsto \log \left(x \cdot 2 - \left(\color{blue}{\frac{0.5 \cdot 1}{x}} + 0.125 \cdot \frac{1}{{x}^{3}}\right)\right) \]
    3. metadata-eval98.5%

      \[\leadsto \log \left(x \cdot 2 - \left(\frac{\color{blue}{0.5}}{x} + 0.125 \cdot \frac{1}{{x}^{3}}\right)\right) \]
    4. associate-*r/98.5%

      \[\leadsto \log \left(x \cdot 2 - \left(\frac{0.5}{x} + \color{blue}{\frac{0.125 \cdot 1}{{x}^{3}}}\right)\right) \]
    5. metadata-eval98.5%

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

    \[\leadsto \log \color{blue}{\left(x \cdot 2 - \left(\frac{0.5}{x} + \frac{0.125}{{x}^{3}}\right)\right)} \]
  5. Taylor expanded in x around inf 96.5%

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

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

    \[\leadsto -1 \]

Alternative 7: 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 48.1%

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

    \[\leadsto \log \left(x + \color{blue}{x}\right) \]
  3. Step-by-step derivation
    1. count-296.6%

      \[\leadsto \log \color{blue}{\left(2 \cdot x\right)} \]
    2. sum-log96.4%

      \[\leadsto \color{blue}{\log 2 + \log x} \]
    3. log1p-expm1-u95.7%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log 2 + \log x\right)\right)} \]
    4. expm1-udef95.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{e^{\log 2 + \log x} - 1}\right) \]
    5. exp-sum95.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{e^{\log 2} \cdot e^{\log x}} - 1\right) \]
    6. add-exp-log95.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{2} \cdot e^{\log x} - 1\right) \]
    7. add-exp-log96.6%

      \[\leadsto \mathsf{log1p}\left(2 \cdot \color{blue}{x} - 1\right) \]
    8. count-296.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\left(x + x\right)} - 1\right) \]
  4. Applied egg-rr96.6%

    \[\leadsto \color{blue}{\mathsf{log1p}\left(\left(x + x\right) - 1\right)} \]
  5. Step-by-step derivation
    1. count-296.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{2 \cdot x} - 1\right) \]
    2. *-commutative96.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{x \cdot 2} - 1\right) \]
    3. add-exp-log96.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{e^{\log \left(x \cdot 2\right)}} - 1\right) \]
    4. expm1-def96.6%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\mathsf{expm1}\left(\log \left(x \cdot 2\right)\right)}\right) \]
    5. log1p-expm1-u96.6%

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

      \[\leadsto \log \color{blue}{\left(2 \cdot x\right)} \]
    7. count-296.6%

      \[\leadsto \log \color{blue}{\left(x + x\right)} \]
    8. flip-+-0.0%

      \[\leadsto \log \color{blue}{\left(\frac{x \cdot x - x \cdot x}{x - x}\right)} \]
    9. log-div-0.0%

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

    \[\leadsto \color{blue}{\log \left(x \cdot x - x \cdot x\right) - \log \left(x - x\right)} \]
  7. Step-by-step derivation
    1. unpow2-0.0%

      \[\leadsto \log \left(\color{blue}{{x}^{2}} - x \cdot x\right) - \log \left(x - x\right) \]
    2. unpow2-0.0%

      \[\leadsto \log \left({x}^{2} - \color{blue}{{x}^{2}}\right) - \log \left(x - x\right) \]
    3. +-inverses-0.0%

      \[\leadsto \log \color{blue}{0} - \log \left(x - x\right) \]
    4. +-inverses-0.0%

      \[\leadsto \log \color{blue}{\left(x - x\right)} - \log \left(x - x\right) \]
    5. +-inverses6.1%

      \[\leadsto \color{blue}{0} \]
  8. Simplified6.1%

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

    \[\leadsto 0 \]

Developer target: 99.1% 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 2023201 
(FPCore (x)
  :name "Rust f32::acosh"
  :precision binary32
  :pre (>= x 1.0)

  :herbie-target
  (log (+ x (* (sqrt (- x 1.0)) (sqrt (+ x 1.0)))))

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