BigQuery · Math function
BigQuery POW / POWER
POW(x, y) raises x to the power y, and POWER is a synonym: POW(2, 10) returns 1024.0. INT64 inputs return FLOAT64, NUMERIC inputs stay exact. Use it for growth rates, squares and unit conversions such as bytes / POW(1024, 4). The ^ operator is bitwise XOR, not a power.
POW and POWER are the same function. With INT64 arguments the result is FLOAT64 (POW(2, 3) is 8.0); with NUMERIC or BIGNUMERIC it stays exact, which matters for interest and growth calculations: POW(NUMERIC '1.05', 3) is exactly 1.157625.
BigQuery has no exponent operator. 2 ^ 3 is valid SQL but returns 1, because ^ is bitwise XOR, so queries ported from PostgreSQL silently produce wrong numbers. 2 ** 3 fails with a syntax error.
POW fails with Floating point error in function when the result overflows (POW(10, 400)), when a negative base gets a fractional exponent (POW(-8, 1/3)), or when 0 is raised to a negative power. SAFE.POW returns NULL in those cases. For square roots, SQRT reads better than POW(x, 0.5).
Syntax
POW(X, Y)
POWER(X, Y)Arguments and return value
| Argument | Description |
|---|---|
X | The base: INT64, NUMERIC, BIGNUMERIC or FLOAT64. |
Y | The exponent, same types. A fractional exponent needs a base of 0 or more. |
Returns: FLOAT64 when either argument is FLOAT64 or both are INT64. Otherwise NUMERIC, or BIGNUMERIC when either argument is BIGNUMERIC.
Examples
Powers with INT64 and NUMERIC inputs
SELECT
POW(2, 10) AS two_to_the_10th,
POWER(10, -2) AS hundredth,
POW(NUMERIC '1.05', 2) AS numeric_power;| two_to_the_10th | hundredth | numeric_power |
|---|---|---|
| 1024.0 | 0.01 | 1.1025 |
Compound growth with exact decimals
1000 at 5% a year for three years. NUMERIC keeps the calculation exact until the final ROUND.
SELECT
ROUND(NUMERIC '1000' * POW(NUMERIC '1.05', 3), 2) AS balance_after_3_years;| balance_after_3_years |
|---|
| 1157.63 |
Convert bytes billed to TiB
On-demand queries are billed per TiB, which is 2^40 bytes; see BigQuery pricing for the rate.
WITH jobs AS (
SELECT 'daily_rollup' AS job, 2199023255552 AS total_bytes_billed UNION ALL
SELECT 'adhoc_export', 274877906944
)
SELECT
job,
total_bytes_billed / POW(1024, 4) AS tib_billed
FROM jobs
ORDER BY job;| job | tib_billed |
|---|---|
| adhoc_export | 0.25 |
| daily_rollup | 2.0 |
^ is XOR, not a power
In binary, 2 is 010 and 3 is 011; XOR gives 001.
SELECT
2 ^ 3 AS bitwise_xor,
POW(2, 3) AS power;| bitwise_xor | power |
|---|---|
| 1 | 8.0 |
Common mistakes
- Using
^for exponents as in PostgreSQL. In BigQuery2 ^ 3is bitwise XOR and returns 1; usePOW(2, 3). - Expecting an INT64 result.
POW(2, 3)is 8.0; cast it if you need an integer, or multiply (x * x) to stay in INT64. - Taking roots of negative numbers with fractional exponents.
POW(-8, 1/3)fails with Floating point error in function; useCBRT(x)for cube roots orSIGN(x) * POW(ABS(x), 1/3). - Doing money math in FLOAT64. Compounding with FLOAT64 accumulates binary rounding errors; use NUMERIC for rates and balances.
FAQ
What is the difference between POW and POWER in BigQuery?
None. POWER is a synonym for POW, with the same arguments and return types.
How do I square a number in BigQuery?
POW(x, 2) returns FLOAT64 for INT64 input. x * x keeps INT64 but overflows once x passes about 3 billion.
Is there an exponent operator in BigQuery?
No. ^ is bitwise XOR and ** is a syntax error. Use POW or POWER, or EXP for powers of e.
Related
Errors you may hit
More mathematical functions
All BigQuery mathematical functions · All functions · BigQuery error messages
