Cody

# Problem 44690. Comparison of floating-point numbers (doubles)

Solution 1606970

Submitted on 11 Aug 2018 by William
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 ≈ 2.2251E-308 bVec = [1E-50, 1E-300, 2.2E-308, 1E-322, 9E-323, 6E-323, 5E-323, 4E-323, 2E-323, 9E-324, 5E-324, 0]; ansVec = [false, false, false, false, false, false, true, true, true, true, true, true]; 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 = -[1E-50, 1E-300, 2.2E-308, 1E-322, 9E-323, 6E-323, 5E-323, 4E-323, 2E-323, 9E-324, 5E-324]; ansVec = [false, false, false, false, false, false, true, true, true, true, true]; 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 = 1.1 + rand()/2; bVec = A + 2.^[-2 : -0.5 : -102]; ansVec = [repelem(false, 94) repelem(true, 107)]; 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 = -104 + rand(); bVec = A + 2.^[+2 : -0.5 : -98]; ansVec = [repelem(false, 90) repelem(true, 111)]; 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 = 1E18 + randi(1E14); bVec = A + 2.^[57 : -0.5 : -43]; ansVec = [repelem(false, 94) repelem(true, 107)]; 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, 94) repelem(true, 107)]; 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!