I'm not sure what is wrong with this code regarding the sin and cos function.
이전 댓글 표시
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>>
채택된 답변
추가 답변 (1개)
Azzi Abdelmalek
2014년 4월 6일
1 개 추천
You can check the value of cos(90*pi/180) and compare it with 10^(-20)
댓글 수: 5
liv
2014년 4월 6일
Azzi Abdelmalek
2014년 4월 6일
Type
pi
is it an exact pi?
liv
2014년 4월 6일
Azzi Abdelmalek
2014년 4월 6일
편집: Azzi Abdelmalek
2014년 4월 6일
OK, you know that the exact pi contains much more decimals. what Matlab or any other langage di is an approximation of pi
liv
2014년 4월 6일
카테고리
도움말 센터 및 File Exchange에서 Sensor Models에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!