Hi,
one of the things you learn in your computing science classes: Never compare 2 floating point numbers directly! Always use some kind of delta.
Recently I came into the situation: I had to filter for a floating point number. I searched the DOCs for any special treatment here, but found nothing. Therefore I simply used:
Unit.objects.filter(transformation_factor=1.0).all()
This worked in my case. But now i’m wondering. How precise is this? Would Django return a unit object that had a trandformation factor of: 1.00000000000000000000000000000000000001 ?
The answer here is that Django delegates the comparison to the Python adapter and database so a queryset of the form you provided results in the following SQL being sent over the wire
"SELECT * FROM unit WHERE transformation_factor = ?", (1.0,)
and from there it depends on how your database adapter turns 1.0
into bytes based on the protocol it uses to communicate with the database and how your database implements float aritmetics.
For example, it seems that Postgres follows IEEE Standard 754 for Binary Floating-Point Arithmetic and that psycopg
adapt values by using their numerical representation which I assume is derived from float.__repr__
?