1.4.6 - Controlling the appearance of floating point number
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Interactive Audio Lesson
Listen to a student-teacher conversation explaining the topic in a relatable way.
Introduction to Display Formats in MATLAB
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Today, we're going to dive into how floating point numbers are displayed in MATLAB. Can anyone tell me why the display format might be important when working with numbers?
I think it’s important because different formats can help us see more or less detail of the numbers we’re working with.
Exactly! Display formats impact how we interpret and utilize numerical results. In MATLAB, by default, numbers are displayed with four decimal places. Let's start with the command 'format short.'
So, what happens if we want to see more decimal places?
Good question! You can use 'format long' to display all 15 digits of a floating point number. Let's try it out with a simple example.
Are there ways to switch back to the original format?
Yes, using 'format short' will revert it back. Always remember to choose the format that suits your needs for clarity.
In summary, remembering the commands 'format short' and 'format long' will enhance your ability to control numeric outputs in MATLAB.
Practical Example of Formatting
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Let's explore practical examples using 'format short' and 'format long' for the same variable. How confident do you feel entering these commands?
I’m ready to try it out!
Great! First, let’s set a variable x. Input ‘x = -163.6667’ and then display it with 'format short'.
I did it! It shows -163.6667.
Now, switch to 'format long' and check x again. What do you see?
Now it shows a longer number: -1.636666666666667e+002!
Excellent observation! This highlights how MATLAB can handle precision. Remember, adding a semicolon at the end suppresses the output during long calculations.
To summarize, controlling how MATLAB displays numbers is crucial for clarity in your work.
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
In this section, we explore how MATLAB handles the display of floating point numbers and how users can modify the format to show more or fewer digits. Specific commands such as 'format short' and 'format long' are introduced, along with examples demonstrating their effects on numerical output.
Detailed
Controlling the Appearance of Floating Point Numbers in MATLAB
This section delves into how MATLAB, by default, displays floating point numbers with a precision of four decimal places (e.g., 163.6667). However, it performs calculations using double precision, which can extend to 15 digits.
Commands for Controlling Format
- format short: Displays numbers in standard five decimal digits.
- format long: Shows all 15 digits for double-precision results.
Example Usage:
- To display a variable x with a short format:
This results in:
- To display x in long format:
This results in:
Returning to Standard Format
To revert to the original settings, users can enter:
This command ensures that outputs return to the initial presentation style.
Note
It's crucial to note that if commands yield extensive output, utilizing a semicolon (;) at the end of commands will suppress the display of results, allowing cleaner command execution while still being able to query variable values later.
Youtube Videos
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Default Number of Displayed Decimals
Chapter 1 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
MATLAB by default displays only 4 decimals in the result of the calculations, for example 163.6667, as shown in above examples. However, MATLAB does numerical calculations in double precision, which is 15 digits.
Detailed Explanation
When you perform calculations in MATLAB, the results are shown with four decimal places by default. For example, if you calculate a number like -163.6667, that's how it will appear in the output. However, MATLAB actually performs these calculations using a method that allows for much greater precision, up to 15 decimal places. This difference is important when you need highly accurate results, but often you don't need to see all 15 digits in everyday calculations.
Examples & Analogies
Think of it like a high-quality camera. The camera can capture many more details (15 digits of precision), but the pictures you usually see are cropped down to a more manageable level (4 decimal places) to make them easier to view and use. If you needed a super detailed image, you would get the original high-res file, much like getting the full floating point representation from MATLAB.
Using the Format Command
Chapter 2 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
The command format controls how the results of computations are displayed. Here are some examples of the different formats together with the resulting outputs.
format short
x=-163.6667
If we want to see all 15 digits, we use the command format long
format long
x= -1.636666666666667e+002
To return to the standard format, enter format short, or simply format.
Detailed Explanation
MATLAB provides a command called 'format' that lets you control how numbers are displayed after calculations. You can set it to 'short' for displaying a few decimals or 'long' to see all available digits (15). For example, when you type 'format short', it will display numbers like -163.6667. If you change it to 'format long', it will show you a more precise version like -1.636666666666667e+002, which gives different detailed information about the number. You can always switch back to the default behavior using 'format short'.
Examples & Analogies
Imagine switching between two types of digital watch faces. One shows the time simply in hours and minutes (like format short), while the other shows the seconds and fractions of seconds (like format long). Depending on what you need, you can choose the more detailed display or go back to a simpler time view.
Preventing Output Echo
Chapter 3 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Note - Up to now, we have let MATLAB repeat everything that we enter at the prompt (>>). Sometimes this is not quite useful, in particular when the output is pages en length. To prevent MATLAB from echoing what we type, simply enter a semicolon (;) at the end of the command. For example,
x=-163.6667;
and then ask about the value of x by typing,
x
x =
-163.6667
Detailed Explanation
In MATLAB, when you enter a command, it typically displays the command you wrote along with the result. Sometimes, this can lead to a lot of output being cluttered on the screen, especially if the results are lengthy. To avoid this echoing of commands, you can put a semicolon (;) at the end of any command. This tells MATLAB to perform the operation without showing it on the screen. You can still check the result later by typing the variable name, like you would do with 'x'.
Examples & Analogies
Think of it like chatting with a friend. If every time you spoke, your friend repeated back everything you said before giving a response, conversations would feel repetitive and slow. But if your friend only answered back with what's important and relevant (the result) without repeating your full statements (the command), the conversation would flow much smoother.
Key Concepts
-
Display Format: MATLAB displays numbers in four decimal places by default.
-
Command 'format short': Changes the display to the standard format of four decimal points.
-
Command 'format long': Increases the display to show all 15 digits of precision.
Examples & Applications
Setting x = -163.6667 and using 'format short' results in output -163.6667.
Using 'format long' for the same x gives -1.636666666666667e+002.
Memory Aids
Interactive tools to help you remember key concepts
Rhymes
Shorter decimals, shorter sights, long in numbers gives more insights.
Stories
Imagine a scientist working with lab data. They find that using 'format short' makes results less clear, so they switch to 'format long' to capture every detail of the measurements, ensuring accuracy.
Memory Tools
Remember 'SIMPLE' - Short for Insightful Measurement Precision Long Exceeds.
Acronyms
'SPL' - Short format
Precision Limited
Long format
Flash Cards
Glossary
- Floating Point Number
A numerical representation in MATLAB that can hold decimal values and is subject to formatting options.
- format short
A command in MATLAB that displays numbers with four decimal places.
- format long
A command in MATLAB that displays numbers with 15 digits of precision.
Reference links
Supplementary resources to enhance your learning experience.