Matlab adds a minor number to integer - why?

2 visualizaciones (últimos 30 días)
Oded T
Oded T el 5 de Mayo de 2019
Respondida: Stephen23 el 5 de Mayo de 2019
hi,
i noticed a very strange phenomanon:
i defined a few numbers:
t1 = 18.1
Delta = 0.1
t2 = t1 + Delta;
now open the cell of t2 and i see matlab add some very small value in the end of the decimal number, i.e.:
t2 = 18.200000000000003
what is the "3" in the end and how can i prevent it from happening?
I need the exact numbers and not additions,
tnanks,
Oded

Respuestas (1)

Stephen23
Stephen23 el 5 de Mayo de 2019
"i noticed a very strange phenomanon:"
Nope, not strange at all.
"what is the "3" in the end..."
Accumulated floating point error.
"...and how can i prevent it from happening?"
You can't. All operations on floating point values have the potential to collect floating point error. It is simply fundemental to how those values are stored. You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well:

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by