[CSHARP-1685] Bson value comparisons incorrect between Double and Int64 Created: 07/Jun/16  Updated: 13/Jul/20  Resolved: 13/Jul/20

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Jeffrey Yemin Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The implementation of BsonValue.compareTo for numeric types does not match the behavior of the server. The problem is that casting the long to double and comparing two doubles will discard precision of very large or very small long values, and therefore result in two values comparing as equal when one is actually larger or smaller. A simple example is:

> ((double)long.MaxValue) > ((double)(long.MaxValue - 1))
false

The server algorithm for comparing Int64 and double is here: https://github.com/mongodb/mongo/blob/master/src/mongo/base/compare_numbers.h#L77-L100

It's easily portable to C#.


Generated at Wed Feb 07 21:40:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.