Matlab basic - how to get around floating point?

In summary, when using MATLAB, it is important to keep in mind that it is a numerical solver and may not always provide an exact answer. To avoid issues with numbers not being exactly zero, it is recommended to increase the numerical accuracy or use a symbolic calculation code like Mathematica or Maple. It is also important to not program any logic decisions based on the equality of two real numbers, but instead allow for a tolerance when comparing them.
  • #1
catsarebad
72
0
i'm trying to learn MATLAB and as such i stumbled here while looking something up

http://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html

so the questions is

how does someone avoid something like

e = 1 - 3*(4/3 - 1) (this is example 1 in the webpage above)

not being zero

or this not being zero

sin(pi) (this is in example 1 again)


or this being zero
sqrt(1e-16 + 1) - 1 (in example 2)

thank you very much!
 
Physics news on Phys.org
  • #2
Matlab is a numerical solver. If having a result like 1e-16 is not close enough to zero for you, you can increase the numerical accuracy that Matlab provides, at the cost of slowing computations down. If you really want exactly zero, use a symbolic calculation code like Mathematica or Maple. They do algebraic manipulations and evaluations the way you would do them in school.
 
  • #4
This is a common problem. Just don't program any logic decisions based on equality of two real numbers. Always allow some tolerance when comparing two reals and apply the tolerance so that the safe decision will be made if the reals are within the tolerance.
 
  • #5


I understand the importance of accuracy and precision in numerical calculations. Floating point numbers in MATLAB can sometimes lead to unexpected results due to the limitations of computer hardware. To avoid these issues, there are a few strategies that can be implemented:

1. Use the "format" function to change the display format of numbers. This can help to see the actual value of a number rather than its rounded version.

2. Use the "eps" function to determine the smallest number that can be represented in MATLAB. This can help in setting a threshold for when a number should be considered as zero.

3. Use the "round" function to round numbers to a certain number of decimal places. This can help in reducing the effects of rounding errors.

4. Use the "sym" function to work with symbolic variables instead of floating point numbers. This can help to avoid any precision issues.

5. Use the "vpa" function to increase the precision of calculations. This can be helpful in cases where high precision is required.

It is also important to note that some mathematical operations, such as division and square root, can introduce errors in floating point calculations. Therefore, it is important to understand the limitations of the hardware and the algorithms being used, and to choose appropriate methods for each specific case.
 

1. What is a floating point number in Matlab?

A floating point number in Matlab is a numerical data type used to represent decimal numbers. It is called "floating point" because the decimal point can "float" or be placed anywhere in the number, allowing for a wide range of values to be represented.

2. How does Matlab handle floating point numbers?

Matlab uses the IEEE 754 standard for representing floating point numbers. This means that floating point numbers are stored in a binary format, with a sign bit, a fixed number of bits for the mantissa (fractional part), and a fixed number of bits for the exponent.

3. Can floating point numbers in Matlab be exact?

No, floating point numbers in Matlab (and in most programming languages) are not exact. This is because they are stored in a binary format, and some decimal numbers cannot be represented accurately in binary. This can lead to rounding errors and small discrepancies in calculations.

4. How can I improve the precision of floating point numbers in Matlab?

One way to improve the precision of floating point numbers in Matlab is by using the "format" function to change the display format to a higher precision, such as "format long". However, this does not change the underlying representation of the numbers and may not eliminate all rounding errors.

5. Are there any special functions in Matlab for handling floating point numbers?

Yes, Matlab has several built-in functions for handling floating point numbers, such as "eps" for calculating the machine epsilon (the smallest number that can be added to 1 without changing its value), "round" for rounding numbers to a specified number of decimal places, and "format" for changing the display format of floating point numbers.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
560
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
741
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Computing and Technology
Replies
4
Views
758
Back
Top