From: dpb on 29 Apr 2010 21:42 AS wrote: > dpb <none(a)non.net> wrote >> One straightahead way... >> digitspi = sscanf(strrep(num2str(pi,8),'.',''),'%1d')' > > That's intriguing... > > It would be nice, however, to not have to specify things like the 8 for > the number of digits and the %1d telling it that the decimal occurs > after the first digit. I.e., I'd like the function to work for > arbitrary numbers. The 8 as well as the num2str() was simply an example. Convert a double and you can have all the precision available via printf() The %1d doesn't tell it where the decimal occurs; strrep() replaced the decimal point w/ a null before sscanf() was called. The 1d is the "trick" to return each decimal digit as a separate number by specifying the field width of one for each conversion. A regexp() could remove that as well as a textscan() by skipping the decimal; my version of ML is somewhat dated and doesn't have either. > Finding the decimal point is not hard, as one can just use num2str and > then find '.'. But the problem still remains as to discovering how many > digits there are in a number. (There's an obvious issue here with > non-terminating decimals, but let's ignore that for now.) There are only 15-16 decimal digits in a double maximum...I don't know what the Mathematica function does but as noted above, you can convert a double to full precision -- any digits beyond those in the 53 bits of a floating point double mantissa are meaningless. > So, the question now reduces to finding a function to give me the number > of digits in a number. For example: > 12353 --> 5 > 32.7182 --> 6 > .9945 --> 4 > > Any thoughts on that? Yeah, it's pretty hard w/o some other ground rules -- like what's to say the precision of any floating point number is n decimal digits past the decimal point? The trailing zeros just _might_ be significant as well. What's the criterion? Well, just went and looked...for the case of realdigits(x), Mathematica does exactly that -- it returns the full double precision converted value; it _doesn't_ try to determine where the limit of significant digits are 'cuz it doesn't know, either. Here's the first example -- Give the list of digits and exponent: In[1]:= RealDigits[123.55555] Out[1]= {{1,2,3,5,5,5,5,5,0,0,0,0,0,0,0,0},3} What it does do that you didn't mention was also provides the exponent. So, the easiest thing to do to implement such a function imo would be to write the string using something like '%18.15e' which gives the fixed decimal point location and the trailing exponent each of which would be trivial to pick from the string and return. Adding the optional arguments for number of digits, etc., is sugar. The one example noted is that Mathematica has a built-in facility for infinite precision so that realdigits(pi,25) doesn't need any other folderol to go w/ it but you'll need toolbox or other means to do that in base Matlab. -- --
From: Matt Fig on 29 Apr 2010 21:50 Perhaps something like this will do what you want, for the examples you give: function D = get_digits(N) % Return the digits of N as individual elements of a vector. D = sscanf(strrep(sprintf('%23.15g',N),'.',''),'%1d')'; if ~D(1) D = D(2:end); end
First
|
Prev
|
Pages: 1 2 Prev: anovan with random effects and continuous predictors Next: photomosaic |