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!
- Category:
- Common Errors,
- New Feature
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.