4

I'm sending data back and forth Python and Cassandra. I'm using both builtin float types in my python program and the data type for my Cassandra table. If I send a number 955.99 from python to Cassandra, in the database it shows 955.989999. When I send a query in python to return the value I just sent, it is now 955.989990234375.

I understand the issue with precision loss in python, I just wanted to know if there's any built-in mechanisms in Cassandra that could prevent this issue.

user2361174
  • 1,872
  • 4
  • 33
  • 51

2 Answers2

6

Python float is an 64-bit IEEE-754 double precision binary floating point number. Use double in Cassandra for a matching type.

  • While that works for some of the values, some like `8.83` are turning into `null` despite it have the same types like the other values, any idea why this is happening? – user2361174 Jul 17 '17 at 09:05
  • Ahh it wasn't the values themselves that were an issue. I used `DROP` to remove the float columns and used `ADD` to put them back. The columns that I didn't name into something else had the null values in them. – user2361174 Jul 17 '17 at 10:18
  • Keep in mind that floatingpoint values like float or double always have issues with precision. – Mandraenke Jul 17 '17 at 10:25
  • Great point Mandraenke. If precision is important, the cql decimal type should be used. – Andy Tolbert Jul 17 '17 at 14:22
1

Also if you cannot change your column definition for some reason, converting your float value to string and passing str to the cassandra-driver will also solve your problem.

It will be able to generate the precise decimal values form str.

Andrea Nagy
  • 1,201
  • 9
  • 21