Insert float casted to double add noise to field
My goal is to insert my float variable in the database.
But when I call the addField method and cast the float into double, the variable gets a weird noise (little supplement) at the end:
This code:
float value = 3.141592f
influxdb::Point p1{"cpu"};
p1.addField("value", (double) value);
std::cout << p1.getFields() << std::endl;
displays this output:
value=3.141592025756835938
As you can see, a few digits are added at the end of the value, not far from the last digit.
I also tried casting with static_cast<double>(value) but it didn't work (same if I pass the float as is, without casting).
Edit:
To sum up, I'd like to know if it's possible to have as much precision as if I inserted a double:
value=3.141592000000000162
NB: with such a value, InfluxDB ignores the 162 at the end, and give me the tuple when I do a query with WHERE value=3.141592 (which is not the case with the float result).
Converting from float to double uses the closest possible double value, thus may cause some garbage numbers.
The library uses double exclusive and for the line protocol a fixed precision. You can change the precision as needed (example). This could be way to tweak your values sent.
If I've understood what you're saying, you mean that I modify the precision of the floats before writing each float? However, the fixed precision is the same for all the values in a batch (as I understand it).
I want to insert the following floats in the same batch:
1.2345e-34f
3.141592f
555.5555f
I have to set the precision to about 40, and it will apply to every value. In this situation, I don't have any way of avoiding the garbage numbers?
Now that I think about it, wouldn't it be a good idea to support the float type in addition to the double type? It might allow users of the library not to have to worry about the type of decimal value they're passing, and it would also solve the problem of extra garbage numbers when converting from a float to a double.
In terms of implementation, it wouldn't be that complicated because the values are just written in line protocols. I think you just need to add the float value to the line as if it were a double
Inserting the float 3.141592f could become the line protocol:
cpu value=3.141592
(no difference compared to a double)
However, the fixed precision is the same for all the values in a batch (as I understand it).
Yes unfortunately.
In terms of implementation, it wouldn't be that complicated because the values are just written in line protocols. I think you just need to add the float value to the line as if it were a double
This might be a breaking change though.
But there are plans for some redesigns (#181) which might consider float support. I'll add it as a candidate.
Thanks for your suggestion anyway :+1:.