-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleadingdigit.m
74 lines (62 loc) · 1.28 KB
/
leadingdigit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function D = leadingdigit(INT,n)
% vpi/leadingdigit: Extract the highest order digits of a vpi object
% usage: D = leadingdigit(INT1)
% usage: D = leadingdigit(INT1,n)
%
%
% arguments: (input)
% INT - A vpi number
%
% n - (OPTIONAL) scalar, integer, positive, numeric
% Denotes the number of leading digits
% to be stripped off of INT. There is no
% maximum value for n, except the number
% of digits in INT.
%
% DEFAULT: n = 1
%
%
% arguments: (output)
% D - A double precision vector, contains
% the leading digit(s) of INT
%
%
% Example:
% INT = vpi(17)^17
% ans =
% 827240261886336764177
%
% leadingdigit(INT,2)
% ans =
% 8 2
%
%
% See also: trailingdigit, quotient, mod, rem
%
%
% Author: John D'Errico
% e-mail: woodchips@rochester.rr.com
% Release: 1.0
% Release date: 1/23/09
% default for n
if (nargin<2) || isempty(n)
n = 1;
end
% ensure that INT is a vpi
INT = vpi(INT);
% a scalar?
if numel(INT) == 1
NINT = order(INT)+1;
% n can not be too large
n = min(n,NINT);
D = INT.digits(NINT + (0:-1:(-n+1)));
elseif isempty(INT)
% empty propagates to empty
D = [];
else
% an array will result in a cell
D = cell(size(INT));
for i = 1:numel(INT)
D{i} = leadingdigit(INT(i),n);
end
end