Bill Allombert on Fri, 16 Feb 2024 12:20:31 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Numerical instability in extracting base B digits of the fractional part of real


On Fri, Feb 16, 2024 at 09:46:17AM +0200, Georgi Guninski wrote:
> I don't claim this is a bug, but it appears very counter intuitive to me.
> 
> Give real number $A$ and positive integer $B$, I am trying to get
> the base $B$ digits of frac(A) with given precision (they may be infinite).
> 
> So far the only reliable way to do this is to compute C=floor(B^L*A)
> and then write C in base B.

Yes, this the correct solution: digits(floor(B^L*A),B)

> I found another algorithm which agrees with chatGPT solution:

If it agree with chatGPT, it is probably wrong...

Your code do "A=C-d;" which leads to cancellation and accuracy loss.
This is a property of floating point numbers.

? a=.33
%6 = 0.33000000000000000000000000000000000000
? a*10-3
%7 = 0.30000000000000000000000000000000000000
? b=a*10-3
%8 = 0.30000000000000000000000000000000000000
? b*10-3
%9 = -2.350988701644575016E-38

The formula B^L*A does only multiplication, which does not have this problem

Cheers,
Bill