
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Floating Point Precision in MATLAB
In MATLAB, the numeric class consists of various data types, such as signed integers, unsigned integers, single-precision floating point number, and double-precision floating point numbers.
In MATLAB, the floating point precision basically defines the number of significant digits that can be specified and manipulated by performing a floating point arithmetic operation.
The IEEE 754 standard determines the default floating point precision in MATLAB. As per this standard, the floating point numbers are represented in the following two formats:
Single-Precision Floating Point Number
Double-Precision Floating Point Number
Single-Precision Floating Point Number
In MATLAB, a single-precision floating point number is a numeric data type used to represent real numbers (floating point numbers) with approximately 7 decimal digits of precision.
In single-precision floating point number, the floating point number is represented by using 32 bits. This precision consumes 4 bytes of memory per value.
The 32 bits of a single-precision floating point number are formatted as follows:
32 Bits | ||
---|---|---|
Sign | Exponent | Mantissa |
1 Bit (0 for Positive & 1 for Negative) | 8 Bits | 23 Bits |
In MATLAB, a data type "single" is used to specify the single-precision floating point number. Single-precision floating point numbers are mainly used in applications where memory efficiency is desired and double-precision is not required.
Example
The following MATLAB program illustrates the declaration of single-precision floating point number.
% MATLAB program to create a single-precision floating point number % Declaring a single-precision floating point number using the "single" data type Number1 = single (22.11); Number2 = single (1.141); % Verifying the data type of the variable whos
Output
Name Size Bytes Class Attributes Number1 1x1 4 single Number2 1x1 4 single
Explanation
In this MATLAB program, we started with creating two single-precision floating point numbers with the help of "single" data type, where we have passed the value "22.11" (for Number1) and "1.141" (for Number2) as an argument to the "single()" function. The "single()" function converts the input value to the single-precision. Finally, we call the "whos" function to display and verify the data types of the variables.
Double-Precision Floating Point Number
In MATLAB, the double-precision floating point number is another data type that provides a precision of approximately 15 decimal digits. The double-precision floating point number data type uses 64 bits to represent a floating point number. Double-precision floating point number occupies 8 bytes of memory per value.
The 64 bits of a double-precision floating point number are formatted as follows:
64 Bits | ||
---|---|---|
Sign | Exponent | Mantissa |
1 Bit (0 for Positive & 1 for Negative) | 11 Bits | 52 Bits |
To specify the double-precision floating point number in a MATLAB program, the data type "double" is used. The double-precision floating point number is mainly used in applications where higher precision and accuracy is desired like in scientific calculations, simulation, data analysis, etc.
It is important to note that MATLAB has the double-precision as the default representation for floating point numbers. Although, we can change it to single-precision by calling a simple MATLAB function. Also, MATLAB allows users to specify the precision through "single" or "double" data types while declaring variables.
Example
The following MATLAB program illustrates the declaration of double-precision floating point number.
% MATLAB program to create a double-precision floating point number % Declaring a double-precision floating point number using the "double" data type Number1 = double (3.45268); Number2 = double (7.15421); % Verifying the data type of the variable whos
Output
Name Size Bytes Class Attributes Number1 1x1 8 single Number2 1x1 8 single
Explanation
In this MATLAB program, we started with creating two double-precision floating point numbers with the help of "double" data type, where we have passed the value "3.45268" (for Number1) and "7.15421" (for Number2) as an argument to the "double()" function. The "double()" function converts the input value to the double-precision. Finally, we call the "whos" function to display and verify the data types of the variables.
Conclusion
In conclusion, the floating point precision in MATLAB specifies the number of significant digits that can be used in numerical calculations. The precision of a floating point number is determined by a specific data type, i.e. single or double. The floating point precision in MATLAB is very important to perform reliable and accurate numerical calculations.