Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-3938

Rounding errors in double type in bson_as_relaxed_extended_json() function

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 1.17.4
    • Component/s: libbson
    • Labels:
      None

      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!

            Assignee:
            roberto.sanchez@mongodb.com Roberto Sanchez
            Reporter:
            fermin.galanmarquez@telefonica.com Fermín Galán
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: