use truediv for /, floordiv for // regarding numeric results
See original GitHub issueI 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:
- Created 4 years ago
- Comments:18 (11 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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