What's wrong with my program and or function?
Show older comments
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 Comment
Stephen23
on 29 Jul 2017
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.
Accepted Answer
More Answers (1)
Image Analyst
on 30 Jul 2017
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
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!