1

I am doing some calculations using a sqlite database. But the behavior is inconsitent. In my local machine, select 10.95 - 2.95 results in 8.0..

> sqlite3
> select 10.95 - 2.95
8.0

But using the online sqlite "https://sqliteonline.com/", select 10.95- 2.95 results in 7.999999999999999. This is the weird behaviour

This weird behaviour also using the python library "https://docs.python.org/3.5/library/sqlite3.html"

>>> from sqlite3 import connect
>>> con = connect(":memory:")
>>> c = con.cursor)
>>> t = c.execute("select 10.95 - 2.95")
>>> t.fetchone()
(7.999999999999999,)
  • Possible duplicate of [Why are floating point numbers inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate) – Tim Biegeleisen Jul 15 '19 at 04:50
  • Floating point arithmetic is not exact, not in SQLite, not in Python. – Tim Biegeleisen Jul 15 '19 at 04:50
  • @TimBiegeleisen, why do you think its correct in my local machine but not in the python implementation db-api ? – Kshitiz Shakya Jul 15 '19 at 05:13
  • Easy: Floating point arithmetic is not exact, so slight differences may appear depending on where you run it, with what tool, version, etc. – Tim Biegeleisen Jul 15 '19 at 05:14
  • 1
    The sqlite3 shell displays floating point numbers in such a way it rounds them; python and that other tool don't. The 7.999999999 is the more accurate result – Shawn Jul 15 '19 at 05:45

0 Answers0