Cody

# Problem 44691. Comparison of floating-point numbers (singles)

Solution 2868914

Submitted on 22 Aug 2020 by Alfonso Nieto-Castanon
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
assessFunctionAbsence({'regexp', 'regexpi'}, 'FileName','compareDoubles.m') RE = regexp(fileread('compareDoubles.m'), '\w+', 'match'); tabooWords = {'ans'}; testResult = cellfun( @(z) ismember(z, tabooWords), RE ); msg = ['Please do not do that in your code!' char([10 13]) ... 'Found: ' strjoin(RE(testResult)) '.' char([10 13]) ... 'Banned word.' char([10 13])]; assert(~any( cellfun( @(z) ismember(z, tabooWords), RE ) ), msg)

2   Pass
A = 0; % Note: realmin ≈ 1.1755e-38 bVec = logspace(-1, -51, 101); ansVec = [repelem(false, 18) repelem(true, 83)]; for j = 1 : length(bVec) B = bVec(j); practicallyEqual1 = compareDoubles(A, B); practicallyEqual2 = compareDoubles(B, A); answer = ansVec(j); assert(practicallyEqual1 == answer, 'Wrong classification (1).') assert(practicallyEqual2 == answer, 'Wrong classification (2).') assert(isequal(class(practicallyEqual1), 'logical'), 'Wrong class.') end;

3   Pass
A = 0; bVec = -logspace(-1, -51, 101); ansVec = [repelem(false, 18) repelem(true, 83)]; for j = 1 : length(bVec) B = bVec(j); practicallyEqual1 = compareDoubles(A, B); practicallyEqual2 = compareDoubles(B, A); answer = ansVec(j); assert(practicallyEqual1 == answer, 'Wrong classification (1).') assert(practicallyEqual2 == answer, 'Wrong classification (2).') assert(isequal(class(practicallyEqual1), 'logical'), 'Wrong class.') end;

4   Pass
A = 0.00048828125; bVec = A - 2.^[-11 : -0.5 : -100]; ansVec = [repelem(false, 40) repelem(true, 161)]; for j = 1 : length(bVec) B = bVec(j); practicallyEqual1 = compareDoubles(A, B); practicallyEqual2 = compareDoubles(B, A); answer = ansVec(j); assert(practicallyEqual1 == answer, 'Wrong classification (1).') assert(practicallyEqual2 == answer, 'Wrong classification (2).') assert(isequal(class(practicallyEqual1), 'logical'), 'Wrong class.') end;

5   Pass
A = 1.1 + rand()/2; bVec = A + 2.^[-2 : -0.5 : -102]; ansVec = [repelem(false, 36) repelem(true, 165)]; for j = 1 : length(bVec) B = bVec(j); practicallyEqual1 = compareDoubles(A, B); practicallyEqual2 = compareDoubles(B, A); answer = ansVec(j); assert(practicallyEqual1 == answer, 'Wrong classification (1).') assert(practicallyEqual2 == answer, 'Wrong classification (2).') assert(isequal(class(practicallyEqual1), 'logical'), 'Wrong class.') end;

6   Pass
A = -104 + rand(); bVec = A + 2.^[+2 : -0.5 : -98]; ansVec = [repelem(false, 32) repelem(true, 169)]; for j = 1 : length(bVec) B = bVec(j); practicallyEqual1 = compareDoubles(A, B); practicallyEqual2 = compareDoubles(B, A); answer = ansVec(j); assert(practicallyEqual1 == answer, 'Wrong classification (1).') assert(practicallyEqual2 == answer, 'Wrong classification (2).') assert(isequal(class(practicallyEqual1), 'logical'), 'Wrong class.') end;

7   Pass
A = 1E18 + randi(1E14); bVec = A + 2.^[57 : -0.5 : -43]; ansVec = [repelem(false, 36) repelem(true, 165)]; for j = 1 : length(bVec) B = bVec(j); practicallyEqual1 = compareDoubles(A, B); practicallyEqual2 = compareDoubles(B, A); answer = ansVec(j); assert(practicallyEqual1 == answer, 'Wrong classification (1).') assert(practicallyEqual2 == answer, 'Wrong classification (2).') assert(isequal(class(practicallyEqual1), 'logical'), 'Wrong class.') end;

8   Pass
A = -1E18 - randi(1E14); bVec = A - 2.^[57 : -0.5 : -43]; ansVec = [repelem(false, 36) repelem(true, 165)]; for j = 1 : length(bVec) B = bVec(j); practicallyEqual1 = compareDoubles(A, B); practicallyEqual2 = compareDoubles(B, A); answer = ansVec(j); assert(practicallyEqual1 == answer, 'Wrong classification (1).') assert(practicallyEqual2 == answer, 'Wrong classification (2).') assert(isequal(class(practicallyEqual1), 'logical'), 'Wrong class.') end;

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!