2

I wrote on Linux PC python3 in eric the following code:

import sqlite3
conn = sqlite3.connect("dbug26.db")
c=conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS prices (price1 REAL,price2 REAL, 
price3 REAL, price4 REAL, price5 REAL)")
c.execute("INSERT INTO prices VALUES (0.01, 0.01, 2.60, 0.01, 2.60)")
conn.commit()

c.execute("SELECT * FROM price")
prices = c.fetchall()
print (prices)

c.close()
conn.close()

If I run the code in python3 in Linux then the output is:

>>> [(2.6,), (1.0,), (2.6,), (1.01,)]

but if I run exactly the same code in qpython3 on Android then the output is:

>>> [(2.6000000000000001,), (1.0, ), ((2.6000000000000001,), (1.01, )]

Where do the extra decimal zeros and one in qpython3 come from? What can I do to prevent this ?

If I look at the sqlite db in several db-browsers on Linux,Mac and Android the data always appears correctly.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Wim Heylen
  • 21
  • 1
  • The actual value is 2.6000000000000000888178419700125232338905334472656250, but Python is not showing that many digits. Floating point can't accurately model 2.6. – Martijn Pieters Aug 18 '17 at 17:46
  • QPython is basically a port of Python 3.2, see the [`qpython3-core` repository](https://github.com/qpython-android/qpython3-core/tree/master/python3-src). The behaviour you see is correct for that Python release. Later Python 3 releases have updated the float representation to hide the floating point details. – Martijn Pieters Aug 18 '17 at 17:55
  • Why is it accurate then in the python3 code in Linux,Mac and not in qpython3 on Android? And what can I do to make the data consistant? – Wim Heylen Aug 18 '17 at 17:58
  • It is accurate in both places. All that is different is the printed representation, how the value is converted to a string when you call `repr()` on it. The *values are still the same*. – Martijn Pieters Aug 18 '17 at 17:58
  • Thank you for the correct and quick response, I understand what is going on now. I'll have to rewrite my code before I launch my shuttle to the Gallifrey in to the Kasterbouros constellation, or I miss the landing bay by miles ;-) . – Wim Heylen Aug 18 '17 at 18:51

0 Answers0