I want to know how can this be calculated mathematically.
Given a total balance $\mathrm{B}$ and an initial bet of $\mathrm{b_{init}}$, I want to calculate the max number of double-downs ($\mathrm{X}$) I can do before I get to $\mathrm{B}$ (i.e. max possible value being strictly lower than $\mathrm{B}$). The total bid quantity by doing consecutive double-downs is calculated as follows: $$ \sum_{x=0}^{X} 2^x \times \mathrm{b_{init}} $$
For example, given a total balance $\mathrm{B}=150000$ an initial bet of $\mathrm{b_{init}}=100$, the result value ($\mathrm{X}$) should be $\mathrm{X} = 9$, because $ \sum_{x=0}^{9} 2^x \times \mathrm{100} = 102300 < \mathrm{B} $, but $ \sum_{x=0}^{10} 2^x \times \mathrm{100} = 204700 > \mathrm{B} $
We need the largest $X$ such that $$\sum_{x=0}^{X}2^x\times b_{init}<B$$ But the left side quantity is a sum of $X+1$ terms of a geometric progression with ratio $2$ and first term $b_{init}$. So using the standard formula we write: $$(2^{X+1}-1)\times b_{init}<B\iff X<\log_2\left(\frac{B}{b_{init}}+1\right)-1$$ Thus using the greatest integer function, we can state our answer as: $$X_{max}=\left\lfloor\log_2\left(\frac{B}{b_{init}}+1\right)\right\rfloor-1$$ If $B>b_{init}$, we can check that $X_{max}$ is always non-negative.