The result of 0.3+0.15==0.45 is true or false?

Hi, I have a problem. I ran this expression "0.3+0.15==0.45" in Matlab the result is "0", but I ran the same expression in Mathematica, the result is "True". Why is that? AND if a,b are both variables for floating-point numbers, is it acceptable if I use "a==b" as the condition in the "if function"? Could anybody help me out? Thanks a lot!

 Respuesta aceptada

Daniel Shub
Daniel Shub el 13 de Dic. de 2011

2 votos

3 comentarios

Jan
Jan el 13 de Dic. de 2011
And a workaround:
if abs(0.3 + 0.15 - 0.45) < 10*eps
But the limit 10*eps is arbitrary! A relative error would be smarter:
if abs(((0.3 + 0.15) - 0.45) / 0.45) < 10*eps
But if the values are very small, even this might fail.
Jiasen
Jiasen el 14 de Dic. de 2011
Thanks
Jan
Jan el 14 de Dic. de 2011
@Jiasen: It is a good idea to read the complete FAQ - why not takine advantage from the problems of others...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 13 de Dic. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by