Rust f32::acosh

Percentage Accurate: 53.4% → 98.2%
Time: 5.6s
Alternatives: 8
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 8 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: 53.4% 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.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 51.7%

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

    \[\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.9%

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

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

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

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

Alternative 2: 44.3% accurate, 2.0× speedup?

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

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

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. difference-of-sqr-151.7%

      \[\leadsto \log \left(x + \sqrt{\color{blue}{\left(x + 1\right) \cdot \left(x - 1\right)}}\right) \]
    2. sub-neg51.7%

      \[\leadsto \log \left(x + \sqrt{\left(x + 1\right) \cdot \color{blue}{\left(x + \left(-1\right)\right)}}\right) \]
    3. metadata-eval51.7%

      \[\leadsto \log \left(x + \sqrt{\left(x + 1\right) \cdot \left(x + \color{blue}{-1}\right)}\right) \]
  3. Applied egg-rr51.7%

    \[\leadsto \log \left(x + \sqrt{\color{blue}{\left(x + 1\right) \cdot \left(x + -1\right)}}\right) \]
  4. Taylor expanded in x around 0 -0.0%

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

    \[\leadsto \log \left(x + \color{blue}{0.875}\right) \]
  6. Final simplification44.2%

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

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

    \[\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 4: 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 51.7%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. difference-of-sqr-151.7%

      \[\leadsto \log \left(x + \sqrt{\color{blue}{\left(x + 1\right) \cdot \left(x - 1\right)}}\right) \]
    2. sub-neg51.7%

      \[\leadsto \log \left(x + \sqrt{\left(x + 1\right) \cdot \color{blue}{\left(x + \left(-1\right)\right)}}\right) \]
    3. metadata-eval51.7%

      \[\leadsto \log \left(x + \sqrt{\left(x + 1\right) \cdot \left(x + \color{blue}{-1}\right)}\right) \]
  3. Applied egg-rr51.7%

    \[\leadsto \log \left(x + \sqrt{\color{blue}{\left(x + 1\right) \cdot \left(x + -1\right)}}\right) \]
  4. Taylor expanded in x around 0 -0.0%

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

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

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

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

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

      \[\leadsto \color{blue}{\log x} \]
  8. Simplified44.1%

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

    \[\leadsto \log x \]

Alternative 5: 20.8% accurate, 207.0× speedup?

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

\\
0.8333333333333334
\end{array}
Derivation
  1. Initial program 51.7%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. difference-of-sqr-151.7%

      \[\leadsto \log \left(x + \sqrt{\color{blue}{\left(x + 1\right) \cdot \left(x - 1\right)}}\right) \]
    2. sub-neg51.7%

      \[\leadsto \log \left(x + \sqrt{\left(x + 1\right) \cdot \color{blue}{\left(x + \left(-1\right)\right)}}\right) \]
    3. metadata-eval51.7%

      \[\leadsto \log \left(x + \sqrt{\left(x + 1\right) \cdot \left(x + \color{blue}{-1}\right)}\right) \]
  3. Applied egg-rr51.7%

    \[\leadsto \log \left(x + \sqrt{\color{blue}{\left(x + 1\right) \cdot \left(x + -1\right)}}\right) \]
  4. Taylor expanded in x around 0 -0.0%

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

    \[\leadsto \color{blue}{0.8333333333333334} \]
  6. Final simplification20.8%

    \[\leadsto 0.8333333333333334 \]

Alternative 6: 22.2% accurate, 207.0× speedup?

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

\\
1.9583333333333333
\end{array}
Derivation
  1. Initial program 51.7%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. difference-of-sqr-151.7%

      \[\leadsto \log \left(x + \sqrt{\color{blue}{\left(x + 1\right) \cdot \left(x - 1\right)}}\right) \]
    2. sub-neg51.7%

      \[\leadsto \log \left(x + \sqrt{\left(x + 1\right) \cdot \color{blue}{\left(x + \left(-1\right)\right)}}\right) \]
    3. metadata-eval51.7%

      \[\leadsto \log \left(x + \sqrt{\left(x + 1\right) \cdot \left(x + \color{blue}{-1}\right)}\right) \]
  3. Applied egg-rr51.7%

    \[\leadsto \log \left(x + \sqrt{\color{blue}{\left(x + 1\right) \cdot \left(x + -1\right)}}\right) \]
  4. Taylor expanded in x around -inf -0.0%

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

    \[\leadsto \color{blue}{1.9583333333333333} \]
  6. Final simplification22.2%

    \[\leadsto 1.9583333333333333 \]

Alternative 7: 22.3% accurate, 207.0× speedup?

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

\\
2.09375
\end{array}
Derivation
  1. Initial program 51.7%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. log1p-expm1-u51.7%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log \left(x + \sqrt{x \cdot x - 1}\right)\right)\right)} \]
    2. expm1-udef51.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{e^{\log \left(x + \sqrt{x \cdot x - 1}\right)} - 1}\right) \]
    3. add-exp-log51.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\left(x + \sqrt{x \cdot x - 1}\right)} - 1\right) \]
    4. fma-neg51.8%

      \[\leadsto \mathsf{log1p}\left(\left(x + \sqrt{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right) - 1\right) \]
    5. metadata-eval51.8%

      \[\leadsto \mathsf{log1p}\left(\left(x + \sqrt{\mathsf{fma}\left(x, x, \color{blue}{-1}\right)}\right) - 1\right) \]
  3. Applied egg-rr51.8%

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

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

    \[\leadsto \color{blue}{2.09375} \]
  6. Final simplification22.3%

    \[\leadsto 2.09375 \]

Alternative 8: 22.3% accurate, 207.0× speedup?

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

\\
2.1458333333333335
\end{array}
Derivation
  1. Initial program 51.7%

    \[\log \left(x + \sqrt{x \cdot x - 1}\right) \]
  2. Step-by-step derivation
    1. log1p-expm1-u51.7%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\log \left(x + \sqrt{x \cdot x - 1}\right)\right)\right)} \]
    2. expm1-udef51.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{e^{\log \left(x + \sqrt{x \cdot x - 1}\right)} - 1}\right) \]
    3. add-exp-log51.7%

      \[\leadsto \mathsf{log1p}\left(\color{blue}{\left(x + \sqrt{x \cdot x - 1}\right)} - 1\right) \]
    4. fma-neg51.8%

      \[\leadsto \mathsf{log1p}\left(\left(x + \sqrt{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right) - 1\right) \]
    5. metadata-eval51.8%

      \[\leadsto \mathsf{log1p}\left(\left(x + \sqrt{\mathsf{fma}\left(x, x, \color{blue}{-1}\right)}\right) - 1\right) \]
  3. Applied egg-rr51.8%

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

    \[\leadsto \color{blue}{\log \left(\frac{-1}{x}\right) + \left(\log -0.5 + \left(0.09375 \cdot \frac{1}{{x}^{4}} + \left(0.052083333333333336 \cdot \frac{1}{{x}^{6}} + 0.25 \cdot \frac{1}{{x}^{2}}\right)\right)\right)} \]
  5. Simplified22.3%

    \[\leadsto \color{blue}{2.1458333333333335} \]
  6. Final simplification22.3%

    \[\leadsto 2.1458333333333335 \]

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