question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

use truediv for /, floordiv for // regarding numeric results

See original GitHub issue

I have the following example:

from sqlalchemy import Column, Integer, MetaData, Table, select

metadata = MetaData()
test = Table("test", metadata, Column("id", Integer))
query = select(columns=[(test.c.id / 2).label("half_id")])
print("the type of the 'half_id' column is", query.c.half_id.type)

query = select(columns=[(test.c.id / 2.0).label("half_id")])
print("the type of the 'half_id' column is", query.c.half_id.type)

This prints INTEGER and FLOAT. The latter is correct, but the former is incorrect.

Dividing 1 by 2 via / gives me 0.5 in MariaDB and Oracle and 0 in Postgres and MSSQL.

I was expecting column type derivation to follow the Python 3 rules in this case: / is a floating point division.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
zzzeekcommented, Dec 8, 2021

2.0 will resolve the issue completely including that it will render CAST / FLOOR on individual dialects as appropriate so that both / and // act as closely to Python as possible.

0reactions
sqla-testercommented, Dec 8, 2021

Mike Bayer has proposed a fix for this issue in the main branch:

consider truediv as truediv; support floordiv operator https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3355

Read more comments on GitHub >

github_iconTop Results From Across the Web

Choosing between true division and floor division
We can do division using Fraction objects and integers. This forces the result to be a mathematically exact rational number: Create at least...
Read more >
Pandas Series: truediv() function - w3resource
The truediv() function is used to get floating division of series and other, element-wise (binary operator truediv).
Read more >
True Division vs Floor Division in Python - Prospero Coder
Today we'll be talking about the difference between true division and floor division in Python. There are two kinds of division operators:.
Read more >
What is `1..__truediv__` ? Does Python have a .. ("dot dot ...
So the string 1. is interpreted as number, the second . is an OP (an operator, in this case the "get attribute" operator)...
Read more >
PEP 238 – Changing the Division Operator
Thus, the author of a numerical routine may only use floating point numbers to ... __div__(), __floordiv__(), __truediv__(); __idiv__(), __ifloordiv__(), ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found