[CDRIVER-2612] bson_as_json prints insignificant digits for doubles Created: 13/Apr/18  Updated: 26/Apr/18  Resolved: 26/Apr/18

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

Type: Bug Priority: Minor - P4
Reporter: Jeroen Ooms [X] Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Unfortunately a new number formatting similar to issue: https://jira.mongodb.org/browse/CDRIVER-652. The bson_as_json function prints many insignificant digits for doubles:

> con <- mongo()
> con$insert('{"test": 3.9}')
> con$export()
{ "_id" : { "$oid" : "5ad088a5ba7cb20ff97e6a54" }, "test" : 3.8999999999999999112 }

OTOH, the mongo command line utility prints the correct number of digits for the same data:

> db.test.find()
{ "_id" : ObjectId("5ad088a5ba7cb20ff97e6a54"), "test" : 3.9 }



 Comments   
Comment by A. Jesse Jiryu Davis [ 13/Apr/18 ]

Go for it!

Comment by Jeroen Ooms [X] [ 13/Apr/18 ]

OK. Do you mind if I fix it in my copy of the vendored libmongoc code? The incorrect digits at the end are a bit unfortunate for us.

Comment by A. Jesse Jiryu Davis [ 13/Apr/18 ]

Sorry, Jeroen, I'm not going to change this again.

Comment by Jeroen Ooms [X] [ 13/Apr/18 ]

It's not a blocker but it's not optimal. The example above shows that you are printing noise at the end, which is incorrect and makes the json payload larger than needed. Perhaps we can make the "%.20g" format a macro so that I can set it back to %.15g for my client? The command line util seems to do the correct thing though.

Comment by A. Jesse Jiryu Davis [ 13/Apr/18 ]

Thanks Jeroen. The C Driver formats doubles with "%.20g", which was updated from "%.15g" some time after CDRIVER-652. If I remember correctly, that was "%g" at some point but that caused a different problem. Can you tell me why this is important? I'd like to stop changing the formatting for doubles if it's basically correct. If it's truly incorrect, could you find out what we could do to act identically to mongoexport?

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