# Matlab is making small errors during computations

10 Ansichten (letzte 30 Tage)
Seeker am 20 Jun. 2013
I am sure this question was already asked somewhere, but I was unable to find the answer.
In my code, Matlab is making very small errors during the basic computations.
For example I know that the result of the certain calculation should be equal to 0 and matlab is giving me some extremely small number (2.1E-19) instead.
This is the result of A-B. When I check A and B they appear to be equal but I am sure they differ in 19-th decimal place.
I am sure this is due to the way matlab stores numbers.
What is the best way to deal with it?
Many thanks!
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

random09983492 am 20 Jun. 2013
If you need to compare two numbers, you can use a tolerance. For example, instead of using a == b, use something like abs(a-b) < 1e-10.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

Anand am 20 Jun. 2013
You haven't given much information, but that's just floating point precision for you. There's tonnes of articles describing this, but the gist of it is that computers use a finite number of bits (and therefore digits) to represent numbers. So when you perform arithmetic operations on these numbers, you will not get 'exact' answers. For most numerical applications this is sufficient.
If this is not sufficient for you, consider using symbolic math.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Seeker am 20 Jun. 2013
Thanks for the prompt answers.
I am writing a code, where I am doing many computations. I was thing about "forcing" certain values to 0.
Should do the trick..
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Logical finden Sie in Help Center und File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by