pow.asciidoc 2.9 KB

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