-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog10.m
64 lines (58 loc) · 1.15 KB
/
log10.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
function L = log10(N)
% vpi/log10: logarithm to the base 10 of the (positive) vpi number N
% usage: L = log10(N)
%
%
% arguments: (input)
% N - a vpi scalar (positive) integer
% N must be positive, or an error will
% result.
%
%
% arguments: (output)
% L - The base 10 logarithm (as a double)
% of the vpi number N.
%
%
% Example:
% log10(vpi(10)^99)
% ans =
% 99
%
%
% See also: log, log2, power, sqrt
%
%
% Author: John D'Errico
% e-mail: woodchips@rochester.rr.com
% Release: 1.0
% Release date: 1/27/09
if isempty(N)
% empty propagates
L = [];
elseif numel(N) == 1
% error check
if N <= 0
error('N must represent a positive integer')
end
% is N small enough to do it exactly?
if N <= (2^53 - 1)
L = log10(double(N));
return
end
% shift the number to do it as a double
Norder = order(N);
excessdigits = max(0,(Norder - 18));
if excessdigits>0
N.digits(1:excessdigits) = [];
end
% compute the natural log by shifting the
% log of the mantissa.
L = log10(double(N)) + excessdigits;
else
% an array
L = zeros(size(N));
for i = 1:numel(N)
L(i) = log10(N(i));
end
end