1

I have a constant 0.29289321881345247559915563789515..., which can be calculated using the equation (1 - √0.5) and then transformed into fixed-point format in order to be used with various sizes of unsigned integers. See the following table for examples:

Word Size Q Format Decimal Value Binary Value
8-bit Q0.3 5 101
16-bit Q0.7 75 1001011
32-bit Q0.15 19195 100101011111011
64-bit Q0.31 1257966796 1001010111110110000110011001100
128-bit Q0.63 5402926248376769403 100101011111011000011001100110000000110001000011001101101111011

My problem is that calculating these values becomes infeasible due to the fact that it involves the square root function and numbers that can get arbitrarily large. I remember learning about a "spigot algorithm" that outputs the digits of π using a bounded amount of memory and was hoping that something similar exists for constants such as the one described above. This paper came up during my search, but I have not been able to grok it well enough to translate into code. Maybe I'm not even going the right direction?

The function I'm really looking for will satisfy the following requirements:

  • outputs my constant to arbitrary levels of precision
  • internal calculations don't get arbitrarily large themselves, as processors are limited to fixed word sizes
  • no dependencies on square root, as the function we're implementing is square root

If it helps, here's the code I'm attempting to refactor; if the hard-coded constants can be eliminated, then it will work for any T.

  • It would help if you could elaborate on your specific problem in this post. –  Apr 09 '24 at 23:27
  • @TobySaunders-A'Court Sure, in what way can I elaborate further? I've tried my best to describe the exact problem already, as I "just" need a way of calculating (1 - SQRT(0.5)) without actually using the SQRT function itself. – Kittoes0124 Apr 09 '24 at 23:28
  • The statement is sort of inconcise: For instance, do you want us to suggest functions, or help you implement a spigot algorithm? –  Apr 09 '24 at 23:34
  • @TobySaunders-A'Court I'm not entirely sure the spigot algorithm is what I need, it's just the first concept I was vaguely aware of that might fit the bill. Any solution that allows me to calculate (1 - SQRT(0.5)) without using SQRT itself or exceeding a fixed word size would be acceptable. For example, a machine that is restricted to 32-bit words cannot use calculations that exceed this limit. – Kittoes0124 Apr 09 '24 at 23:44

1 Answers1

0

I am not sure to properly understand by, if you want to compute $\left(1-\sqrt{\frac{1}{2}}\right)$, why not to write it as $$\left(1-\sqrt{\frac{1}{2}}\right)=\frac 3 {10}-\frac 7 {10}\sum_{n=1}^\infty \binom{\frac{1}{2}}{n} \frac 1{49^n}$$ If you want to know how many terms $p$ have to be added for a given accuracy, you need to solve for $p$ the equation $$\frac{\sqrt{\pi }\,\, 7^{-(2 p+1)}}{20 \,\Gamma \left(\frac{1}{2}-p\right)\, \Gamma (p+2)} \leq 10^{-k}$$

That is to say (more or less) $$\exp\left(\frac{589 }{151}p+\frac{681}{61}\right) \geq 10^{k}$$