function [r,n,f,nd] = base(N,b,mx)
% [r,n] = base(N,b)
% function for writing N from decimal to base b with maximum of mx significant digits
% n: number of digits
% r: vector of coefficients: N = sum(r.*b.^i), i=0:n-1
% f: vector of coefficients of fractional part
% nd: number of digits of fractional part

	s = sign(N);
	sn = '- +';
	N = s*N;
   P = floor(N);
   
   if P,
      d = [];
      n = 1 + floor(log(P)/log(b)); 
      for i = 1:n
   	   M = P / b;
      	P = floor(M);
         r(i) = b*(M-P);
         
         if r(i) < 10,
            x = num2str(r(i));
         else
            x = 55 + r(i);
         end
      	d = [x d];
      end
   else
      r = 0; n = 1;
      d = '0';
   end
   
   d=sprintf('(%c%ld)_10 = (%c%s', sn(s+2), N, sn(s+2), d);
   
   if N ~= floor(N)	% fractional
      P = b * (N - floor(N));
      d = [d '.'];
      for i = 1:mx
         M = floor(P);
      	P = b * (P - M);
         f(i) = M;
         
         if f(i) < 10,
            x = num2str(f(i));
         else
            x = 55 + f(i);
         end
         d = [d x];
         if ~P break, end
      end
      nd = i;
   else
     f=[]; nd=0;
   end
   
   d=sprintf('%s)_%d', d, b);
   disp(d);
   