Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
problem in reading large deimal number

Subject: problem in reading large deimal number

From: seeker_123

Date: 17 Jul, 2013 06:06:10

Message: 1 of 2

hiii

  my main intention to convert decimal number to IEEE 754 form.
so for that conversion i am going to use below function
-----------------------------------------------------------------------------------------------
function s = num2bin(x)
s = sprintf('%bx',x);
t = sprintf('%bx',2);
if t(1)==0
  s = reshape(fliplr(reshape(s,2,8)),1,16);
end
s = reshape(dec2bin(hex2dec(s'),4)',1,4*16);
-------------------------------------------------------------------------------------------------

function calling
fid = fopen('decimal.txt','r');
x = fscanf(fid,'%f',1)
y = num2bin(x)
fclose(fid)


where x will be decimal no.
which i am going to read from a file
this function works fine if decimal number is small
 if it is large like this
89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164809964723200
then it x is truncating
   8.9885e+307
 this i dont want because i am loosing precision
if i gave %d in scanf then it gives
     -2.1473e+009
this i don't want

i tried by giving %s while scanning it scan whole value as it is
 but then there is problem in num2bin function because sprintf gives wrong value for that

num2bin function works fine for such a large number because when i am direction giving such a large number as input to num2bin function it works very fine

Subject: problem in reading large deimal number

From: Steven_Lord

Date: 17 Jul, 2013 13:41:55

Message: 2 of 2



"seeker_123 " <vinuhere5653@gmail.com> wrote in message
news:ks5c8i$ls8$1@newscl01ah.mathworks.com...
> hiii
>
> my main intention to convert decimal number to IEEE 754 form.
> so for that conversion i am going to use below function
> -----------------------------------------------------------------------------------------------
> function s = num2bin(x)
> s = sprintf('%bx',x);
> t = sprintf('%bx',2);
> if t(1)==0
> s = reshape(fliplr(reshape(s,2,8)),1,16); end
> s = reshape(dec2bin(hex2dec(s'),4)',1,4*16);
> -------------------------------------------------------------------------------------------------
>
> function calling fid = fopen('decimal.txt','r');
> x = fscanf(fid,'%f',1)
> y = num2bin(x)
> fclose(fid)
>
> where x will be decimal no.
> which i am going to read from a file this function works fine if decimal
> number is small
> if it is large like this
> 89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164809964723200
> then it x is truncating
> 8.9885e+307
> this i dont want because i am loosing precision

That's unavoidable. The large number you posted is not exactly representable
as an IEEE 754 double precision number. The best you can do is to work
symbolically with Symbolic Math Toolbox.

s =
sym('89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164809964723200')

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us