#community-help

Resolving Floating Point Arithmetic in Facets

TLDR Claudiu reported an issue with floating point value representation in facets and found a workaround by converting everything to int32. Kishore Nallan validated this fix and also committed to finding a solution. Later, Erick faced a related error.

Powered by Struct AI

2

1

1

30
21mo
Solved
Join the chat
May 19, 2021 (31 months ago)
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
07:40 PM
does anybody know about this issue?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:18 PM
Ah good ol' floating point arithmetic. Could you open a github issue about this Claudiu?
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
08:22 PM
hey, i will tomorrow, in the document the value looks ok in the hits, just on the facets value is the problem
May 21, 2021 (30 months ago)
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:03 AM
Claudiu I'm not able to reproduce this issue. I indexed a single document with a facet field value of 113.4 and facet on the same field during searching. See the screenshot: the response looks fine.
10:03
Kishore Nallan
10:03 AM
If you can share a small snippet that reproduces this issue, that will help us narrow down the issue.
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
12:09 PM
hey Kishore Nallan but in the document the value of the field "points" is 113.4 and in the facet is returned as 113.400002. Why is this happening?
12:11
Claudiu
12:11 PM
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:16 PM
Claudiu That's because many floating point numbers cannot be represented exactly, so when they are serialized they might have some trailing decimal places at the end. In general, for facets it is better to store price in cents (as integer) instead of a float.
12:17
Kishore Nallan
12:17 PM
In your original post though, you said that 113.4 is changed to 114.300003 -> such a large difference should not happen. Was that a typo?
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
12:31 PM
look at the CB field 63.3 is returned in facet like 63.299999
12:33
Claudiu
12:33 PM
the thing is i have to do a filter in the sidebar, and i need the exact value
12:33
Claudiu
12:33 PM
because i will filter by that value
12:33
Claudiu
12:33 PM
a workaround is to convert everything to int32
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:33 PM
Yes, it's the same issue. When you convert a number from a floating point representation to string, this can happen unless we know how many decimal places the original precision of the float was. In this case, the best way to work around this issue is to store the float as an integer in cents.

1

Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
12:34 PM
thanks
12:34
Claudiu
12:34 PM
like i guessed
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:35 PM
In the mean time, I will see if there is anything we can do here. It's a notoriously difficult problem because of the way a float is represented.
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
12:36 PM
i understand
12:36
Claudiu
12:36 PM
i will user. integer
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:36 PM
๐Ÿ‘
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
12:37 PM
you are doing a good job

1

Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:28 PM
Claudiu Found a way to fix this. I can cut you a RC build where this is addressed, if you would like.
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
01:28 PM
uh
01:28
Claudiu
01:28 PM
i already used integer
01:28
Claudiu
01:28 PM
but i can test it
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:28 PM
No worries, then! Just wanted to check ๐Ÿ™‚
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
01:29 PM
you guys are so fast ๐Ÿ˜„
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:29 PM
๐Ÿ˜„
Claudiu
Photo of md5-d619094f9c50a3e0a4927dc0e63ba30d
Claudiu
01:29 PM
i hope this community is getting bigger and bigger

2

Feb 01, 2022 (21 months ago)
Erick
Photo of md5-5d002d197dc556e6bc619deaa3a8aba7
Erick
07:06 PM
hey Kishore Nallan, I'm getting the error 400: {"message": "Field cost should be a string or a string array."} . The actual value is float. What do you think is the issue here?