Rust f32::atanh

Percentage Accurate: 99.8% → 99.8%
Time: 4.8s
Alternatives: 6
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \tanh^{-1} x \end{array} \]
(FPCore (x) :precision binary32 (atanh x))
float code(float x) {
	return atanhf(x);
}
function code(x)
	return atanh(x)
end
function tmp = code(x)
	tmp = atanh(x);
end
\begin{array}{l}

\\
\tanh^{-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 6 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: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \end{array} \]
(FPCore (x) :precision binary32 (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))
float code(float x) {
	return 0.5f * log1pf(((2.0f * x) / (1.0f - x)));
}
function code(x)
	return Float32(Float32(0.5) * log1p(Float32(Float32(Float32(2.0) * x) / Float32(Float32(1.0) - x))))
end
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right)
\end{array}

Alternative 1: 99.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \end{array} \]
(FPCore (x) :precision binary32 (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))
float code(float x) {
	return 0.5f * log1pf(((2.0f * x) / (1.0f - x)));
}
function code(x)
	return Float32(Float32(0.5) * log1p(Float32(Float32(Float32(2.0) * x) / Float32(Float32(1.0) - x))))
end
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right)
\end{array}
Derivation
  1. Initial program 99.7%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Final simplification99.7%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]

Alternative 2: 99.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \mathsf{log1p}\left(x \cdot \frac{2}{1 - x}\right) \end{array} \]
(FPCore (x) :precision binary32 (* 0.5 (log1p (* x (/ 2.0 (- 1.0 x))))))
float code(float x) {
	return 0.5f * log1pf((x * (2.0f / (1.0f - x))));
}
function code(x)
	return Float32(Float32(0.5) * log1p(Float32(x * Float32(Float32(2.0) / Float32(Float32(1.0) - x)))))
end
\begin{array}{l}

\\
0.5 \cdot \mathsf{log1p}\left(x \cdot \frac{2}{1 - x}\right)
\end{array}
Derivation
  1. Initial program 99.7%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Step-by-step derivation
    1. associate-/l*99.4%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{2}{\frac{1 - x}{x}}}\right) \]
    2. associate-/r/99.7%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{2}{1 - x} \cdot x}\right) \]
  3. Applied egg-rr99.7%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{\frac{2}{1 - x} \cdot x}\right) \]
  4. Final simplification99.7%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(x \cdot \frac{2}{1 - x}\right) \]

Alternative 3: 98.7% accurate, 8.4× speedup?

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

\\
0.5 \cdot \left(2 \cdot x + 0.6666666666666666 \cdot \left(x \cdot \left(x \cdot x\right)\right)\right)
\end{array}
Derivation
  1. Initial program 99.7%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Taylor expanded in x around 0 98.5%

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

      \[\leadsto 0.5 \cdot \left(2 \cdot x + 0.6666666666666666 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot x\right)}\right) \]
  4. Applied egg-rr98.5%

    \[\leadsto 0.5 \cdot \left(2 \cdot x + 0.6666666666666666 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot x\right)}\right) \]
  5. Final simplification98.5%

    \[\leadsto 0.5 \cdot \left(2 \cdot x + 0.6666666666666666 \cdot \left(x \cdot \left(x \cdot x\right)\right)\right) \]

Alternative 4: 98.6% accurate, 9.9× speedup?

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

\\
0.5 \cdot \left(x \cdot \left(2 + 0.6666666666666666 \cdot \left(x \cdot x\right)\right)\right)
\end{array}
Derivation
  1. Initial program 99.7%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Taylor expanded in x around 0 98.5%

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

      \[\leadsto 0.5 \cdot \left(2 \cdot x + 0.6666666666666666 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot x\right)}\right) \]
  4. Applied egg-rr98.5%

    \[\leadsto 0.5 \cdot \left(2 \cdot x + 0.6666666666666666 \cdot \color{blue}{\left(\left(x \cdot x\right) \cdot x\right)}\right) \]
  5. Step-by-step derivation
    1. associate-*r*98.5%

      \[\leadsto 0.5 \cdot \left(2 \cdot x + \color{blue}{\left(0.6666666666666666 \cdot \left(x \cdot x\right)\right) \cdot x}\right) \]
    2. distribute-rgt-out98.4%

      \[\leadsto 0.5 \cdot \color{blue}{\left(x \cdot \left(2 + 0.6666666666666666 \cdot \left(x \cdot x\right)\right)\right)} \]
  6. Applied egg-rr98.4%

    \[\leadsto 0.5 \cdot \color{blue}{\left(x \cdot \left(2 + 0.6666666666666666 \cdot \left(x \cdot x\right)\right)\right)} \]
  7. Final simplification98.4%

    \[\leadsto 0.5 \cdot \left(x \cdot \left(2 + 0.6666666666666666 \cdot \left(x \cdot x\right)\right)\right) \]

Alternative 5: 97.1% accurate, 21.8× speedup?

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

\\
0.5 \cdot \left(x + x\right)
\end{array}
Derivation
  1. Initial program 99.7%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Taylor expanded in x around 0 96.6%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot {x}^{2} + 2 \cdot x}\right) \]
  3. Step-by-step derivation
    1. distribute-lft-out96.6%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot \left({x}^{2} + x\right)}\right) \]
    2. unpow296.6%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(2 \cdot \left(\color{blue}{x \cdot x} + x\right)\right) \]
  4. Simplified96.6%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot \left(x \cdot x + x\right)}\right) \]
  5. Taylor expanded in x around 0 97.0%

    \[\leadsto 0.5 \cdot \color{blue}{\left(2 \cdot x\right)} \]
  6. Step-by-step derivation
    1. count-297.0%

      \[\leadsto 0.5 \cdot \color{blue}{\left(x + x\right)} \]
  7. Simplified97.0%

    \[\leadsto 0.5 \cdot \color{blue}{\left(x + x\right)} \]
  8. Final simplification97.0%

    \[\leadsto 0.5 \cdot \left(x + x\right) \]

Alternative 6: 10.8% accurate, 109.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 99.7%

    \[0.5 \cdot \mathsf{log1p}\left(\frac{2 \cdot x}{1 - x}\right) \]
  2. Taylor expanded in x around 0 92.9%

    \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{2 \cdot x}\right) \]
  3. Step-by-step derivation
    1. count-292.9%

      \[\leadsto 0.5 \cdot \mathsf{log1p}\left(\color{blue}{x + x}\right) \]
  4. Simplified92.9%

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

      \[\leadsto 0.5 \cdot \color{blue}{\log \left(1 + \left(x + x\right)\right)} \]
    2. flip-+19.2%

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

      \[\leadsto 0.5 \cdot \log \left(\frac{\color{blue}{1} - \left(x + x\right) \cdot \left(x + x\right)}{1 - \left(x + x\right)}\right) \]
    4. flip-+-0.0%

      \[\leadsto 0.5 \cdot \log \left(\frac{1 - \left(x + x\right) \cdot \color{blue}{\frac{x \cdot x - x \cdot x}{x - x}}}{1 - \left(x + x\right)}\right) \]
    5. +-inverses-0.0%

      \[\leadsto 0.5 \cdot \log \left(\frac{1 - \left(x + x\right) \cdot \frac{x \cdot x - x \cdot x}{\color{blue}{0}}}{1 - \left(x + x\right)}\right) \]
    6. +-inverses-0.0%

      \[\leadsto 0.5 \cdot \log \left(\frac{1 - \left(x + x\right) \cdot \frac{x \cdot x - x \cdot x}{\color{blue}{x \cdot x - x \cdot x}}}{1 - \left(x + x\right)}\right) \]
    7. associate-*r/-0.0%

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

      \[\leadsto 0.5 \cdot \log \left(\frac{1 - \frac{\left(x + x\right) \cdot \color{blue}{0}}{x \cdot x - x \cdot x}}{1 - \left(x + x\right)}\right) \]
    9. +-inverses-0.0%

      \[\leadsto 0.5 \cdot \log \left(\frac{1 - \frac{\left(x + x\right) \cdot \color{blue}{\left(x - x\right)}}{x \cdot x - x \cdot x}}{1 - \left(x + x\right)}\right) \]
    10. difference-of-squares-0.0%

      \[\leadsto 0.5 \cdot \log \left(\frac{1 - \frac{\color{blue}{x \cdot x - x \cdot x}}{x \cdot x - x \cdot x}}{1 - \left(x + x\right)}\right) \]
    11. +-inverses-0.0%

      \[\leadsto 0.5 \cdot \log \left(\frac{1 - \frac{x \cdot x - x \cdot x}{\color{blue}{0}}}{1 - \left(x + x\right)}\right) \]
    12. +-inverses-0.0%

      \[\leadsto 0.5 \cdot \log \left(\frac{1 - \frac{x \cdot x - x \cdot x}{\color{blue}{x - x}}}{1 - \left(x + x\right)}\right) \]
    13. flip-+10.8%

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

      \[\leadsto 0.5 \cdot \color{blue}{\left(\log \left(1 - \left(x + x\right)\right) - \log \left(1 - \left(x + x\right)\right)\right)} \]
  6. Applied egg-rr10.8%

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

      \[\leadsto 0.5 \cdot \color{blue}{0} \]
  8. Simplified10.8%

    \[\leadsto 0.5 \cdot \color{blue}{0} \]
  9. Final simplification10.8%

    \[\leadsto 0 \]

Reproduce

?
herbie shell --seed 2023174 
(FPCore (x)
  :name "Rust f32::atanh"
  :precision binary32
  (* 0.5 (log1p (/ (* 2.0 x) (- 1.0 x)))))