Hyperbolic arc-(co)tangent

Percentage Accurate: 8.5% → 99.7%
Time: 4.6s
Alternatives: 5
Speedup: 3.5×

Specification

?
\[\frac{1}{2} \cdot \log \left(\frac{1 + x}{1 - x}\right) \]
(FPCore (x)
  :precision binary64
  (* (/ 1.0 2.0) (log (/ (+ 1.0 x) (- 1.0 x)))))
double code(double x) {
	return (1.0 / 2.0) * log(((1.0 + x) / (1.0 - x)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (1.0d0 / 2.0d0) * log(((1.0d0 + x) / (1.0d0 - x)))
end function
public static double code(double x) {
	return (1.0 / 2.0) * Math.log(((1.0 + x) / (1.0 - x)));
}
def code(x):
	return (1.0 / 2.0) * math.log(((1.0 + x) / (1.0 - x)))
function code(x)
	return Float64(Float64(1.0 / 2.0) * log(Float64(Float64(1.0 + x) / Float64(1.0 - x))))
end
function tmp = code(x)
	tmp = (1.0 / 2.0) * log(((1.0 + x) / (1.0 - x)));
end
code[x_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[Log[N[(N[(1.0 + x), $MachinePrecision] / N[(1.0 - x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\frac{1}{2} \cdot \log \left(\frac{1 + x}{1 - x}\right)

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 5 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: 8.5% accurate, 1.0× speedup?

\[\frac{1}{2} \cdot \log \left(\frac{1 + x}{1 - x}\right) \]
(FPCore (x)
  :precision binary64
  (* (/ 1.0 2.0) (log (/ (+ 1.0 x) (- 1.0 x)))))
double code(double x) {
	return (1.0 / 2.0) * log(((1.0 + x) / (1.0 - x)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x)
use fmin_fmax_functions
    real(8), intent (in) :: x
    code = (1.0d0 / 2.0d0) * log(((1.0d0 + x) / (1.0d0 - x)))
end function
public static double code(double x) {
	return (1.0 / 2.0) * Math.log(((1.0 + x) / (1.0 - x)));
}
def code(x):
	return (1.0 / 2.0) * math.log(((1.0 + x) / (1.0 - x)))
function code(x)
	return Float64(Float64(1.0 / 2.0) * log(Float64(Float64(1.0 + x) / Float64(1.0 - x))))
end
function tmp = code(x)
	tmp = (1.0 / 2.0) * log(((1.0 + x) / (1.0 - x)));
end
code[x_] := N[(N[(1.0 / 2.0), $MachinePrecision] * N[Log[N[(N[(1.0 + x), $MachinePrecision] / N[(1.0 - x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\frac{1}{2} \cdot \log \left(\frac{1 + x}{1 - x}\right)

Alternative 1: 99.7% accurate, 0.8× speedup?

\[\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.14285714285714285, x \cdot x, 0.2\right), x \cdot x, 0.3333333333333333\right), \left(x \cdot x\right) \cdot x, x\right) \]
(FPCore (x)
  :precision binary64
  (fma
 (fma
  (fma 0.14285714285714285 (* x x) 0.2)
  (* x x)
  0.3333333333333333)
 (* (* x x) x)
 x))
double code(double x) {
	return fma(fma(fma(0.14285714285714285, (x * x), 0.2), (x * x), 0.3333333333333333), ((x * x) * x), x);
}
function code(x)
	return fma(fma(fma(0.14285714285714285, Float64(x * x), 0.2), Float64(x * x), 0.3333333333333333), Float64(Float64(x * x) * x), x)
end
code[x_] := N[(N[(N[(0.14285714285714285 * N[(x * x), $MachinePrecision] + 0.2), $MachinePrecision] * N[(x * x), $MachinePrecision] + 0.3333333333333333), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] * x), $MachinePrecision] + x), $MachinePrecision]
\mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.14285714285714285, x \cdot x, 0.2\right), x \cdot x, 0.3333333333333333\right), \left(x \cdot x\right) \cdot x, x\right)
Derivation
  1. Initial program 8.5%

    \[\frac{1}{2} \cdot \log \left(\frac{1 + x}{1 - x}\right) \]
  2. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
  3. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto x \cdot \color{blue}{\left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
    2. lower-+.f64N/A

      \[\leadsto x \cdot \left(1 + \color{blue}{{x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
    3. lower-*.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \color{blue}{\left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
    4. lower-pow.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\color{blue}{\frac{1}{3}} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) \]
    5. lower-+.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + \color{blue}{{x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)}\right)\right) \]
    6. lower-*.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \color{blue}{\left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)}\right)\right) \]
    7. lower-pow.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\color{blue}{\frac{1}{5}} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) \]
    8. lower-+.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \color{blue}{\frac{1}{7} \cdot {x}^{2}}\right)\right)\right) \]
    9. lower-*.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot \color{blue}{{x}^{2}}\right)\right)\right) \]
    10. lower-pow.f6499.7%

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.2 + 0.14285714285714285 \cdot {x}^{\color{blue}{2}}\right)\right)\right) \]
  4. Applied rewrites99.7%

    \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.2 + 0.14285714285714285 \cdot {x}^{2}\right)\right)\right)} \]
  5. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto x \cdot \color{blue}{\left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
    2. lift-+.f64N/A

      \[\leadsto x \cdot \left(1 + \color{blue}{{x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
    3. +-commutativeN/A

      \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) + \color{blue}{1}\right) \]
    4. distribute-lft-inN/A

      \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + \color{blue}{x \cdot 1} \]
    5. *-rgt-identityN/A

      \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + x \]
    6. lift-*.f64N/A

      \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + x \]
    7. associate-*r*N/A

      \[\leadsto \left(x \cdot {x}^{2}\right) \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) + x \]
    8. *-commutativeN/A

      \[\leadsto \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) \cdot \left(x \cdot {x}^{2}\right) + x \]
    9. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right), \color{blue}{x \cdot {x}^{2}}, x\right) \]
  6. Applied rewrites99.7%

    \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.14285714285714285, x \cdot x, 0.2\right), x \cdot x, 0.3333333333333333\right), \color{blue}{\left(x \cdot x\right) \cdot x}, x\right) \]
  7. Add Preprocessing

