Matlab basic - how to get around floating point?

  • Context: MATLAB 
  • Thread starter Thread starter catsarebad
  • Start date Start date
  • Tags Tags
    Floating Matlab Point
Click For Summary

Discussion Overview

The discussion revolves around the challenges of floating-point arithmetic in MATLAB, particularly how to handle situations where expected results are not exactly zero due to numerical precision issues. Participants explore various approaches and considerations related to numerical accuracy and comparisons in programming.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant seeks to understand why certain expressions in MATLAB, such as e = 1 - 3*(4/3 - 1) and sin(pi), do not yield zero as expected.
  • Another participant suggests that increasing numerical accuracy in MATLAB can help, but it may slow down computations. They also recommend using symbolic calculation tools like Mathematica or Maple for exact results.
  • A third participant emphasizes the importance of not relying on exact equality when comparing floating-point numbers, advocating for the use of a tolerance in comparisons.
  • Additional resources on floating-point arithmetic are provided for further reading.

Areas of Agreement / Disagreement

Participants express differing views on how to handle floating-point precision issues, with some advocating for increased numerical accuracy and others emphasizing the need for tolerance in comparisons. No consensus is reached on a single best approach.

Contextual Notes

Limitations include the potential for misunderstandings regarding the nature of floating-point representation and the implications of numerical precision in programming logic.

catsarebad
Messages
69
Reaction score
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
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.
 
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.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
3
Views
2K
Replies
2
Views
3K