pow.asciidoc 3.3 KB

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