Matlab ROUND function not rounding small decimals away
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joe
el 24 de Mzo. de 2015
Respondida: James Tursa
el 24 de Mzo. de 2015
Hello,
I am trying to round some numbers but the round function is not giving me what I think I should get.
I have "format long" enabled. After performing a 'meshgrid' fucntion, some of the values are as below:
ans =
66.599999999999994
66.599999999999994
66.599999999999994
66.599999999999994
66.599999999999994
66.599999999999994
59.399999999999999
59.399999999999999
59.399999999999999
59.399999999999999
But when I try
round(ans,8) I still get the same numbers as above. Even round(ans,2) gives me the same numbers. round(ans) gives me:
ans =
67
67
67
67
67
67
59
59
59
59
Which is not what want. Any ideas why 'round' is not actually rounding? I have Matlab 2015a installed
Thanks
0 comentarios
Respuesta aceptada
James Tursa
el 24 de Mzo. de 2015
It is just a display issue and the fact that IEEE double cannot represent the numbers exactly. E.g.,
>> num2strexact(66.6)
ans =
6.6599999999999994315658113919198513031005859375e1
So the closest number to 66.6 that is representable in IEEE double, when converted to an exact decimal equivalent, in fact has all of those 9's and the 4 at the end. Rounding the number to the nearest tenth or hundredth etc isn't going to change anything because there isn't anything closer to 66.6 in the IEEE double set that you can round to.
Similarly,
>> num2strexact(59.4)
ans =
5.939999999999999857891452847979962825775146484375e1
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!