pow.asciidoc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [[esql-pow]]
  2. === `POW`
  3. [.text-center]
  4. image::esql/functions/signature/pow.svg[Embedded,opts=inline]
  5. Returns the value of a base (first argument) raised to the power of an exponent (second argument).
  6. Both arguments must be numeric.
  7. [source.merge.styled,esql]
  8. ----
  9. include::{esql-specs}/math.csv-spec[tag=powDI]
  10. ----
  11. [%header.monospaced.styled,format=dsv,separator=|]
  12. |===
  13. include::{esql-specs}/math.csv-spec[tag=powDI-result]
  14. |===
  15. ==== Type rules
  16. The type of the returned value is determined by the types of the base and exponent.
  17. The following rules are applied to determine the result type:
  18. * If either of the base or exponent are of a floating point type, the result will be a double
  19. * Otherwise, if either the base or the exponent are 64-bit (long or unsigned long), the result will be a long
  20. * Otherwise, the result will be a 32-bit integer (this covers all other numeric types, including int, short and byte)
  21. For example, using simple integers as arguments will lead to an integer result:
  22. [source.merge.styled,esql]
  23. ----
  24. include::{esql-specs}/math.csv-spec[tag=powII]
  25. ----
  26. [%header.monospaced.styled,format=dsv,separator=|]
  27. |===
  28. include::{esql-specs}/math.csv-spec[tag=powII-result]
  29. |===
  30. NOTE: The actual power function is performed using double precision values for all cases.
  31. This means that for very large non-floating point values there is a small chance that the
  32. operation can lead to slightly different answers than expected.
  33. However, a more likely outcome of very large non-floating point values is numerical overflow.
  34. ==== Arithmetic errors
  35. Arithmetic errors and numeric overflow do not result in an error. Instead, the result will be `null`
  36. and a warning for the `ArithmeticException` added.
  37. For example:
  38. [source.merge.styled,esql]
  39. ----
  40. include::{esql-specs}/math.csv-spec[tag=powULOverrun]
  41. ----
  42. [%header.monospaced.styled,format=dsv,separator=|]
  43. |===
  44. include::{esql-specs}/math.csv-spec[tag=powULOverrun-warning]
  45. |===
  46. [%header.monospaced.styled,format=dsv,separator=|]
  47. |===
  48. include::{esql-specs}/math.csv-spec[tag=powULOverrun-result]
  49. |===
  50. If it is desired to protect against numerical overruns, use `TO_DOUBLE` on either of the arguments:
  51. [source.merge.styled,esql]
  52. ----
  53. include::{esql-specs}/math.csv-spec[tag=pow2d]
  54. ----
  55. [%header.monospaced.styled,format=dsv,separator=|]
  56. |===
  57. include::{esql-specs}/math.csv-spec[tag=pow2d-result]
  58. |===
  59. ==== Fractional exponents
  60. The exponent can be a fraction, which is similar to performing a root.
  61. For example, the exponent of `0.5` will give the square root of the base:
  62. [source.merge.styled,esql]
  63. ----
  64. include::{esql-specs}/math.csv-spec[tag=powID-sqrt]
  65. ----
  66. [%header.monospaced.styled,format=dsv,separator=|]
  67. |===
  68. include::{esql-specs}/math.csv-spec[tag=powID-sqrt-result]
  69. |===
  70. ==== Table of supported input and output types
  71. For clarity, the following table describes the output result type for all combinations of numeric input types:
  72. include::types/pow.asciidoc[]