bson-append_double with double precision

290 Views Asked by At

I got a problem with function bson_append_double below:

dvalue = 0.01;
bson_append_double(pbson, field_name, -1,(double)dvalue);

but the result i got is annoying long: 0.0099999997764825820923 Did anyone faced this problem before, please help me. Thanks and Regards

1

There are 1 best solutions below

0
On

this is simply a float-to-double conversion. If you don't want to lose the precision, you'll need to declare the variable as a double in the first place.

somewhere in your code you have a line

float dvalue;

if you are able to promote to type double you should consider changing the declaration to:

double dvalue;

here is a previous question on loss of precision in conversions.

Precision loss from float to double, and from double to float?