View Ticket
Not logged in
Ticket Hash: 8f59ddebf0dfcab9f8d25aca7cd6808fa50caf15
Title: Log(b, x) is actually returning the result of Log(x, b).
Status: Fixed Type: Code_Defect
Severity: Severe Priority: Immediate
Subsystem: Resolution: Fixed
Last Modified: 2018-03-28 15:39:54
Version Found In: 4.3.0
User Comments:
anonymous added on 2018-03-26 14:56:03:
As documented, we expect Log(b, x) but it seems as though it is actually Log(x, b).

Here is a simple example in Python 3:

>>> import math
>>> import sqlite3
>>> connection = sqlite3.connect(':memory:')
>>> connection.enable_load_extension(True)
>>> connection.load_extension('mod_spatialite')
>>> cursor = connection.cursor()

>>> cursor.execute('SELECT LOG(2, 4);')
<sqlite3.Cursor object at 0x7f00bf6573b0>
>>> cursor.fetchone()[0]
0.5
>>> cursor.execute('SELECT LOG(4) / LOG(2);')
<sqlite3.Cursor object at 0x7f00bf6573b0>
>>> cursor.fetchone()[0]
2.0
>>> cursor.execute('SELECT LOG(4, 2);')
<sqlite3.Cursor object at 0x7f00bf6573b0>
>>> cursor.fetchone()[0]
2.0
>>> math.log(4, 2)
2.0

Note the documentation for math.log(): https://docs.python.org/3/library/math.html#math.log

I have only tested this on v4.3.0, but I imagine the problem has existed for a long time.

All other database backends I have tested use LOG(B, X), but I think that for backwards compatibility the only solution here is to fix the documentation for the existing behaviour?

sandro added on 2018-03-28 15:39:54:
yes, I confirm; the two arguments are badly flipped by the C implementing code.

as far as I can remember this SQL function was initially introduced about ten years ago and never changed since then.

as you suggested, the most reasonable patch seems the one to just update the documentation leaving the code untouched.

fixed on 2018-03-28 by commit f4249ca317