What's wrong with my program and or function?
显示 更早的评论
so this is my function
function [m] = ImperialToMetric(ft,in)
% This function converts imperial measurement (in feet and inches)
% to a metric measurement (in metres)
% Input: ft = number of feet
% in = number of inches
% Output: m = measurement in metres
% Author: Tapiwa Zvenyika
% Convert foot to inches and add to existing inches
TotalInches = ((ft * 12) + in);
% Calculate the total number of m
m = ((TotalInches) / 39.3701);
end
and this is my test script
% LabTwoTaskThree.m test the function I wrote in Task 2 with the data
% I supplied and other randoms
% Author: Tapiwa Zvenyika
if ImperialToMetric(7,10) == 2.3876
disp('There is no problem with the code')
else
disp('There is a problem with the code')
end
if ImperialToMetric(6,2) == 1.8796
disp('There is no problem with the code')
else
disp('There is a problem with the code')
end
if ImperialToMetric(5,10) == 1.7780
disp('There is no problem with the code')
else
disp('There is a problem with the code')
end
disp('The Test was ran!')
I keep getting the opposite of what i want
1 个评论
Stephen23
2017-7-29
Floating-point numbers have been explained many times on this forum:
etc, etc, etc
"What's wrong with my program and or function?"
Always use a tolerance when comparing floating point values.
采纳的回答
更多回答(1 个)
Image Analyst
2017-7-30
Just do this:
% LabTwoTaskThree.m test the function I wrote in Task 2 with the data
% I supplied and other randoms
% Author: Tapiwa Zvenyika
if ismembertol(ImperialToMetric(7,10), 2.3876, 0.00001)
disp('There is no problem with the code')
else
disp('There is a problem with the code')
end
if ismembertol(ImperialToMetric(6,2), 1.8796, 0.00001)
disp('There is no problem with the code')
else
disp('There is a problem with the code')
end
if ismembertol(ImperialToMetric(5,10), 1.7780, 0.00001)
disp('There is no problem with the code')
else
disp('There is a problem with the code')
end
disp('Done! The Test was run!')
function [m] = ImperialToMetric(ft,in)
% This function converts imperial measurement (in feet and inches)
% to a metric measurement (in metres)
% Input: ft = number of feet
% in = number of inches
% Output: m = measurement in metres
% Author: Tapiwa Zvenyika
% Convert foot to inches and add to existing inches
TotalInches = ((ft * 12) + in);
% Calculate the total number of m
m = ((TotalInches) / 39.3701);
end
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!