Cody

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

Solution 1565316

Submitted on 20 Jun 2018
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   Fail
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;

A = 1×2 single row vector 0 0.1000 B = 1×2 single row vector 0 0.1000 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical A = 1×2 single row vector 0.1000 0 B = 1×2 single row vector 0.1000 0 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical

Error using assert The condition input argument must be a scalar logical. Error in Test2 (line 10) assert(practicallyEqual1 == answer, 'Wrong classification (1).')

3   Fail
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;

A = 1×2 single row vector 0 -0.1000 B = 1×2 single row vector 0 -0.1000 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical A = 1×2 single row vector -0.1000 0 B = 1×2 single row vector -0.1000 0 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical

Error using assert The condition input argument must be a scalar logical. Error in Test3 (line 9) assert(practicallyEqual1 == answer, 'Wrong classification (1).')

4   Fail
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;

A = 1×2 single row vector 1.0e-03 * 0.4883 0 B = 1×2 single row vector 1.0e-03 * 0.4883 0 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical A = 1×2 single row vector 1.0e-03 * 0 0.4883 B = 1×2 single row vector 1.0e-03 * 0 0.4883 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical

Error using assert The condition input argument must be a scalar logical. Error in Test4 (line 9) assert(practicallyEqual1 == answer, 'Wrong classification (1).')

5   Fail
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;

A = 1×2 single row vector 1.5981 1.8481 B = 1×2 single row vector 1.5981 1.8481 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical A = 1×2 single row vector 1.8481 1.5981 B = 1×2 single row vector 1.8481 1.5981 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical

Error using assert The condition input argument must be a scalar logical. Error in Test5 (line 9) assert(practicallyEqual1 == answer, 'Wrong classification (1).')

6   Fail
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;

A = 1×2 single row vector -103.9944 -99.9944 B = 1×2 single row vector -103.9944 -99.9944 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical A = 1×2 single row vector -99.9944 -103.9944 B = 1×2 single row vector -99.9944 -103.9944 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical

Error using assert The condition input argument must be a scalar logical. Error in Test6 (line 9) assert(practicallyEqual1 == answer, 'Wrong classification (1).')

7   Fail
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;

A = 1×2 single row vector 1.0e+18 * 1.0000 1.1442 B = 1×2 single row vector 1.0e+18 * 1.0000 1.1442 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical A = 1×2 single row vector 1.0e+18 * 1.1442 1.0000 B = 1×2 single row vector 1.0e+18 * 1.1442 1.0000 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical

Error using assert The condition input argument must be a scalar logical. Error in Test7 (line 9) assert(practicallyEqual1 == answer, 'Wrong classification (1).')

8   Fail
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;

A = 1×2 single row vector 1.0e+18 * -1.0001 -1.1442 B = 1×2 single row vector 1.0e+18 * -1.0001 -1.1442 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical A = 1×2 single row vector 1.0e+18 * -1.1442 -1.0001 B = 1×2 single row vector 1.0e+18 * -1.1442 -1.0001 practicallyEqual = 1×2 logical array 1 1 Name Size Bytes Class Attributes A 1x2 8 single B 1x2 8 single practicallyEqual 1x2 2 logical

Error using assert The condition input argument must be a scalar logical. Error in Test8 (line 9) assert(practicallyEqual1 == answer, 'Wrong classification (1).')

### Community Treasure Hunt

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

Start Hunting!