Skip to content

Instantly share code, notes, and snippets.

@neomantra
Created June 13, 2017 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neomantra/a3ae147b5040d63daf1a606080cfa97c to your computer and use it in GitHub Desktop.
Save neomantra/a3ae147b5040d63daf1a606080cfa97c to your computer and use it in GitHub Desktop.
BSON Float test
/// cc -o bson_float_test bson_float_test.c -lbson-1.0.0
#include <stdio.h>
#include <libbson-1.0/bson.h>
int main(int argc, const char* argv[])
{
bson_t b;
bson_init(&b);
double dvalue = 0.01;
printf("double: %0.20f\n", dvalue);
bson_append_double(&b, "double", -1, dvalue);
float fvalue = 0.01f;
printf("float: %0.20f\n", fvalue);
bson_append_double(&b, "float_cast", -1, (double)fvalue);
char* json = bson_as_json(&b, NULL);
printf("%s\n", json);
bson_free(json);
return 0;
}
@neomantra
Copy link
Author

Result:

$ ./bson_float_test

double: 0.01000000000000000021

float:  0.00999999977648258209

{ "double" : 0.010000000000000000208, "float_cast" : 0.0099999997764825820923 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment