2023-10-17 23:36:14 +08:00
|
|
|
[discrete]
|
2023-05-11 23:02:27 +08:00
|
|
|
[[esql-pow]]
|
|
|
|
=== `POW`
|
2023-08-25 21:07:25 +08:00
|
|
|
[.text-center]
|
2023-08-27 02:19:47 +08:00
|
|
|
image::esql/functions/signature/pow.svg[Embedded,opts=inline]
|
2023-08-25 21:07:25 +08:00
|
|
|
|
2023-07-15 01:06:12 +08:00
|
|
|
Returns the value of a base (first argument) raised to the power of an exponent (second argument).
|
|
|
|
Both arguments must be numeric.
|
2023-05-11 23:02:27 +08:00
|
|
|
|
2023-07-07 21:45:06 +08:00
|
|
|
[source.merge.styled,esql]
|
2023-05-11 23:02:27 +08:00
|
|
|
----
|
2023-07-15 01:06:12 +08:00
|
|
|
include::{esql-specs}/math.csv-spec[tag=powDI]
|
2023-05-11 23:02:27 +08:00
|
|
|
----
|
2023-07-07 21:45:06 +08:00
|
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|
|
|===
|
2023-07-15 01:06:12 +08:00
|
|
|
include::{esql-specs}/math.csv-spec[tag=powDI-result]
|
|
|
|
|===
|
|
|
|
|
2023-10-17 23:36:14 +08:00
|
|
|
[discrete]
|
2023-07-21 01:03:29 +08:00
|
|
|
==== Type rules
|
|
|
|
|
2023-07-15 01:06:12 +08:00
|
|
|
The type of the returned value is determined by the types of the base and exponent.
|
|
|
|
The following rules are applied to determine the result type:
|
|
|
|
|
|
|
|
* If either of the base or exponent are of a floating point type, the result will be a double
|
2023-08-17 23:29:06 +08:00
|
|
|
* Otherwise, if either the base or the exponent are 64-bit (long or unsigned long), the result will be a long
|
|
|
|
* Otherwise, the result will be a 32-bit integer (this covers all other numeric types, including int, short and byte)
|
2023-07-15 01:06:12 +08:00
|
|
|
|
|
|
|
For example, using simple integers as arguments will lead to an integer result:
|
|
|
|
|
|
|
|
[source.merge.styled,esql]
|
|
|
|
----
|
|
|
|
include::{esql-specs}/math.csv-spec[tag=powII]
|
|
|
|
----
|
|
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|
|
|===
|
|
|
|
include::{esql-specs}/math.csv-spec[tag=powII-result]
|
|
|
|
|===
|
|
|
|
|
2023-08-17 23:29:06 +08:00
|
|
|
NOTE: The actual power function is performed using double precision values for all cases.
|
|
|
|
This means that for very large non-floating point values there is a small chance that the
|
|
|
|
operation can lead to slightly different answers than expected.
|
2023-07-21 01:03:29 +08:00
|
|
|
However, a more likely outcome of very large non-floating point values is numerical overflow.
|
|
|
|
|
2023-10-17 23:36:14 +08:00
|
|
|
[discrete]
|
2023-07-21 01:03:29 +08:00
|
|
|
==== Arithmetic errors
|
|
|
|
|
2023-08-17 23:29:06 +08:00
|
|
|
Arithmetic errors and numeric overflow do not result in an error. Instead, the result will be `null`
|
2023-07-21 01:03:29 +08:00
|
|
|
and a warning for the `ArithmeticException` added.
|
|
|
|
For example:
|
2023-07-15 01:06:12 +08:00
|
|
|
|
|
|
|
[source.merge.styled,esql]
|
|
|
|
----
|
|
|
|
include::{esql-specs}/math.csv-spec[tag=powULOverrun]
|
|
|
|
----
|
|
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|
|
|===
|
2023-07-21 01:03:29 +08:00
|
|
|
include::{esql-specs}/math.csv-spec[tag=powULOverrun-warning]
|
|
|
|
|===
|
|
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|
|
|===
|
2023-07-15 01:06:12 +08:00
|
|
|
include::{esql-specs}/math.csv-spec[tag=powULOverrun-result]
|
|
|
|
|===
|
|
|
|
|
2023-09-11 22:20:10 +08:00
|
|
|
If it is desired to protect against numerical overruns, use `TO_DOUBLE` on either of the arguments:
|
2023-07-15 01:06:12 +08:00
|
|
|
|
|
|
|
[source.merge.styled,esql]
|
|
|
|
----
|
|
|
|
include::{esql-specs}/math.csv-spec[tag=pow2d]
|
|
|
|
----
|
|
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|
|
|===
|
|
|
|
include::{esql-specs}/math.csv-spec[tag=pow2d-result]
|
|
|
|
|===
|
|
|
|
|
2023-10-17 23:36:14 +08:00
|
|
|
[discrete]
|
2023-07-21 01:03:29 +08:00
|
|
|
==== Fractional exponents
|
|
|
|
|
|
|
|
The exponent can be a fraction, which is similar to performing a root.
|
|
|
|
For example, the exponent of `0.5` will give the square root of the base:
|
|
|
|
|
|
|
|
[source.merge.styled,esql]
|
|
|
|
----
|
|
|
|
include::{esql-specs}/math.csv-spec[tag=powID-sqrt]
|
|
|
|
----
|
|
|
|
[%header.monospaced.styled,format=dsv,separator=|]
|
|
|
|
|===
|
|
|
|
include::{esql-specs}/math.csv-spec[tag=powID-sqrt-result]
|
|
|
|
|===
|
|
|
|
|
2023-10-17 23:36:14 +08:00
|
|
|
[discrete]
|
2023-07-21 01:03:29 +08:00
|
|
|
==== Table of supported input and output types
|
|
|
|
|
2023-07-15 01:06:12 +08:00
|
|
|
For clarity, the following table describes the output result type for all combinations of numeric input types:
|
|
|
|
|
2023-08-25 21:07:25 +08:00
|
|
|
include::types/pow.asciidoc[]
|