AllRounder.ai

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.

Enrol free

1.4.6. Controlling the appearance of floating point number

Interactive Audio Lesson

Session 1: Introduction to Display Formats in MATLAB

Unlock the classroom podcast

The transcript is above and free to read. A free account plays the conversation back.

Create a free account
Sarah
SarahInstructor

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?

Noah
Noah

I think it’s important because different formats can help us see more or less detail of the numbers we’re working with.

Sarah
SarahInstructor

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.'

Isabella
Isabella

So, what happens if we want to see more decimal places?

Sarah
SarahInstructor

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.

Akash
Akash

Are there ways to switch back to the original format?

Sarah
SarahInstructor

Yes, using 'format short' will revert it back. Always remember to choose the format that suits your needs for clarity.

Sarah
SarahInstructor

In summary, remembering the commands 'format short' and 'format long' will enhance your ability to control numeric outputs in MATLAB.

Session 2: Practical Example of Formatting

Unlock the classroom podcast

The transcript is above and free to read. A free account plays the conversation back.

Create a free account
Robert
RobertInstructor

Let's explore practical examples using 'format short' and 'format long' for the same variable. How confident do you feel entering these commands?

Ananya
Ananya

I’m ready to try it out!

Robert
RobertInstructor

Great! First, let’s set a variable x. Input ‘x = -163.6667’ and then display it with 'format short'.

Noah
Noah

I did it! It shows -163.6667.

Robert
RobertInstructor

Now, switch to 'format long' and check x again. What do you see?

Isabella
Isabella

Now it shows a longer number: -1.636666666666667e+002!

Robert
RobertInstructor

Excellent observation! This highlights how MATLAB can handle precision. Remember, adding a semicolon at the end suppresses the output during long calculations.

Robert
RobertInstructor

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:

  1. To display a variable x with a short format:
    - matlab
    >> format short
    >> x = -163.6667
    This results in:
    - matlab
    x = -163.6667
  2. To display x in long format:
    - matlab
    >> format long
    >> x = -163.6667
    This results in:
    - matlab
    x = -1.636666666666667e+002

Returning to Standard Format

To revert to the original settings, users can enter:

- matlab
>> format short

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.

Reference YouTube Videos

Audio Book

Voice:
Default Number of Displayed Decimals

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 account

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

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 account

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

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 account

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

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

Step-by-step examples to apply the section's ideas and test your understanding.

1

Setting x = -163.6667 and using 'format short' results in output -163.6667.

2

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.