help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NaN error


From: Los Griegos Engineering
Subject: Re: NaN error
Date: Wed, 12 Jan 2011 15:01:37 -0600

On Tue, 2011-01-11 at 13:52 -0500, John W. Eaton wrote:
> On 11-Jan-2011, Los Griegos Engineering wrote:
> 
> | Hello all,
> | 
> | I came across the 196 algorithm at:
> | http://mathworld.wolfram.com/196-Algorithm.html
> | 
> | I decided to code a version of it, and it works... until I increase the
> | iterations past a limit. I think the error is in the memory, but I'm
> | just not sure where the error is.
> | 
> | function [ a ] = rev (a,iter)
> | 
> | 
> | for k=1:iter
> | 
> | a=num2str(a);
> | b=[];
> | 
> | for i=length(a):-1:1
> | j=length(a)-i+1;
> | b(j)=a(i);
> | endfor
> | 
> | b=str2double(b);
> | a=str2double(a);
> | 
> | a=b+a;
> | 
> | endfor
> | endfunction
> | 
> | The results I get are:
> | octave-3.2.3> rev(123,1)
> | ans =  444
> | octave-3.2.3> rev(123,10)
> | ans =  1354353
> | octave-3.2.3> rev(123,11)
> | ans = NaN
> | 
> | octave-3.2.3> rev(12,11)
> | ans =  175857
> | octave-3.2.3> rev(12,14)
> | ans = NaN
> | octave-3.2.3> 
> | 
> | Is the error due to the memory address of the character array?
> | GNU Octave, version 3.2.3
> | Octave was configured for "i486-pc-linux-gnu".
> 
> You can write your function as
> 
>   function a = rev (a, iter)
>     for k = 1:iter
>       a = num2str (a);
>       b = fliplr (a);
>       b = str2double (b);
>       a = str2double (a);
>       a = b + a;
>     endfor
>   endfunction
> 
> to avoid the inner loop.  If I modify it as follows to see what the
> arguments to str2double are, and what the result of the call to
> str2double is
> 
>   function a = rev (a, iter)
>     for k = 1:iter
>       a = num2str (a);
>       b = fliplr (a);
>       bs = char (b)
>       b = str2double (b)
>       as = char (a)
>       a = str2double (a)
>       a = b + a;
>     endfor
>   endfunction
> 
> I see
> 
>   octave> rev (123, 11)
>   bs = 627627
>   b =  627627
>   as = 726726
>   a =  726726
>   bs = 60+e53453.1
>   b = NaN
>   as = 1.35435e+06
>   a =  1354350
>   ans = NaN
> 
> so the problem appears to bee that num2str starts returning numbers
> formatted as floating point constants, and when the characters are
> reversed you get something like "60+e53453.1" which is not a number.
> 
> Octave doesn't have arbitrary precision integers, so using built-in
> data types here, you will hit a limit at some point no matter what you
> do.
> 
> jwe


Thank you for the input. After some more thought, I came up with the
following. It works good as long as I keep the number of digits under
10, i.e. <999999999. I tested the iterations up to 250 with no issues,
other than being very large numbers.

function [ret]=reverseadd(a, iter)

#k controls the number of reveres to add up.
for k=1:iter

n=ceil(log10(a+1));

#Takes each number in "a" and stores it into matrix b size of number of
digits in "a".
for i=1:n
b(i)=(floor(a/10^(n-i)));
a=(a/10^(n-i))-(b(i));
a=a*10^(n-i)#+10^-(1e15);
j=n-i+1;

#Reversing "b"
c(j)=b(i);
endfor
 

#Takes each element of "b" and "c" and converts it to a number.
d=e=0;

for i=1:length(b)
d=d+b(i)*10^(n-i);
e=e+c(i)*10^(n-i);
endfor

#adds "d" and "e"
a=d+e;

endfor

ret=a;

endfunction

------------- 
Blessings,
Roman D. Griego
Los Griegos Engineering


Random Dhammapada Verse: He who has known his former lives and can see
heaven and hell themselves, while he has attained the extinction of
rebirth, a seer, master of transcendent knowledge, and master of all
masteries - that is what I call a brahmin. 423 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]