Alternative 2: 99.7% accurate, 1.1× speedup?

\[\mathsf{fma}\left(\mathsf{fma}\left(0.2, x \cdot x, 0.3333333333333333\right), \left(x \cdot x\right) \cdot x, x\right) \]
(FPCore (x)
  :precision binary64
  (fma (fma 0.2 (* x x) 0.3333333333333333) (* (* x x) x) x))
double code(double x) {
	return fma(fma(0.2, (x * x), 0.3333333333333333), ((x * x) * x), x);
}
function code(x)
	return fma(fma(0.2, Float64(x * x), 0.3333333333333333), Float64(Float64(x * x) * x), x)
end
code[x_] := N[(N[(0.2 * N[(x * x), $MachinePrecision] + 0.3333333333333333), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] * x), $MachinePrecision] + x), $MachinePrecision]
\mathsf{fma}\left(\mathsf{fma}\left(0.2, x \cdot x, 0.3333333333333333\right), \left(x \cdot x\right) \cdot x, x\right)
Derivation
  1. Initial program 8.5%

    \[\frac{1}{2} \cdot \log \left(\frac{1 + x}{1 - x}\right) \]
  2. Taylor expanded in x around 0

    \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
  3. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto x \cdot \color{blue}{\left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
    2. lower-+.f64N/A

      \[\leadsto x \cdot \left(1 + \color{blue}{{x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
    3. lower-*.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \color{blue}{\left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
    4. lower-pow.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\color{blue}{\frac{1}{3}} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) \]
    5. lower-+.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + \color{blue}{{x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)}\right)\right) \]
    6. lower-*.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \color{blue}{\left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)}\right)\right) \]
    7. lower-pow.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\color{blue}{\frac{1}{5}} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) \]
    8. lower-+.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \color{blue}{\frac{1}{7} \cdot {x}^{2}}\right)\right)\right) \]
    9. lower-*.f64N/A

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot \color{blue}{{x}^{2}}\right)\right)\right) \]
    10. lower-pow.f6499.7%

      \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.2 + 0.14285714285714285 \cdot {x}^{\color{blue}{2}}\right)\right)\right) \]
  4. Applied rewrites99.7%

    \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.2 + 0.14285714285714285 \cdot {x}^{2}\right)\right)\right)} \]
  5. Step-by-step derivation
    1. lift-*.f64N/A

      \[\leadsto x \cdot \color{blue}{\left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
    2. lift-+.f64N/A

      \[\leadsto x \cdot \left(1 + \color{blue}{{x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
    3. +-commutativeN/A

      \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) + \color{blue}{1}\right) \]
    4. distribute-lft-inN/A

      \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + \color{blue}{x \cdot 1} \]
    5. *-rgt-identityN/A

      \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + x \]
    6. lift-*.f64N/A

      \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + x \]
    7. associate-*r*N/A

      \[\leadsto \left(x \cdot {x}^{2}\right) \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) + x \]
    8. *-commutativeN/A

      \[\leadsto \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) \cdot \left(x \cdot {x}^{2}\right) + x \]
    9. lower-fma.f64N/A

      \[\leadsto \mathsf{fma}\left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right), \color{blue}{x \cdot {x}^{2}}, x\right) \]
  6. Applied rewrites99.7%

    \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.14285714285714285, x \cdot x, 0.2\right), x \cdot x, 0.3333333333333333\right), \color{blue}{\left(x \cdot x\right) \cdot x}, x\right) \]
  7. Taylor expanded in x around 0

    \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{5}, x \cdot x, 0.3333333333333333\right), \left(\color{blue}{x} \cdot x\right) \cdot x, x\right) \]
  8. Step-by-step derivation
    1. Applied rewrites99.7%

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(0.2, x \cdot x, 0.3333333333333333\right), \left(\color{blue}{x} \cdot x\right) \cdot x, x\right) \]
    2. Add Preprocessing

    Alternative 3: 99.5% accurate, 1.9× speedup?

    \[\mathsf{fma}\left(0.3333333333333333, \left(x \cdot x\right) \cdot x, x\right) \]
    (FPCore (x)
      :precision binary64
      (fma 0.3333333333333333 (* (* x x) x) x))
    double code(double x) {
    	return fma(0.3333333333333333, ((x * x) * x), x);
    }
    
    function code(x)
    	return fma(0.3333333333333333, Float64(Float64(x * x) * x), x)
    end
    
    code[x_] := N[(0.3333333333333333 * N[(N[(x * x), $MachinePrecision] * x), $MachinePrecision] + x), $MachinePrecision]
    
    \mathsf{fma}\left(0.3333333333333333, \left(x \cdot x\right) \cdot x, x\right)
    
    Derivation
    1. Initial program 8.5%

      \[\frac{1}{2} \cdot \log \left(\frac{1 + x}{1 - x}\right) \]
    2. Taylor expanded in x around 0

      \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
    3. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto x \cdot \color{blue}{\left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
      2. lower-+.f64N/A

        \[\leadsto x \cdot \left(1 + \color{blue}{{x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
      3. lower-*.f64N/A

        \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \color{blue}{\left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
      4. lower-pow.f64N/A

        \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\color{blue}{\frac{1}{3}} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) \]
      5. lower-+.f64N/A

        \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + \color{blue}{{x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)}\right)\right) \]
      6. lower-*.f64N/A

        \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \color{blue}{\left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)}\right)\right) \]
      7. lower-pow.f64N/A

        \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\color{blue}{\frac{1}{5}} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) \]
      8. lower-+.f64N/A

        \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \color{blue}{\frac{1}{7} \cdot {x}^{2}}\right)\right)\right) \]
      9. lower-*.f64N/A

        \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot \color{blue}{{x}^{2}}\right)\right)\right) \]
      10. lower-pow.f6499.7%

        \[\leadsto x \cdot \left(1 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.2 + 0.14285714285714285 \cdot {x}^{\color{blue}{2}}\right)\right)\right) \]
    4. Applied rewrites99.7%

      \[\leadsto \color{blue}{x \cdot \left(1 + {x}^{2} \cdot \left(0.3333333333333333 + {x}^{2} \cdot \left(0.2 + 0.14285714285714285 \cdot {x}^{2}\right)\right)\right)} \]
    5. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto x \cdot \color{blue}{\left(1 + {x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right)} \]
      2. lift-+.f64N/A

        \[\leadsto x \cdot \left(1 + \color{blue}{{x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)}\right) \]
      3. +-commutativeN/A

        \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) + \color{blue}{1}\right) \]
      4. distribute-lft-inN/A

        \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + \color{blue}{x \cdot 1} \]
      5. *-rgt-identityN/A

        \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + x \]
      6. lift-*.f64N/A

        \[\leadsto x \cdot \left({x}^{2} \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right)\right) + x \]
      7. associate-*r*N/A

        \[\leadsto \left(x \cdot {x}^{2}\right) \cdot \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) + x \]
      8. *-commutativeN/A

        \[\leadsto \left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right)\right) \cdot \left(x \cdot {x}^{2}\right) + x \]
      9. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(\frac{1}{3} + {x}^{2} \cdot \left(\frac{1}{5} + \frac{1}{7} \cdot {x}^{2}\right), \color{blue}{x \cdot {x}^{2}}, x\right) \]
    6. Applied rewrites99.7%

      \[\leadsto \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(0.14285714285714285, x \cdot x, 0.2\right), x \cdot x, 0.3333333333333333\right), \color{blue}{\left(x \cdot x\right) \cdot x}, x\right) \]
    7. Taylor expanded in x around 0

      \[\leadsto \mathsf{fma}\left(\frac{1}{3}, \color{blue}{\left(x \cdot x\right)} \cdot x, x\right) \]
    8. Step-by-step derivation
      1. Applied rewrites99.5%

        \[\leadsto \mathsf{fma}\left(0.3333333333333333, \color{blue}{\left(x \cdot x\right)} \cdot x, x\right) \]
      2. Add Preprocessing

      Alternative 4: 98.9% accurate, 3.5× speedup?

      \[\left(x + x\right) \cdot 0.5 \]
      (FPCore (x)
        :precision binary64
        (* (+ x x) 0.5))
      double code(double x) {
      	return (x + x) * 0.5;
      }
      
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      real(8) function code(x)
      use fmin_fmax_functions
          real(8), intent (in) :: x
          code = (x + x) * 0.5d0
      end function
      
      public static double code(double x) {
      	return (x + x) * 0.5;
      }
      
      def code(x):
      	return (x + x) * 0.5
      
      function code(x)
      	return Float64(Float64(x + x) * 0.5)
      end
      
      function tmp = code(x)
      	tmp = (x + x) * 0.5;
      end
      
      code[x_] := N[(N[(x + x), $MachinePrecision] * 0.5), $MachinePrecision]
      
      \left(x + x\right) \cdot 0.5
      
      Derivation
      1. Initial program 8.5%

        \[\frac{1}{2} \cdot \log \left(\frac{1 + x}{1 - x}\right) \]
      2. Step-by-step derivation
        1. lift-log.f64N/A

          \[\leadsto \frac{1}{2} \cdot \color{blue}{\log \left(\frac{1 + x}{1 - x}\right)} \]
        2. lift-/.f64N/A

          \[\leadsto \frac{1}{2} \cdot \log \color{blue}{\left(\frac{1 + x}{1 - x}\right)} \]
        3. log-divN/A

          \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\log \left(1 + x\right) - \log \left(1 - x\right)\right)} \]
        4. lower-unsound--.f64N/A

          \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(\log \left(1 + x\right) - \log \left(1 - x\right)\right)} \]
        5. lower-unsound-log.f64N/A

          \[\leadsto \frac{1}{2} \cdot \left(\color{blue}{\log \left(1 + x\right)} - \log \left(1 - x\right)\right) \]
        6. lift-+.f64N/A

          \[\leadsto \frac{1}{2} \cdot \left(\log \color{blue}{\left(1 + x\right)} - \log \left(1 - x\right)\right) \]
        7. +-commutativeN/A

          \[\leadsto \frac{1}{2} \cdot \left(\log \color{blue}{\left(x + 1\right)} - \log \left(1 - x\right)\right) \]
        8. add-flipN/A

          \[\leadsto \frac{1}{2} \cdot \left(\log \color{blue}{\left(x - \left(\mathsf{neg}\left(1\right)\right)\right)} - \log \left(1 - x\right)\right) \]
        9. metadata-evalN/A

          \[\leadsto \frac{1}{2} \cdot \left(\log \left(x - \color{blue}{-1}\right) - \log \left(1 - x\right)\right) \]
        10. lower--.f64N/A

          \[\leadsto \frac{1}{2} \cdot \left(\log \color{blue}{\left(x - -1\right)} - \log \left(1 - x\right)\right) \]
        11. lower-unsound-log.f648.5%

          \[\leadsto \frac{1}{2} \cdot \left(\log \left(x - -1\right) - \color{blue}{\log \left(1 - x\right)}\right) \]
      3. Applied rewrites8.5%

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

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(2 \cdot x\right)} \]
      5. Step-by-step derivation
        1. lower-*.f6498.9%

          \[\leadsto \frac{1}{2} \cdot \left(2 \cdot \color{blue}{x}\right) \]
      6. Applied rewrites98.9%

        \[\leadsto \frac{1}{2} \cdot \color{blue}{\left(2 \cdot x\right)} \]
      7. Step-by-step derivation
        1. lift-*.f64N/A

          \[\leadsto \color{blue}{\frac{1}{2} \cdot \left(2 \cdot x\right)} \]
        2. lift-/.f64N/A

          \[\leadsto \color{blue}{\frac{1}{2}} \cdot \left(2 \cdot x\right) \]
        3. metadata-evalN/A

          \[\leadsto \color{blue}{\frac{1}{2}} \cdot \left(2 \cdot x\right) \]
        4. *-commutativeN/A

          \[\leadsto \color{blue}{\left(2 \cdot x\right) \cdot \frac{1}{2}} \]
        5. lower-*.f6498.9%

          \[\leadsto \color{blue}{\left(2 \cdot x\right) \cdot 0.5} \]
        6. lift-*.f64N/A

          \[\leadsto \left(2 \cdot \color{blue}{x}\right) \cdot \frac{1}{2} \]
        7. count-2-revN/A

          \[\leadsto \left(x + \color{blue}{x}\right) \cdot \frac{1}{2} \]
        8. lower-+.f6498.9%

          \[\leadsto \left(x + \color{blue}{x}\right) \cdot 0.5 \]
      8. Applied rewrites98.9%

        \[\leadsto \color{blue}{\left(x + x\right) \cdot 0.5} \]
      9. Add Preprocessing

      Alternative 5: 1.6% accurate, 4.8× speedup?

      \[\frac{-1}{x} \]
      (FPCore (x)
        :precision binary64
        (/ -1.0 x))
      double code(double x) {
      	return -1.0 / x;
      }
      
      module fmin_fmax_functions
          implicit none
          private
          public fmax
          public fmin
      
          interface fmax
              module procedure fmax88
              module procedure fmax44
              module procedure fmax84
              module procedure fmax48
          end interface
          interface fmin
              module procedure fmin88
              module procedure fmin44
              module procedure fmin84
              module procedure fmin48
          end interface
      contains
          real(8) function fmax88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(4) function fmax44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
          end function
          real(8) function fmax84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmax48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
          end function
          real(8) function fmin88(x, y) result (res)
              real(8), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(4) function fmin44(x, y) result (res)
              real(4), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
          end function
          real(8) function fmin84(x, y) result(res)
              real(8), intent (in) :: x
              real(4), intent (in) :: y
              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
          end function
          real(8) function fmin48(x, y) result(res)
              real(4), intent (in) :: x
              real(8), intent (in) :: y
              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
          end function
      end module
      
      real(8) function code(x)
      use fmin_fmax_functions
          real(8), intent (in) :: x
          code = (-1.0d0) / x
      end function
      
      public static double code(double x) {
      	return -1.0 / x;
      }
      
      def code(x):
      	return -1.0 / x
      
      function code(x)
      	return Float64(-1.0 / x)
      end
      
      function tmp = code(x)
      	tmp = -1.0 / x;
      end
      
      code[x_] := N[(-1.0 / x), $MachinePrecision]
      
      \frac{-1}{x}
      
      Derivation
      1. Initial program 8.5%

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

        \[\leadsto \color{blue}{\frac{-1}{x}} \]
      3. Step-by-step derivation
        1. lower-/.f641.6%

          \[\leadsto \frac{-1}{\color{blue}{x}} \]
      4. Applied rewrites1.6%

        \[\leadsto \color{blue}{\frac{-1}{x}} \]
      5. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2025252 
      (FPCore (x)
        :name "Hyperbolic arc-(co)tangent"
        :precision binary64
        (* (/ 1.0 2.0) (log (/ (+ 1.0 x) (- 1.0 x)))))