Round error using if statement. Two things don't equal each other
Mostrar comentarios más antiguos
Ok this is the problem I am having.
I have an M-File containing the following...
>> x = sin(pi/6)
>> y = cos(pi/6)
>> mytan = x/y;
>> if mytan == tan (pi/6)
disp('equal')
else
disp('not equal')
end
......
When I run it, I want to get 'equal' to display but I keep getting 'not equal'
I know x = .5000 or .500000000000000 using format long
and y = .8660 or .866025403784439 using format long.
now mytan = x/y
comes out to be .5774 or 0.577350269189626 using format long
and tan(pi/6) is .5774 or 0.577350269189626 using format long
I should be getting the "equal to display"
however I am not.
Im pretty sure this is due to round off error, so how should I change my if statement to get the equal message to display.
Thank You
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 4 de Oct. de 2011
0 votos
This is a known and unavoidable issue in any finite-precision computer, no matter what the base of arithmetic (decimal, binary, hex, whatever) that it uses. For more information, please read this FAQ
Derrick
el 4 de Oct. de 2011
0 votos
Categorías
Más información sobre Just for fun en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!