Search code examples
matlabfloating-pointnumbersnumber-formatting

Why did MATLAB delete my decimals?


Let's say I create some number A, of the order 10^4:

A = 81472.368639;
disp(A)
   8.1472e+04

That wasn't what I wanted. Where are my decimals? There should be six decimals more. Checking the variable editor shows me this:

Variable editor

Again, I lost my decimals. How do I keep these for further calculations?


Solution

  • Scientific notation, or why you didn't lose any decimals

    You didn't lose any decimals, this is just MATLAB's way of displaying large numbers. MATLAB rounds the display of numbers, both in the command window and in the variable editor, to one digit before the dot and four after that, using scientific notation. Scientific notation is the Xe+y notation, where X is some number, and y an integer. This means X times 10 to the power of y, which can be visualised as "shift the dot to the right for y places" (or to the left if y is negative).

    Force MATLAB to show you all your decimals

    Now that we know what MATLAB does, can we force it to show us our number? Of course, there're several options for that, the easiest is setting a longer format. The most used for displaying long numbers are format long and format longG, whose difference is apparent when we use them:

    format long
    A
    
    A =
    
         8.1472368639e+04
    
    format longG
    A
    
    A =
    
              81472.368639
    

    format long displays all decimals (up to 16 total) using scientific notation, format longG tries to display numbers without scientific notation but with most available decimals, again: as many as there are or up to 16 digits, both before and after the dot, in total.

    A more fancy solution is using disp(sprintf()) or fprintf if you want an exact number of decimals before the dot, after the dot, or both:

    fprintf('A = %5.3f\n',A) % \n is just to force a line break
    A = 81472.369
    disp(sprintf('A = %5.2f\n',A))
    A = 81472.37
    

    Finally, remember the variable editor? How do we get that to show our variable completely? Simple: click on the cell containing the number:

    Variable editor after clicking

    So, in short: we didn't lose any decimals along the way, MATLAB still stores them internally, it just displays less decimals by default.


    Other uses of format

    format has another nice property in that you can set format compact, which gets rid of all the additional empty lines which MATLAB normally adds in the command window:

    format compact
    format long
    A
    A =
         8.147236863931789e+04
    format longG
    A
    A =
              81472.3686393179
    

    which in my opinion is very handy when you don't want to make your command window very big, but don't want to scroll a lot either.

    format shortG and format longG are useful when your array has very different numbers in them:

    b = 10.^(-3:3);
    A.*b
    ans =
       1.0e+07 *
        0.0000    0.0001    0.0008    0.0081    0.0815    0.8147    8.1472
    format longG
    A.*b
    ans =
      Columns 1 through 3
                  81.472368639              814.72368639              8147.2368639
      Columns 4 through 6
                  81472.368639              814723.68639              8147236.8639
      Column 7
                  81472368.639
    format shortG
    A.*b
    ans =
           81.472       814.72       8147.2        81472   8.1472e+05   8.1472e+06   8.1472e+07
    

    i.e. they work like long and short on single numbers, but chooses the most convenient display format for each of the numbers.

    There's a few more exotic options, like shortE, shortEng, hex etc, but those you can find well documented in The MathWork's own documentation on format.