Rust f32::acosh

Percentage Accurate: 52.6% → 98.3%
Time: 4.4s
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.6% 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.3% 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 58.3%

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

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

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

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

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

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

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

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

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

    \[\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 58.3%

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

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

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

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

    \[\leadsto \log \left(x + \color{blue}{\left(x - \frac{0.5}{x}\right)}\right) \]
  5. Step-by-step derivation
    1. *-un-lft-identity96.8%

      \[\leadsto \log \left(\color{blue}{1 \cdot x} + \left(x - \frac{0.5}{x}\right)\right) \]
    2. fma-def96.8%

      \[\leadsto \log \color{blue}{\left(\mathsf{fma}\left(1, x, x - \frac{0.5}{x}\right)\right)} \]
    3. sub-neg96.8%

      \[\leadsto \log \left(\mathsf{fma}\left(1, x, \color{blue}{x + \left(-\frac{0.5}{x}\right)}\right)\right) \]
    4. distribute-neg-frac96.8%

      \[\leadsto \log \left(\mathsf{fma}\left(1, x, x + \color{blue}{\frac{-0.5}{x}}\right)\right) \]
    5. metadata-eval96.8%

      \[\leadsto \log \left(\mathsf{fma}\left(1, x, x + \frac{\color{blue}{-0.5}}{x}\right)\right) \]
  6. Applied egg-rr96.8%

    \[\leadsto \log \color{blue}{\left(\mathsf{fma}\left(1, x, x + \frac{-0.5}{x}\right)\right)} \]
  7. Simplified43.0%

    \[\leadsto \log \color{blue}{\left(-2 + \left(x + -2\right)\right)} \]
  8. Taylor expanded in x around inf 43.7%

    \[\leadsto \log \color{blue}{x} \]
  9. Final simplification43.7%

    \[\leadsto \log x \]

Alternative 4: 21.2% accurate, 207.0× speedup?

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

\\
1.1068793402777777
\end{array}
Derivation
  1. Initial program 58.3%

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

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

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

      \[\leadsto {\left(\sqrt{\log \left(x + \sqrt{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right)}\right)}^{2} \]
    4. metadata-eval57.4%

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

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

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

    \[\leadsto {\color{blue}{1.0520833333333333}}^{2} \]
  6. Step-by-step derivation
    1. metadata-eval21.4%

      \[\leadsto \color{blue}{1.1068793402777777} \]
  7. Applied egg-rr21.4%

    \[\leadsto \color{blue}{1.1068793402777777} \]
  8. Final simplification21.4%

    \[\leadsto 1.1068793402777777 \]

Alternative 5: 21.3% accurate, 207.0× speedup?

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

\\
1.1614583333333333
\end{array}
Derivation
  1. Initial program 58.3%

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

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

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

      \[\leadsto {\left(\sqrt{\log \left(x + \sqrt{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right)}\right)}^{2} \]
    4. metadata-eval57.4%

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

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

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

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

    \[\leadsto 1.1614583333333333 \]

Alternative 6: 22.3% accurate, 207.0× speedup?

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

\\
2.109375
\end{array}
Derivation
  1. Initial program 58.3%

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

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

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

      \[\leadsto {\left(\sqrt{\log \left(x + \sqrt{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right)}\right)}^{2} \]
    4. metadata-eval57.4%

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

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

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

    \[\leadsto \color{blue}{2.109375} \]
  6. Final simplification22.5%

    \[\leadsto 2.109375 \]

Alternative 7: 23.7% accurate, 207.0× speedup?

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

\\
4.211046006944445
\end{array}
Derivation
  1. Initial program 58.3%

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

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

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

      \[\leadsto {\left(\sqrt{\log \left(x + \sqrt{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right)}\right)}^{2} \]
    4. metadata-eval57.4%

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

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

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

    \[\leadsto {\color{blue}{2.0520833333333335}}^{2} \]
  6. Step-by-step derivation
    1. metadata-eval24.3%

      \[\leadsto \color{blue}{4.211046006944445} \]
  7. Applied egg-rr24.3%

    \[\leadsto \color{blue}{4.211046006944445} \]
  8. Final simplification24.3%

    \[\leadsto 4.211046006944445 \]

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