[SERVER-38851] Replace assert.close() with separate functions for integer and floating point closeness Created: 04/Jan/19  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Testing Infrastructure
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: James Wahlin Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Unresolved Votes: 0
Labels: tig-assertjs
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File Screen Shot 2019-01-06 at 12.04.21 AM.png     PNG File Screen Shot 2019-01-06 at 12.04.48 AM.png    
Issue Links:
Related
is related to SERVER-5044 $stdDev aggregation operator for stan... Closed
Assigned Teams:
Server Tooling & Methods
Operating System: ALL
Participants:

 Description   

assert.close() is used in our code both to compare for equality floating point numbers to a degree of precision and to integer values, evaluating only the X most significant digits. We should instead have separate functions for the 2. The current implementation can lead to surprising results when looking at it from only one use case:

> 
> assert.close(1, 2, '', 4)
2019-01-04T16:26:47.030-0500 E QUERY    [js] Error: 1 is not equal to 2 within 4 places, absolute error: 1, relative error: 0.5 :
doassert@src/mongo/shell/assert.js:20:14
assert.close@src/mongo/shell/assert.js:903:9
@(shell):1:1
> 
> // Result may be surprising to someone looking for a floating point precision check.
> assert.close(100001, 100002, '', 4)
> 
> assert.close(0.01, 0.02, '', 4)
2019-01-04T16:27:11.086-0500 E QUERY    [js] Error: 0.01 is not equal to 0.02 within 4 places, absolute error: 0.01, relative error: 0.5 :
doassert@src/mongo/shell/assert.js:20:14
assert.close@src/mongo/shell/assert.js:903:9
@(shell):1:1
> 
> assert.close(0.000001, 0.000002, '', 4)
> 
> // This fails a floating point precision check, but does not assert.
> assert.close(2000000.1, 2000000.2, '', 4)
> 



 Comments   
Comment by Steven Vannelli [ 10/May/22 ]

Moving this ticket to the Backlog and removing the "Backlog" fixVersion as per our latest policy for using fixVersions.

Comment by Max Hirschhorn [ 06/Jan/19 ]

assert.close() is used in our code both to compare for equality floating point numbers to a degree of precision and to integer values, evaluating only the X most significant digits. We should instead have separate functions for the 2.

james.wahlin, the assert.close() function compares both (1) the absolute error between two numbers (i.e. Math.abs(a - b)) as well as (2) the relative error between two numbers (i.e. Math.abs(a - b) / Math.abs(b)). I wouldn't say that the assert.close() function has surprising behavior for integral values in particular. It's just that larger values are more willing to be compared as equal to each other - this is a direct result of #2.

The places option to Python's unittest.TestCase.assertAlmostEqual() method is equivalent to #1. Its delta option is equivalent to Chai's assert.closeTo() function. Comparing two numbers according to their relative error was introduced by the changes from faf0663 as part of SERVER-5044, so some care needs to be taken to identify the tests that may be depending on it. Fortunately, the assert.close() function is one of our lesser used assertion functions and because they aren't part of our public mongo shell documentation, I don't believe we are bound to avoid changing their behavior between major versions of MongoDB.

My understanding is also that the assertion functions built into the mongo shell are owned by the TIG team (specifically STM) so I'm going to go ahead and triage this ticket.

Generated at Thu Feb 08 04:50:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.