Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
1.4.6. Controlling the appearance of floating point number
Interactive Audio Lesson
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountToday, 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.
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountLet'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.
Overview
Short Summary
This section explains how to control the display format of floating point numbers in MATLAB, showcasing different formatting options to represent numerical results.
Medium Summary
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 Summary
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:- matlab
>> format short >> x = -163.6667- matlabx = -163.6667 - To display x in long format:
This results in:- matlab
>> format long >> x = -163.6667- matlabx = -1.636666666666667e+002
Returning to Standard Format
To revert to the original settings, users can enter:
>> format shortThis 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.
Reference YouTube Videos
Audio Book
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountMATLAB 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.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountThe 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.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountNote - 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
Core takeaways and short definitions to help you quickly recall the key ideas from this section.
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
Memory Aids
Interactive tools to help you remember key concepts