Search code examples
sqlnumbersprestotrino

32- or 64-bits float division with PrestoSQL


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?)


Solution

  • 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