[CDRIVER-3938] Rounding errors in double type in bson_as_relaxed_extended_json() function Created: 26/Mar/21  Updated: 29/Jan/24  Resolved: 27/Apr/21

Status: Closed
Project: C Driver
Component/s: libbson
Affects Version/s: 1.17.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Fermín Galán Assignee: Roberto Sanchez
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CDRIVER-3812 bson_as_json outputs illegal JSON for... Backlog
Related
related to CDRIVER-4819 Allow reducing precision when convert... Backlog
is related to CDRIVER-3812 bson_as_json outputs illegal JSON for... Backlog
is related to CDRIVER-2063 JSON export prints insignificant digi... Closed

 Description   

The bson_as_relaxed_extended_json() is not showing correctly doubles. In particular, if I had:

 

doc = bson_new ();
bson_oid_init (&oid, NULL);
BSON_APPEND_OID (doc, "_id", &oid);
BSON_APPEND_DOUBLE (doc, "d1", -3.691944);
BSON_APPEND_DOUBLE (doc, "d2", 40.418889);
char* bsonStr = bson_as_relaxed_extended_json(doc, NULL);
fprintf(stdout, "%s\n", bsonStr);
bson_t *opt = BCON_NEW("validate", BCON_BOOL(false));
if (!mongoc_collection_insert_one (
  collection, doc, opt, NULL, &error)) {
  fprintf (stderr, "ERROR: %s\n", error.message);
}

 

(I have included only the relevant part of my program for the sake of briefness, but of course, the full code is available if you need it) 

What I see in my output is:

 

{ "_id" : { "$oid" : "605deb4b0547463a2521fcd2" }, "d1" : -3.6919439999999998925, "d2" : 40.418889000000000067 }

instead of (i.e. my expectation):

{ "_id" : { "$oid" : "605deb4b0547463a2521fcd2" }, "d1" : -3.691944, "d2" : 40.418889 }

However, in the DB the data is inserted correctly:

 

> db.mycoll.find()
{ "_id" : ObjectId("605dea3e23a3171905115392"), "d1" : -3.691944, "d2" : 40.418889 }

Not sure if other functions that also render JSON strings have the same problem. I have checked only bson_as_relaxed_extended_json().

Maybe I'm doing something wrong?

Thanks!



 Comments   
Comment by Roberto Sanchez [ 27/Apr/21 ]

The behavior described in this ticket is really a variation of the behavior already described in CDRIVER-3812. As that ticket has a some investigation and discussion already associated with it, as well as a reproducer, I am closing this ticket as a duplicate. The work will be completed under CDRIVER-3812.

Comment by Kevin Albertson [ 27/Mar/21 ]

Hi fermin.galanmarquez@telefonica.com, thank you for the report! We will look into this soon.

At a glance, CDRIVER-2063 may provide some additional background. The double-to-string conversion is done with the %.20g format, which was used to conform to our common driver standards in the Extended JSON specification.

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