32- or 64-bits float division with PrestoSQL
Asked Answered
P

2

5

In Presto SQL, SELECT 1 / 3 returns 0, because / performs integer division.

But SELECT 1.0 / 3 returns 0.3... How can I get 0.3333333333? (i.e., more specifically, 32- or 64-bits precision instead of first decimal truncation?)

Photosphere answered 13/11, 2019 at 10:58 Comment(0)
M
7

You can cast() before dividing.

To get a 64-bit precision:

select cast(1 as double) / 3

To get a 32-bit precision:

select cast(1 as real) / 3
Maseru answered 13/11, 2019 at 11:0 Comment(0)
G
2

I suppose the literal 1.0 is treated as DECIMAL(2, 1). You could use floating point literals instead:

SELECT REAL '1'   / 3 -- '1' is a 32-bit float
SELECT DOUBLE '1' / 3 -- '1' is a 64-bit float
SELECT 1e0        / 3 -- scientific notation implies 64-bit float
Gorgonzola answered 13/11, 2019 at 11:0 Comment(2)
I realized we don't currently document the literals, so here's the PR: github.com/prestosql/presto/pull/2013Irresolvable
@piotr that is great. Revised the answer based on the docs (earlier it was just a guess).Gorgonzola

© 2022 - 2024 — McMap. All rights reserved.