1

In short, my question is:

Is a double in Matlab really a double, or is it a class with the additional property to act as an integer?

And here is the context and motivation for the question :)

>> 1:4
ans =
     1     2     3     4
>> class(ans)
ans =
double

Just doing this creates a double...

>> 1.00:4.00
ans =
     1     2     3     4
>> class(ans)
ans =
double

...as does this, even though it's printed as integers.

The floating point nature of the numbers only shows when greater numerical uncertainty is introduced.

>> acosd(cosd(1:4))
ans =
     0.999999999999900   1.999999999999947   3.000000000000045   4.000000000000041

Is a double in Matlab really a double, or is it a class with the additional property to act as an integer?


A vector defined with "integers" (which of course really is doubles), it can be used to index another vector, which is usually a property of integers.

>> A = [9 8 7 6]
A =
     9     8     7     6
>> idx = [4 3 2 1]
idx =
     4     3     2     1
>> class(idx)
ans =
double
>> A(idx)
ans =
     6     7     8     9

I also tried A(acosd(cosd(1:4))) which does not work.

AllanLRH
  • 1,124
  • 1
  • 12
  • 23

2 Answers2

5

It's just a double, but your command prompt format gives you the most compact view. Specifically,

format short

But you can change it to always display decimals, and lots of them, with

format longEng

There are many other options on the format help page.

Interestingly, you can use non-integer numbers as indexes with the colon operator, but it will warn. I would take this warning seriously as this indexing behavior is odd.

As I mentioned in my comments, the reason it is OK for MATLAB to use doubles for indexing has to do with the largest value of an integer that can be specified without losing precision in MATLAB. Double precision (64-bit) floating point numbers can exactly represent integers up to 2^53 (9,007,199,254,740,992) without losing any accuracy. The maximum array size allowed by MATLAB is far below this number, so there is no risk of indexing errors as a result of floating point precision.

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • But how come I can index with the doubles when defined as `[4 3 2 1]`, but not when they have (presumeable) lesser floating point accuracy (compared to when they ware originalley defined)? Guess Matlab should only distinguish from the class, not the number itself? – AllanLRH Nov 11 '13 at 23:42
  • @NovicePhysicist `double`s do not lose accuracy until above 53-bits, so MATLAB lets you use them (numbers over 9,007,199,254,740,992 will lose precision). See [here](http://stackoverflow.com/a/1848762/2778484). – chappjc Nov 11 '13 at 23:44
  • @NovicePhysicist If you do a test with something like `x=rand(4,1); x(1.6:2.4), x(1.2:2.6)` there is clearly some rounding going on (truncation?). I don't know what happens when you try to give an index over about 9 quadrillion, but I think that is over the [maximum array size in MATLAB on every platform listed](http://www.mathworks.com/support/solutions/en/data/1-IHYHFZ/index.html). – chappjc Nov 11 '13 at 23:52
  • Thank you, I have a better understanding of this now... though I still feel a bit uncompfortable using floating point numbers for indexing :) – AllanLRH Nov 11 '13 at 23:54
  • @NovicePhysicist Agreed. Don't do it! ;) – chappjc Nov 11 '13 at 23:55
  • Guess that I can't really avoid it, unless i do something like `A = [1 2 3 4]; idx = int8([1 3 2 4]); A(idx);`? – AllanLRH Nov 12 '13 at 09:01
  • 1
    @NovicePhysicist - Oh, well that is not worth doing. I meant avoiding decimal valued (not integral-valued) indexing. It's fine to use doubles to represent indexes because it warns if there are fractional components. – chappjc Nov 12 '13 at 21:07
4

In MATLAB, all numeric literals (i.e. numbers in the text of your program) are interpreted as double-precision. You must cast them explicitly to get any other type. It's worth remembering that IEEE floating point can exactly represent a wide range of integer values, up to FLINTMAX.

Edric
  • 23,676
  • 2
  • 38
  • 40