Ruud H.G. van Tol on Sun, 15 Dec 2024 18:31:30 +0100 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
fdigits() implementation |
First try: fdigits( v, c=precision(.), s=1 )= { localprec( max( c+s+8, precision(.)+1 ) ); while( type(v) == "t_CLOSURE", v=v() ); localprec( max( precision(v+.)+1, precision(.) ) ); v+=.; my( r=digits( v * 10^(c+s-2) \ 1 ) ); if( #r < c+s-1, r= concat( vector(c+s-1-#r), r ) ); if( s > #r, s= 0 ); r= r[ s .. -1 ]; if( c < #r, r= r[ 1 .. c ] ); r; } Some example runs: ? precision(.) % 38 ? fdigits( 1 - 10^-20, 24 ) % [0, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 0] ? #fdigits( Pi ) % 38 ? fdigits( Pi, 10, 761 ) ERROR ? fdigits( ()->Pi, 10, 761 ) % [3, 4, 9, 9, 9, 9, 9, 9, 8, 3] ? fdigits( ()->Pi, 10, 19991 ) % [4, 0, 4, 9, 0, 7, 5, 5, 1, 7] -- Ruud