Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Loren on the Art of MATLAB

December 27th, 2006

Displaying Numbers in MATLAB

The way MATLAB displays numbers sometimes confuses users. Occasionally someone posts a concern to the MATLAB newsgroup that the calculation just performed was done to only 4 digits but the user expected more decimals. Assuming the code is "typical" in some vague sense, the user's numeric information at this point is almost always sitting in double precision MATLAB variables.

Contents

Numeric Formats in MATLAB

Helpful posts typically follow up with the explanation that the display of output is controlled by the format setting and that the default numeric variables in MATLAB are indeed double precision, holding about 15 significant decimal digits. By default, MATLAB displays floating point values using what's called a scaled fixed point format, resulting in 4 digits displayed after the decimal point.

Here's the value of pi displayed with the default format in MATLAB.

format
mypi = pi
mypi =

    3.1416

and we can see that the variable mypi is stored as a double precision value.

class(mypi)
ans =

double

To see more digits, I can now use some of the other format options, for example

format long
mypi
mypi =

   3.141592653589793

Engineering Notation

In Release R14SP2, we enhanced format to include the engineering formats (short and long), resulting in values displayed with powers of 10 appearing in multiples of 3.

format short eng
manypis = mypi*[1 10 100 1000]
manypis =

     3.1416e+000    31.4159e+000   314.1593e+000     3.1416e+003

The new formats allow you some extra flexibility on how to view data before resorting to writing your own specialized formatting routines, for example using fprintf.

Interesting Diversion

While thinking about this post, I wondered about the history of scientific and engineering notation. Using some of my favorite web tools, I found out that scientific notation was first mentioned historically by Florian Cajori who wrote A History of Mathematical Notations (1928-29, 2 vols.).

I next wandered down the hall to talk to Steve of Image Processing blog fame. Wouldn't you know! He sent a letter to his son's teacher last year when the class was asking about what engineering notation was useful for. Steve found some interesting information engineering format for calculators. How cool is it that I can walk into his office and talk about something tangential like this bit of history and find out that not only does the topic interest him too, but he had his own reason at looking into it from a slightly different perspective. It made my day!

References

Here are some other references that might be helpful for those sorting out questions of display and precision.

Year End

It's nearly the end of the year 2006 as well as being approximately the first year birthday for this blog. It's been wonderful meeting you in this forum (and occasionally other ways as well!). I'd encourage you to continue sharing your thoughts with my during the next year as well. Happy New Year to all!


Get the MATLAB code

Published with MATLAB® 7.3

3 Responses to “Displaying Numbers in MATLAB”

  1. Ken Campbell replied on :

    I have found all of these blogs particularly interesting and am starting to incorporate some of the tricks I’ve read about it in my own code. I wouldn’t say the end result is particularly slick, but it’s getting better :-) Thanks for your efforts.

  2. Loren replied on :

    Ken-

    Thanks for the kind words. Here’s to a new year of good code and happy coding!

    –Loren

  3. Mohammad Reza replied on :

    One the tasks of an array oriented language is to generalise concepts. Take for example the decimal point. In 10.987; it seperate the number into two parts, those parts bigger than one and those that are a fraction. This can be generalised to present the following types of numbers:

    complex eg. 1j7 (1+7j)
    circular eg. 3p2 (3.5*pi or may be 3*pi^2)
    natural eg. 2×3 (2*exp(3))
    rational eg. 2r5 (2/5)
    base eg. 2b101 (101 in base 2)

    and more; imagination is the limit!

    The notations can of course be combined, eg:
    3r4j5 or 1.2e2r4

    just a thought.

    /m

Leave a Reply


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • J.B. Brown: Ah, and I am at fault for simply testing collinearity with the origin in the example above.
  • J.B. Brown: Indeed, > collinear( [0 3],[0 8],[0 -1e21+2e-15] ) ans = 1 > collinear( [0 3],[0 8],[0 -1e22+2e-15]...
  • OkinawaDolphin: Loren, thank you for telling me where to download timeit. Here are the two functions I just tested...
  • Loren: JB- It looks to me like Ilya’s solution and therefore yours are equivalent to the determinant. As Tim...
  • Loren: OkinawaDolphin, timeit can be downloaded from the File Exchange. Steve Eddins is the author. It does not ship...
  • OkinawaDolphin: It seems that neither R2007a nor R2007b have the function timeit, but I investigated computation time...
  • J.B. Brown: It would appear to me that Ilya Rozenfeld’s solution would be the cleanest. Just to help those who...
  • Loren: Markus- Congratulations on winning! And a nice illustration of how the size matters. Small enough, and the...
  • Markus: Hi Loren, which version is fastest also depends very much on the matrix dimensions. Look at my test function:...
  • Duncan: OkinawaDolphin, Regarding why your third example is slower than your second example, the result is in fact...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics