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.
(1 - SQRT(0.5))without actually using theSQRTfunction itself. – Kittoes0124 Apr 09 '24 at 23:28(1 - SQRT(0.5))without usingSQRTitself 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