Curious about an empirically found continued fraction for tanh

First of all, and since this is my first question in this forum, I would like to specify that I am not a professional mathematician (but a philosophy teacher); I apologize by advance if something is wrong in my question.

I enjoy doing numerical computations on my leisure time, and at the end of the last summer, I was working on some personal routines related to the world of ISC. With the help of these pieces of code, I detected algorithmically several identities, among which the following one $$ \tanh z = \operatorname*{K}_{n=1}^{\infty} \frac{1 + \left((n-1) \frac{\pi}{4} z^{-1}\right)^2}{(2n-1) \frac{\pi}{4} z^{-1}} \tag{1} \label{1} $$

The previous notation is the one I use; I find it convenient and it can be found for instance in Continued Fractions with Applications by Lorentzen & Waadeland, but I know that some people don't like it; it has to be read the following way: $$ a_0 + \operatorname*{K}_{n=1}^{\infty} \frac{b_n}{a_n} = a_0 + \cfrac{b_1}{a_1 + \cfrac{b_2}{a_2 + \cfrac{b_3}{a_3 + \dotsb}}} $$

This continued fraction is nice when used for computing the hyperbolic tangent of numbers like $\pi/4$ and other simple multiples of $\pi$ since it will only involve integer numbers in the expansion of the continued fraction.

Of course I browsed a little in order to see what was known about it, for example here, but I didn't find anything similar. I also sent it to several professional mathematicians, who told me that it could be difficult to recognize easily whether this continued fraction was equivalent to some other identity or not.

I haven't myself the required skills to study further this expression, but I would be very happy to know more about it: is it something well-known? Is it something that comes trivially from some other identity? Is it (who knows?) something new?

Edit 1: I posted myself an answer to the question by thinking something new was found; but it happened to be related to the precision in the computation. For that reason I may delete this answer in the future.

Edit 2: In this edit I am going to explain more how I came up with the identity and to add a code that can be used to test the identity.

I will not post my C code here because it is too long but I can really share it if someone wants it. Basically: I coded in the end of last summer a program in C computing very quickly millions of random continued fractions; each one was computed up to its 36th convergent; convergence was checked by looking at the difference between 35th and 36th convergent. Any quadratic value was discarded as not interesting for my purpose. Remaining values were matched against all non-quadratic values in Shamos's catalogue of real numbers by computing the PSLQ vector for $[z,1,S]$ (where $z$ was a continued fraction and $S$ a number from Shamos's book). I used a personal hygienic C macro for the pslq algorithm that I share here. The precision when computing the PSLQ algorithm on double-precision numbers was "poor" (about 12 digits) but I focused on speed for this project. Whenever an interesting result was returned by the PSLQ algorithm (low norm of the returned vector), the continued fraction was computed a second time with the dd type from D.H. Bailey's libqd library (about 32 exact digits only but much quicker than any arbitrary precision library; furthermore precision in Shamos's book is only 20 digits). If the coefficients previously returned by the PSLQ algorithm were able to find again the relation with at least 17 exact digits, current parameters were printed. I made this program run for several weeks on three cores of a Raspberry Pi 2 (which is rather slow but which can compute long tasks without getting warm). Results had to be later generalized "by hand" when similar values were noticed by me. What I finally got was much more than I could expect. Above is one of these results.

Below is some code for Maxima (using the bfloat types):

/* Set the precision of the computation with bfloat numbers */
fpprec: 1024$

/* compute the n-th convergent of the continued fraction at x */
K(x,n) :=  block([ a,b,p1:1,p2:0,q1:0,q2:1],
  for k:1 thru n do (
    /* compute the k-th partial denominator as a */
    a:bfloat((2*k-1)*%pi/4/x),
    /* compute the k-th partial numerator as b */
    b:bfloat(1+((k-1)*%pi/4/x)^2),
    /* compute the k-th convergent as p2/q2 */
    p:a*p2+b*p1, q:a*q2+b*q1,
    /* shift values for simulating the matrix operation */
    p1:p2, p2:p, q1:q2, q2:q), p/q);

You can try it online by using this link (just change the value at the end of the code once the page will be loaded).

In a future edit I will add some plots illustrating the convergence of the continued fraction in \eqref{1} when the number of partial numerators (denominators) goes to infinity.

Edit 3:

Here is a plot showing the convergence of the continued fraction compared to the classical one for $\tanh$. The abscissa is the rank of the convergent (the last value of $n$ in the formula above):

enter image description here


Solution 1:

Your continued fraction is a very special case of the general continued fraction for the quotient of gamma functions conjectured in this post,see corollary (iii)

Edited: It is also a special case of the hyperbolic

$$\displaystyle\frac{1}{\sqrt{z}}\tanh\left(\frac{n}{m}\tan^{-1}\left(\frac{\sqrt{z}}{k}\right)\right)=\cfrac{n}{km+\cfrac{z(m^2+n^2)}{3km+\cfrac{z(4m^2+n^2)}{5km+\cfrac{z(9m^2+n^2)}{7km+\cfrac{z(16m^2+n^2)}{9km+\ddots}}}}}$$

Incidentally, note its trigonometric companion,

$$\frac{1}{\sqrt{z}}\tan\left(\frac{n}{m}\tan^{-1}\left(\frac{\sqrt{z}}{k}\right)\right)=\cfrac{n}{km+\cfrac{z(m^2-n^2)}{3km+\cfrac{z(4m^2-n^2)}{5km+\cfrac{z(9m^2-n^2)}{7km+\cfrac{z(16m^2-n^2)}{9km+\ddots}}}}}$$

as discussed in this comment.