Rust f32::acosh

Percentage Accurate: 52.9% → 98.1%
Time: 5.0s
Alternatives: 4
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 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: 52.9% 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.1% 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 49.9%

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

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

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

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

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

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

Alternative 2: 96.8% 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 49.9%

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

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

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

Alternative 3: 44.2% 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 49.9%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. add-cube-cbrt49.9%

      \[\leadsto \log \left(x + \color{blue}{\left(\sqrt[3]{\sqrt{x \cdot x - 1}} \cdot \sqrt[3]{\sqrt{x \cdot x - 1}}\right) \cdot \sqrt[3]{\sqrt{x \cdot x - 1}}}\right) \]
    2. pow349.9%

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

      \[\leadsto \log \left(x + {\color{blue}{\left({\left(\sqrt{x \cdot x - 1}\right)}^{0.3333333333333333}\right)}}^{3}\right) \]
    4. pow1/249.7%

      \[\leadsto \log \left(x + {\left({\color{blue}{\left({\left(x \cdot x - 1\right)}^{0.5}\right)}}^{0.3333333333333333}\right)}^{3}\right) \]
    5. pow-pow49.7%

      \[\leadsto \log \left(x + {\color{blue}{\left({\left(x \cdot x - 1\right)}^{\left(0.5 \cdot 0.3333333333333333\right)}\right)}}^{3}\right) \]
    6. fma-neg49.7%

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

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

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

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

    \[\leadsto \log \color{blue}{\left(0.16666666666666666 \cdot \left({1}^{0.3333333333333333} \cdot \frac{1}{x}\right) + \left(-1 \cdot x + \left(0.3333333333333333 \cdot \frac{1}{x} + x\right)\right)\right)} \]
  5. Simplified44.5%

    \[\leadsto \log \color{blue}{\left(0.041666666666666664 + \left(1.3333333333333333 + x\right)\right)} \]
  6. Taylor expanded in x around inf 44.2%

    \[\leadsto \color{blue}{-1 \cdot \log \left(\frac{1}{x}\right)} \]
  7. 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} \]
  8. Simplified44.2%

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

    \[\leadsto \log x \]

Alternative 4: 21.5% accurate, 207.0× speedup?

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

\\
1.3333333333333333
\end{array}
Derivation
  1. Initial program 49.9%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. add-cube-cbrt49.9%

      \[\leadsto \log \left(x + \color{blue}{\left(\sqrt[3]{\sqrt{x \cdot x - 1}} \cdot \sqrt[3]{\sqrt{x \cdot x - 1}}\right) \cdot \sqrt[3]{\sqrt{x \cdot x - 1}}}\right) \]
    2. pow349.9%

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

      \[\leadsto \log \left(x + {\color{blue}{\left({\left(\sqrt{x \cdot x - 1}\right)}^{0.3333333333333333}\right)}}^{3}\right) \]
    4. pow1/249.7%

      \[\leadsto \log \left(x + {\left({\color{blue}{\left({\left(x \cdot x - 1\right)}^{0.5}\right)}}^{0.3333333333333333}\right)}^{3}\right) \]
    5. pow-pow49.7%

      \[\leadsto \log \left(x + {\color{blue}{\left({\left(x \cdot x - 1\right)}^{\left(0.5 \cdot 0.3333333333333333\right)}\right)}}^{3}\right) \]
    6. fma-neg49.7%

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

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

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

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

    \[\leadsto \color{blue}{0.5 + \left(-1 \cdot \log \left(\frac{1}{x}\right) + 0.16666666666666666 \cdot \frac{2 \cdot {x}^{3} + 6 \cdot \left(-0.16666666666666666 \cdot \left({1}^{0.3333333333333333} \cdot x\right) - 0.3333333333333333 \cdot x\right)}{{x}^{3}}\right)} \]
  5. Simplified21.6%

    \[\leadsto \color{blue}{1.3333333333333333} \]
  6. Final simplification21.6%

    \[\leadsto 1.3333333333333333 \]

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 2023200 
(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)))))