Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert float casted to double add noise to field #210

Open
Tracked by #181
Globostofo opened this issue Sep 13, 2023 · 3 comments
Open
Tracked by #181

Insert float casted to double add noise to field #210

Globostofo opened this issue Sep 13, 2023 · 3 comments

Comments

@Globostofo
Copy link

Globostofo commented Sep 13, 2023

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).

@offa offa added the question label Sep 13, 2023
@offa
Copy link
Owner

offa commented Sep 13, 2023

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.

@Globostofo
Copy link
Author

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)

@offa
Copy link
Owner

offa commented Sep 13, 2023

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 👍.

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

No branches or pull requests

2 participants