[CXX-1753] bsoncxx:to_json causes double value to truncate Created: 29/Mar/19  Updated: 27/Oct/23  Resolved: 02/Apr/19

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

Type: Task Priority: Major - P3
Reporter: Andrew Skripnikov Assignee: Unassigned
Resolution: Gone away Votes: 0
Labels: bson
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

ubuntu.16-04



 Description   

When I'm using bsoncxx::to_json function to convert prepared bsoncxx document to json all float/double values are truncated to 5 digits precision. Please check following code:

 

#include <iostream>
#include <bsoncxx/json.hpp>
 #include <bsoncxx/builder/basic/array.hpp>
 #include <bsoncxx/builder/stream/document.hpp>
 #include <bsoncxx/builder/basic/document.hpp>
using namespace bsoncxx;
using namespace std;
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::document;
int main(void) {
  bsoncxx::builder::stream::document d1;
  bsoncxx::builder::basic::document d2;
  cout.precision(17);
  double v1=1.0123456789;
  double v2=9.8765432111;
  d1<<"T1"<<v1;
  cout<<bsoncxx::to_json(d1.view())<<endl;
 d2.append(kvp("T2", types::b_double{v2}));
 cout<<bsoncxx::to_json(d2.view())<<endl;
  cout<<"V1: "<<v1<<endl;
  cout<<"V2: "<<v2<<endl;
  cout<<"T1: "<<d1.view()["T1"].get_double()<<endl;
  cout<<"T2: "<<d2.view()["T2"].get_double()<<endl;
}

Sample output:

 

{
 "T1" : 1.01235
 }
 {
 "T2" : 9.87654
 }
V1: 1.0123456789
 V2: 9.8765432110999996
 T1: 1.0123456789
 T2: 9.8765432110999996

I'm expecting following output from to_json call:

 

{
 "T1" : 1.0123456789
 }
{
 "T2" : 9.8765432110999996
 }

How to fix this issue?

libbsoncxx -> 3.0.1 libbson -> 1.9.2

Thank you!



 Comments   
Comment by Kevin Albertson [ 02/Apr/19 ]

screep glad to hear! Closing.

Comment by Andrew Skripnikov [ 02/Apr/19 ]

No. After upgrade to 3.4.0 everything is fine. Thank you!

 

{ "T1" : 1.0123456789000000455 } { "T2" : 9.876543211099999553 }

V1: 1.0123456789
V2: 9.8765432110999996
T1: 1.0123456789
T2: 9.8765432110999996

 

 

Comment by Kevin Albertson [ 01/Apr/19 ]

Hi screep, thanks for reporting this. If possible, are you able to reproduce this on the latest released C++ driver, 3.4.0?

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