MATLAB: I’m not sure what is wrong with this code regarding the sin and cos function.

cosnot workingsin

Hello, I have a problem with the following code:
angle=input('what is the angle in degrees: ');
t=angle*(pi/180);
if cos(t)<10^(-20)
disp('error! cos is close to zero');
else
y=sin(t)/cos(t);
fprintf('tan is equal to %f',y);
end
My professor said that sin and cos work in radians so I converted the degrees to radians. However, whenever I test for something like 90 degrees (when cos is 0), it should be displaying the error message, but it's not.
I get the following
what is the angle in degrees: 90
tan is equal to 16331239353195370.000000>>

Best Answer

Like Azzi said, pi contains infinite decimal numbers, To avoid this ambiguity i recommend you to use the degree functions instead of radians cosd and sind,
cosd(90)=0
cos(pi/2)=6.1232e-017
Related Question