Skip to content
Advertisement

How do I select floating-point infinity literals from a SQLite database?

I have a SQLite database with a table named measurements, which contains some experimental data measured by a power meter. They’re stored as data type REAL in a field named power_dbm. In some measurements, the readings can be off the scale, the meter cannot obtain a reading due to overrange or underrange, thus the values +Inf and -Inf are used to represent these values.

For example, here is some of the data.

As you see, there’s a floating-point number of negative infinity. Unfortunate I don’t know the correct syntax to select these values from the database. I tried:

What is the correct syntax to specify the literal value of positive or negative infinity in SQL/SQLite? Any additional references to relevant documentation will also greatly be appreciated.

Advertisement

Answer

Solution 1: Use an impossibly-huge floating-point literal

I found writing an impossibly-huge floating-point literal such as 9e999 is one way to construct +Inf/-Inf.

Solution 2: Use ieee754() with hardcoded exponent/significand for Infinity

You can construct +Inf and -Inf manually from an exponent and significand in the IEEE 754 representation via function ieee754(). This is the original solution I used before I found the 9e999 trick…

The detailed usage of ieee754() and other related functions is documented in Floating Point Numbers.

Solution 3: Use Parameterized SQL queries

Using ieee754(-4503599627370496, 972) feels like a more rigorous construction than a random value like 9e999, but 9e999 is adequate for typing commands in a console by hand when all you want is having a quick look of the data, so correctness is not a real concern.

In an actual program, one would be using parameterized SQL queries instead of handling strings anyway, simply passing the native Infinity constant (e.g. Python’s math.inf) in your programming language is sufficient.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement