Question about precision in Elastic Search, Need to take the values till 18 to 20 decimals

Hi,

I have been using Elastic Search V 6.6.4 for my project from last 6 months or so. However, after completing the product when we are doing the verification of the values , we can see there is a mismatch in values after 15 decimal places. There are some values where we need the values to be saved till 18 or 20 decimal places. We checked that Elastic search is automatically rounding off the values to 15 decimals or so.

We are looking for some immediate answers that is it a functionality or a known bug to ES team or is it already resolved in some previous releases and then we need some help on priority basis to resolve this.

Hope to get a reply asap.

Vishal Gupta
Bengaluru, India

It may be a limitation in Elasticsearch, but it is also possible that this is a limitation in JSON and how floating point values are encoded/decoded. It may therefore depend on which client library/language you use, so some additional information about this could be useful.

We are using C#.Net. Let me know which other information is required?

The value in the document source is returned to clients exactly as it was received (unless you are using source filtering). However if you are mapping the value as a numeric datatype then, as documented, it will be treated as an IEEE 754 floating point number during searches.

When you say you are "doing the verification of the values", what do you mean?

Thanks David. In my case Value is a double data type field. Could you tell me what is the max number of decimal digits a double data type can store in ES? Also, if it is 15 then which one i should use so that it does not miss the last few decimals?

As per the manual that I linked above, Elasticsearch stores doubles as 64-bit IEEE 754 floating point numbers, giving 53 binary digits of precision, which is roughly 15 decimal places. But I still don't understand what "doing the verification of the values" is: Elasticsearch does the same thing as C# here, so I suspect that your verification process might not be doing the right thing.

In our application, we are storing the account balance of an account holder in Values variable of double data type. We checked that for one of the account holder, C# is sending the account balance similar to -9.909909909118561064 but after saving it into ES, we are getting it as -9.909909909118561... last 3 decimal values are not getting fetched.

Let me know what alternative i can use to avoid this.

You cannot store numbers to this precision in a double variable in C#:

		double v1 = -9.909909909118561064;
		double v2 = -9.909909909118561;
		Console.WriteLine(String.Format("{0} {0:r} {1} {1:r} {2}", v1, v2, v1 == v2));

The output was:

-9.90990990911856 -9.90990990911856 -9.90990990911856 -9.90990990911856 True

I cannot reproduce the problem you are experiencing with the small amount of detail you've given. You will need to describe this "verification" process in much more detail.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.