funcs_math.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "fmt"
  17. "math"
  18. "math/rand"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/ast"
  21. "github.com/lf-edge/ekuiper/pkg/cast"
  22. )
  23. func registerMathFunc() {
  24. builtins["abs"] = builtinFunc{
  25. fType: ast.FuncTypeScalar,
  26. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  27. switch v := args[0].(type) {
  28. case int:
  29. return int(math.Abs(float64(v))), true
  30. case int64:
  31. return int64(math.Abs(float64(v))), true
  32. case float64:
  33. return math.Abs(v), true
  34. default:
  35. if vi, err := cast.ToInt(v, cast.STRICT); err == nil {
  36. return int(math.Abs(float64(vi))), true
  37. }
  38. if vf, err := cast.ToFloat64(v, cast.STRICT); err == nil {
  39. return math.Abs(vf), true
  40. }
  41. return fmt.Errorf("only float64 & int type are supported"), false
  42. }
  43. },
  44. val: ValidateOneNumberArg,
  45. }
  46. builtins["acos"] = builtinFunc{
  47. fType: ast.FuncTypeScalar,
  48. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  49. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  50. r := math.Acos(v)
  51. if math.IsNaN(r) {
  52. return nil, true
  53. } else {
  54. return r, true
  55. }
  56. } else {
  57. return e, false
  58. }
  59. },
  60. val: ValidateOneNumberArg,
  61. }
  62. builtins["asin"] = builtinFunc{
  63. fType: ast.FuncTypeScalar,
  64. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  65. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  66. r := math.Asin(v)
  67. if math.IsNaN(r) {
  68. return nil, true
  69. } else {
  70. return r, true
  71. }
  72. } else {
  73. return e, false
  74. }
  75. },
  76. val: ValidateOneNumberArg,
  77. }
  78. builtins["atan"] = builtinFunc{
  79. fType: ast.FuncTypeScalar,
  80. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  81. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  82. r := math.Atan(v)
  83. if math.IsNaN(r) {
  84. return nil, true
  85. } else {
  86. return r, true
  87. }
  88. } else {
  89. return e, false
  90. }
  91. },
  92. val: ValidateOneNumberArg,
  93. }
  94. builtins["atan2"] = builtinFunc{
  95. fType: ast.FuncTypeScalar,
  96. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  97. if v1, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  98. if v2, e1 := cast.ToFloat64(args[1], cast.CONVERT_SAMEKIND); e1 == nil {
  99. r := math.Atan2(v1, v2)
  100. if math.IsNaN(r) {
  101. return nil, true
  102. } else {
  103. return r, true
  104. }
  105. } else {
  106. return e1, false
  107. }
  108. } else {
  109. return e, false
  110. }
  111. },
  112. val: ValidateTwoNumberArg,
  113. }
  114. builtins["bitand"] = builtinFunc{
  115. fType: ast.FuncTypeScalar,
  116. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  117. v1, err := cast.ToInt(args[0], cast.STRICT)
  118. if err != nil {
  119. return fmt.Errorf("Expect int type for the first operand but got %v", args[0]), false
  120. }
  121. v2, err := cast.ToInt(args[1], cast.STRICT)
  122. if err != nil {
  123. return fmt.Errorf("Expect int type for the second operand but got %v", args[1]), false
  124. }
  125. return v1 & v2, true
  126. },
  127. val: ValidateTwoIntArg,
  128. }
  129. builtins["bitor"] = builtinFunc{
  130. fType: ast.FuncTypeScalar,
  131. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  132. v1, err := cast.ToInt(args[0], cast.STRICT)
  133. if err != nil {
  134. return fmt.Errorf("Expect int type for the first operand but got %v", args[0]), false
  135. }
  136. v2, err := cast.ToInt(args[1], cast.STRICT)
  137. if err != nil {
  138. return fmt.Errorf("Expect int type for the second operand but got %v", args[1]), false
  139. }
  140. return v1 | v2, true
  141. },
  142. val: ValidateTwoIntArg,
  143. }
  144. builtins["bitxor"] = builtinFunc{
  145. fType: ast.FuncTypeScalar,
  146. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  147. v1, err := cast.ToInt(args[0], cast.STRICT)
  148. if err != nil {
  149. return fmt.Errorf("Expect int type for the first operand but got %v", args[0]), false
  150. }
  151. v2, err := cast.ToInt(args[1], cast.STRICT)
  152. if err != nil {
  153. return fmt.Errorf("Expect int type for the second operand but got %v", args[1]), false
  154. }
  155. return v1 ^ v2, true
  156. },
  157. val: ValidateTwoIntArg,
  158. }
  159. builtins["bitnot"] = builtinFunc{
  160. fType: ast.FuncTypeScalar,
  161. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  162. v1, err := cast.ToInt(args[0], cast.STRICT)
  163. if err != nil {
  164. return fmt.Errorf("Expect int type for operand but got %v", args[0]), false
  165. }
  166. return ^v1, true
  167. },
  168. val: func(_ api.FunctionContext, args []ast.Expr) error {
  169. if err := ValidateLen(1, len(args)); err != nil {
  170. return err
  171. }
  172. if ast.IsFloatArg(args[0]) || ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  173. return ProduceErrInfo(0, "int")
  174. }
  175. return nil
  176. },
  177. }
  178. builtins["ceil"] = builtinFunc{
  179. fType: ast.FuncTypeScalar,
  180. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  181. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  182. return math.Ceil(v), true
  183. } else {
  184. return e, false
  185. }
  186. },
  187. val: ValidateOneNumberArg,
  188. }
  189. builtins["cos"] = builtinFunc{
  190. fType: ast.FuncTypeScalar,
  191. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  192. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  193. r := math.Cos(v)
  194. if math.IsNaN(r) {
  195. return nil, true
  196. } else {
  197. return r, true
  198. }
  199. } else {
  200. return e, false
  201. }
  202. },
  203. val: ValidateOneNumberArg,
  204. }
  205. builtins["cosh"] = builtinFunc{
  206. fType: ast.FuncTypeScalar,
  207. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  208. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  209. r := math.Cosh(v)
  210. if math.IsNaN(r) {
  211. return nil, true
  212. } else {
  213. return r, true
  214. }
  215. } else {
  216. return e, false
  217. }
  218. },
  219. val: ValidateOneNumberArg,
  220. }
  221. builtins["exp"] = builtinFunc{
  222. fType: ast.FuncTypeScalar,
  223. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  224. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  225. r := math.Exp(v)
  226. if math.IsNaN(r) {
  227. return nil, true
  228. } else {
  229. return r, true
  230. }
  231. } else {
  232. return e, false
  233. }
  234. },
  235. val: ValidateOneNumberArg,
  236. }
  237. builtins["ln"] = builtinFunc{
  238. fType: ast.FuncTypeScalar,
  239. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  240. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  241. r := math.Log2(v)
  242. if math.IsNaN(r) {
  243. return nil, true
  244. } else {
  245. return r, true
  246. }
  247. } else {
  248. return e, false
  249. }
  250. },
  251. val: ValidateOneNumberArg,
  252. }
  253. builtins["log"] = builtinFunc{
  254. fType: ast.FuncTypeScalar,
  255. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  256. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  257. r := math.Log10(v)
  258. if math.IsNaN(r) {
  259. return nil, true
  260. } else {
  261. return r, true
  262. }
  263. } else {
  264. return e, false
  265. }
  266. },
  267. val: ValidateOneNumberArg,
  268. }
  269. builtins["mod"] = builtinFunc{
  270. fType: ast.FuncTypeScalar,
  271. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  272. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  273. if v1, e1 := cast.ToFloat64(args[1], cast.CONVERT_SAMEKIND); e == nil {
  274. return math.Mod(v, v1), true
  275. } else {
  276. return e1, false
  277. }
  278. } else {
  279. return e, false
  280. }
  281. },
  282. val: ValidateTwoNumberArg,
  283. }
  284. builtins["power"] = builtinFunc{
  285. fType: ast.FuncTypeScalar,
  286. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  287. if v1, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  288. if v2, e2 := cast.ToFloat64(args[1], cast.CONVERT_SAMEKIND); e2 == nil {
  289. return math.Pow(v1, v2), true
  290. } else {
  291. return e2, false
  292. }
  293. } else {
  294. return e, false
  295. }
  296. },
  297. val: ValidateTwoNumberArg,
  298. }
  299. builtins["rand"] = builtinFunc{
  300. fType: ast.FuncTypeScalar,
  301. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  302. return rand.Float64(), true
  303. },
  304. val: ValidateOneArg,
  305. }
  306. builtins["round"] = builtinFunc{
  307. fType: ast.FuncTypeScalar,
  308. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  309. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  310. return math.Round(v), true
  311. } else {
  312. return e, false
  313. }
  314. },
  315. val: ValidateOneNumberArg,
  316. }
  317. builtins["sign"] = builtinFunc{
  318. fType: ast.FuncTypeScalar,
  319. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  320. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  321. if v > 0 {
  322. return 1, true
  323. } else if v < 0 {
  324. return -1, true
  325. } else {
  326. return 0, true
  327. }
  328. } else {
  329. return e, false
  330. }
  331. },
  332. val: ValidateOneNumberArg,
  333. }
  334. builtins["sin"] = builtinFunc{
  335. fType: ast.FuncTypeScalar,
  336. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  337. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  338. r := math.Sin(v)
  339. if math.IsNaN(r) {
  340. return nil, true
  341. } else {
  342. return r, true
  343. }
  344. } else {
  345. return e, false
  346. }
  347. },
  348. val: ValidateOneNumberArg,
  349. }
  350. builtins["sinh"] = builtinFunc{
  351. fType: ast.FuncTypeScalar,
  352. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  353. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  354. r := math.Sinh(v)
  355. if math.IsNaN(r) {
  356. return nil, true
  357. } else {
  358. return r, true
  359. }
  360. } else {
  361. return e, false
  362. }
  363. },
  364. val: ValidateOneNumberArg,
  365. }
  366. builtins["sqrt"] = builtinFunc{
  367. fType: ast.FuncTypeScalar,
  368. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  369. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  370. r := math.Sqrt(v)
  371. if math.IsNaN(r) {
  372. return nil, true
  373. } else {
  374. return r, true
  375. }
  376. } else {
  377. return e, false
  378. }
  379. },
  380. val: ValidateOneNumberArg,
  381. }
  382. builtins["tan"] = builtinFunc{
  383. fType: ast.FuncTypeScalar,
  384. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  385. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  386. r := math.Tan(v)
  387. if math.IsNaN(r) {
  388. return nil, true
  389. } else {
  390. return r, true
  391. }
  392. } else {
  393. return e, false
  394. }
  395. },
  396. val: ValidateOneNumberArg,
  397. }
  398. builtins["tanh"] = builtinFunc{
  399. fType: ast.FuncTypeScalar,
  400. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  401. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  402. r := math.Tanh(v)
  403. if math.IsNaN(r) {
  404. return nil, true
  405. } else {
  406. return r, true
  407. }
  408. } else {
  409. return e, false
  410. }
  411. },
  412. val: ValidateOneNumberArg,
  413. }
  414. }