2atan (example 3.5)

?

Percentage Accurate: 76.1% → 99.4%
Time: 6.2s
Precision: binary64
Cost: 13184

?

\[\tan^{-1} \left(N + 1\right) - \tan^{-1} N \]
\[\tan^{-1}_* \frac{1}{\mathsf{fma}\left(N, 1 + N, 1\right)} \]
(FPCore (N) :precision binary64 (- (atan (+ N 1.0)) (atan N)))
(FPCore (N) :precision binary64 (atan2 1.0 (fma N (+ 1.0 N) 1.0)))
double code(double N) {
	return atan((N + 1.0)) - atan(N);
}
double code(double N) {
	return atan2(1.0, fma(N, (1.0 + N), 1.0));
}
function code(N)
	return Float64(atan(Float64(N + 1.0)) - atan(N))
end
function code(N)
	return atan(1.0, fma(N, Float64(1.0 + N), 1.0))
end
code[N_] := N[(N[ArcTan[N[(N + 1.0), $MachinePrecision]], $MachinePrecision] - N[ArcTan[N], $MachinePrecision]), $MachinePrecision]
code[N_] := N[ArcTan[1.0 / N[(N * N[(1.0 + N), $MachinePrecision] + 1.0), $MachinePrecision]], $MachinePrecision]
\tan^{-1} \left(N + 1\right) - \tan^{-1} N
\tan^{-1}_* \frac{1}{\mathsf{fma}\left(N, 1 + N, 1\right)}

Local Percentage Accuracy?

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.

Herbie found 2 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Target

Original76.1%
Target99.4%
Herbie99.4%
\[\tan^{-1} \left(\frac{1}{1 + N \cdot \left(N + 1\right)}\right) \]

Derivation?

  1. Initial program 76.3%

    \[\tan^{-1} \left(N + 1\right) - \tan^{-1} N \]
  2. Applied egg-rr76.8%

    \[\leadsto \color{blue}{\tan^{-1}_* \frac{N + \left(1 - N\right)}{\mathsf{fma}\left(N, N + 1, 1\right)}} \]
    Step-by-step derivation

    [Start]76.3

    \[ \tan^{-1} \left(N + 1\right) - \tan^{-1} N \]

    diff-atan [=>]76.8

    \[ \color{blue}{\tan^{-1}_* \frac{\left(N + 1\right) - N}{1 + \left(N + 1\right) \cdot N}} \]

    associate--l+ [=>]76.8

    \[ \tan^{-1}_* \frac{\color{blue}{N + \left(1 - N\right)}}{1 + \left(N + 1\right) \cdot N} \]

    +-commutative [=>]76.8

    \[ \tan^{-1}_* \frac{N + \left(1 - N\right)}{\color{blue}{\left(N + 1\right) \cdot N + 1}} \]

    *-commutative [=>]76.8

    \[ \tan^{-1}_* \frac{N + \left(1 - N\right)}{\color{blue}{N \cdot \left(N + 1\right)} + 1} \]

    fma-def [=>]76.8

    \[ \tan^{-1}_* \frac{N + \left(1 - N\right)}{\color{blue}{\mathsf{fma}\left(N, N + 1, 1\right)}} \]
  3. Simplified99.6%

    \[\leadsto \color{blue}{\tan^{-1}_* \frac{1}{\mathsf{fma}\left(N, 1 + N, 1\right)}} \]
    Step-by-step derivation

    [Start]76.8

    \[ \tan^{-1}_* \frac{N + \left(1 - N\right)}{\mathsf{fma}\left(N, N + 1, 1\right)} \]

    +-commutative [=>]76.8

    \[ \tan^{-1}_* \frac{\color{blue}{\left(1 - N\right) + N}}{\mathsf{fma}\left(N, N + 1, 1\right)} \]

    associate-+l- [=>]99.6

    \[ \tan^{-1}_* \frac{\color{blue}{1 - \left(N - N\right)}}{\mathsf{fma}\left(N, N + 1, 1\right)} \]

    +-inverses [=>]99.6

    \[ \tan^{-1}_* \frac{1 - \color{blue}{0}}{\mathsf{fma}\left(N, N + 1, 1\right)} \]

    metadata-eval [=>]99.6

    \[ \tan^{-1}_* \frac{\color{blue}{1}}{\mathsf{fma}\left(N, N + 1, 1\right)} \]

    +-commutative [=>]99.6

    \[ \tan^{-1}_* \frac{1}{\mathsf{fma}\left(N, \color{blue}{1 + N}, 1\right)} \]
  4. Final simplification99.6%

    \[\leadsto \tan^{-1}_* \frac{1}{\mathsf{fma}\left(N, 1 + N, 1\right)} \]

Alternatives

Alternative 1
Accuracy76.1%
Cost13120
\[\tan^{-1} \left(1 + N\right) - \tan^{-1} N \]

Reproduce?

herbie shell --seed 2023160 
(FPCore (N)
  :name "2atan (example 3.5)"
  :precision binary64

  :herbie-target
  (atan (/ 1.0 (+ 1.0 (* N (+ N 1.0)))))

  (- (atan (+ N 1.0)) (atan N)))