I assume you should use floor division to preserve accuracy. As a numerical example, say you want the first 100 /decimal/ digits of 3 / 173. That's the same as floor(10^100 * 3 / 173).

In [105]: from decimal import Decimal, getcontext
     ...: getcontext().prec = 300
     ...: print(10**100 * 91 // 173)
     ...: print(int(str(Decimal(91) / Decimal(173)).split(".")[1][:100]))
5260115606936416184971098265895953757225433526011560693641618497109826589595375722543352601156069364
5260115606936416184971098265895953757225433526011560693641618497109826589595375722543352601156069364

Gareth

On 10/02/2024 16:24, Georgi Guninski wrote:
Given SR constant, I want to extract the first L base B digits
of the fractional part.

So far the best solution I found is:
floor((SR(expression)*B**L).numerical_approx(digits=L)).digits(B)

Can I do better?

Why the following approach fails numerically:

```
def base_B_digits(A, B,prot=False):
     digits=[]
     fractional_part = A - int(A)
     while fractional_part != 0:
        digit = int(fractional_part * B)
        digits.append(digit)
        fractional_part = fractional_part * B - digit
        if prot:  print(fractional_part)
     return digits
```
tt=base_B_digits(SR(1/3).numerical_approx(digits=10),10,1)

The fractional part starts:

0.3333333333
0.3333333331
0.3333333314
0.3333333139
0.3333331393
...
0.7812500000
0.8125000000
0.1250000000
0.2500000000
0.5000000000
0.0000000000

I am not sure the program must terminate.

For A=1/4, the base 10 digits are computed correctly.


--
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/4ca77049-3bed-45b6-8e62-5fbda28285e3%40gmail.com.

Reply via email to