1

Below is a printout of from my terminal when I create two vectors of ones. Does anyone know the reason why the second call to ones() issues a warning, while the first does not?

>> p1

p1 =

    0.7000

>> p2

p2 =

    0.3000

>> whos p1
  Name      Size            Bytes  Class     Attributes

  p1        1x1                 8  double              

>> whos p2
  Name      Size            Bytes  Class     Attributes

  p2        1x1                 8  double              

>> N

N =

   100

>> T1 = ones(N*p1,1);
>> T2 = ones(N*p2,1);
Warning: Size vector should be a row vector with integer elements. 
Vidar
  • 4,141
  • 5
  • 24
  • 30
  • related question: [About floating point precision: why the iteration numbers are not equal?](http://stackoverflow.com/questions/6477178/about-floating-point-precision-why-the-iteration-numbers-are-not-equal) (also check out the linked questions as well) – Amro Aug 29 '11 at 15:45

1 Answers1

5

Yes, you might think that 100*.3 would be an integer, but it is not. This is because 0.3 is not stored as exactly 0.3 in the IEEE numeric representation used. Most such decimal numbers are not represented exactly. Remember that numbers are stored in a binary form. The true decimal representation of what is stored when you enter 0.3 into matlab is:

0.299999999999999988897769753748434595763683319091796875

It is close to 0.3, but not exactly so.

http://www.mit.edu/~pwb/cssm/matlab-faq_toc.html

http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html

Sometimes a result will turn out to be an exact integer.

  • Sorry to revive such an old post, but how can I see the exact decimal representation of whatever is stored in memory as 0.3? Did you calculate it yourself or is there a way in Matlab to display it to this level of precision? – user13267 Nov 30 '12 at 02:56
  • @user13267 - I use my HPF tool. It is on the file exchange. But you can just use sprintf. For example, try this: sprintf('%0.55f',.3) –  Nov 30 '12 at 03:39
  • >> sprintf('%.55f', 0.3) ans = 0.2999999999999999900000000000000000000000000000000000000 >> sprintf('%.99f', 0.3) ans = 0.299999999999999990000000000000000000000000000000000000000000000000000000000000000000000000000000000 – user13267 Nov 30 '12 at 04:13
  • I got these; what was the significance of 0.55? How can we know how much to put there? My matlab version is MATLAB Version 7.9.0.529 (R2009b) – user13267 Nov 30 '12 at 04:15
  • @user13267 - It might be the semi-old version of MATLAB you are using. It works fine for me in 2012b. The 55 comes from my knowledge that all double precision numbers need no more than 55 non-zero digits when converted from their IEEE binary form into a decimal. (This came from writing HPF.) I think you can use my HPF tool in that MATLAB release, as it does not rely on the sprintf trick. Note that I do not mean that a double has more than 16 decimal digits or so that are correct, but that when you convert the binary form into a decimal, it dies off after about 55 decimal digits always. –  Nov 30 '12 at 04:43
  • sprintf('%0.55f',.3) ans = 0.2999999999999999888977697537484345957636833190917968750 –  Nov 30 '12 at 04:44