How to stop from rounding off the decimal numbers?

4 visualizaciones (últimos 30 días)
Virtualkeeda
Virtualkeeda el 4 de En. de 2015
Respondida: Virtualkeeda el 4 de En. de 2015
Hi,
if yupperleft(j)== piinterval(i) upperleft(i)= x1(j);
In the above code, yupperleft is the array which i have calculated by some formulation. piinterval is the array generated from : piinterval=(0:0.1:1);
Matlab is storing the value at 31 index of yupperleft as 0.1000. piinterval(2) will also be 0.1000.
But when comparing they are not getting equal. The reason is because yupperleft is actually 0.0999999999999999.
Is there any way so that i can round off or do something so that 0.0999999999999999 gets to 0.1000 actually not just for to show.
Thanks

Respuesta aceptada

Image Analyst
Image Analyst el 4 de En. de 2015
Use tolerances to compare floating point numbers. See the FAQ for code samples and an explanation: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Más respuestas (2)

Stephen23
Stephen23 el 4 de En. de 2015
Editada: Stephen23 el 4 de En. de 2015
The simplest method is to allow for some tolerance in the comparison:
A = 0.1;
B = 0.09999999;
Instead of
A==B
you should use
abs(A-B) < 1e-6 % pick this value to suit your needs

Virtualkeeda
Virtualkeeda el 4 de En. de 2015
Thank you so much @Image Analyst and @Stephen Cobeldick . The tolerance method worked.
Thank you @Star Strider yours roundn method is also working.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by