Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between NUMERIC and FLOAT in BigQuery?

I read the docs: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types https://cloud.google.com/bigquery/pricing#data

I know that FLOAT is 8 bytes while NUMERIC is 16 bytes Is that the only difference? The docs says that NUMERIC can range -99999999999999999999999999999.999999999 to 99999999999999999999999999999.999999999 but it doesn't specify the range for FLOAT.

like image 266
Luis Avatar asked Feb 20 '19 15:02

Luis


People also ask

What is the difference between float and numeric?

Float is Approximate-number data type, which means that not all values in the data type range can be represented exactly. Decimal/Numeric is Fixed-Precision data type, which means that all the values in the data type range can be represented exactly with precision and scale.

What is float in BigQuery?

Float (Float 64): Numbers with approximate numeric values and fractional components. Numeric: There is a data type called 'NUMERIC' which is similar to 'Decimal' which can store values with 38 decimal digits of precision and nine decimal digits of scale. Suitable for exact calculations.

Is Numeric in BigQuery?

BigQuery supports casting to NUMERIC.


3 Answers

I like the current answers. I want to add this as a proof of why NUMERIC is necessary:

SELECT 
  4.35 * 100 a_float
  , CAST(4.35 AS NUMERIC) * 100 a_numeric

enter image description here

This is not a bug - this is exactly how the IEEE defines floats should be handled. Meanwhile NUMERIC exhibits behavior closer to what humans expect.

For another proof of NUMERIC usefulness, this answer shows how NUMERIC can handle numbers too big for JavaScript to normally handle.

Before you blame BigQuery for this problem, you can check that most other programming languages will do the same. Python, for example:

enter image description here

like image 195
Felipe Hoffa Avatar answered Sep 22 '22 05:09

Felipe Hoffa


There are quite a few differences:

  • Range: FLOAT can be get as big as ±2^1023 (although it can't represent every integer in this range)

  • Precision near zero: near zero, FLOAT can be as precise as 2^-1022.

  • NaN/Inf: FLOAT has "not a number" and positive and negative "infinite" values, while NUMERIC does not.

  • Storage size: each NUMERIC requires 16 bytes storage, but FLOAT only requires 8 bytes.

  • Unpredictable nature: As Ajay hints at, the big drawback to FLOAT is that the numbers that it can precisely represent don't really align that closely with the numbers we are typically interested in. As a result, floating point should be used for physical quantities where some error is acceptable. If you require predictable, exact human-predictable results, like in financial calculations, use NUMERIC

like image 20
David Avatar answered Sep 22 '22 05:09

David


The main difference is Floats / Doubles are binary floating point types and a Numeric will store the value as a floating decimal point type. So Numeric have much higher precision and are usually used within monetary (financial) applications that require a high degree of accuracy. But in performance wise Numeric are slower than double and float types.Numeric can 100% accurately represent any number within the precision of the decimal format, whereas Float and Double, cannot accurately represent all numbers, even numbers that are within their respective formats precision.

like image 39
Ajay Kharade Avatar answered Sep 26 '22 05:09

Ajay Kharade