funcs_math.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. "errors"
  17. "fmt"
  18. "math"
  19. "math/rand"
  20. "github.com/lf-edge/ekuiper/pkg/api"
  21. "github.com/lf-edge/ekuiper/pkg/ast"
  22. "github.com/lf-edge/ekuiper/pkg/cast"
  23. )
  24. func registerMathFunc() {
  25. builtins["abs"] = builtinFunc{
  26. fType: ast.FuncTypeScalar,
  27. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  28. switch v := args[0].(type) {
  29. case int:
  30. return int(math.Abs(float64(v))), true
  31. case int64:
  32. return int64(math.Abs(float64(v))), true
  33. case float64:
  34. return math.Abs(v), true
  35. default:
  36. if vi, err := cast.ToInt(v, cast.STRICT); err == nil {
  37. return int(math.Abs(float64(vi))), true
  38. }
  39. if vf, err := cast.ToFloat64(v, cast.STRICT); err == nil {
  40. return math.Abs(vf), true
  41. }
  42. return fmt.Errorf("only float64 & int type are supported"), false
  43. }
  44. },
  45. val: ValidateOneNumberArg,
  46. check: returnNilIfHasAnyNil,
  47. }
  48. builtins["acos"] = builtinFunc{
  49. fType: ast.FuncTypeScalar,
  50. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  51. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  52. r := math.Acos(v)
  53. if math.IsNaN(r) {
  54. return nil, true
  55. } else {
  56. return r, true
  57. }
  58. } else {
  59. return e, false
  60. }
  61. },
  62. val: ValidateOneNumberArg,
  63. check: returnNilIfHasAnyNil,
  64. }
  65. builtins["asin"] = builtinFunc{
  66. fType: ast.FuncTypeScalar,
  67. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  68. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  69. r := math.Asin(v)
  70. if math.IsNaN(r) {
  71. return nil, true
  72. } else {
  73. return r, true
  74. }
  75. } else {
  76. return e, false
  77. }
  78. },
  79. val: ValidateOneNumberArg,
  80. check: returnNilIfHasAnyNil,
  81. }
  82. builtins["atan"] = builtinFunc{
  83. fType: ast.FuncTypeScalar,
  84. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  85. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  86. r := math.Atan(v)
  87. if math.IsNaN(r) {
  88. return nil, true
  89. } else {
  90. return r, true
  91. }
  92. } else {
  93. return e, false
  94. }
  95. },
  96. val: ValidateOneNumberArg,
  97. check: returnNilIfHasAnyNil,
  98. }
  99. builtins["atan2"] = builtinFunc{
  100. fType: ast.FuncTypeScalar,
  101. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  102. if v1, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  103. if v2, e1 := cast.ToFloat64(args[1], cast.CONVERT_SAMEKIND); e1 == nil {
  104. r := math.Atan2(v1, v2)
  105. if math.IsNaN(r) {
  106. return nil, true
  107. } else {
  108. return r, true
  109. }
  110. } else {
  111. return e1, false
  112. }
  113. } else {
  114. return e, false
  115. }
  116. },
  117. val: ValidateTwoNumberArg,
  118. check: returnNilIfHasAnyNil,
  119. }
  120. builtins["bitand"] = builtinFunc{
  121. fType: ast.FuncTypeScalar,
  122. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  123. v1, err := cast.ToInt(args[0], cast.STRICT)
  124. if err != nil {
  125. return fmt.Errorf("Expect int type for the first operand but got %v", args[0]), false
  126. }
  127. v2, err := cast.ToInt(args[1], cast.STRICT)
  128. if err != nil {
  129. return fmt.Errorf("Expect int type for the second operand but got %v", args[1]), false
  130. }
  131. return v1 & v2, true
  132. },
  133. val: ValidateTwoIntArg,
  134. check: returnNilIfHasAnyNil,
  135. }
  136. builtins["bitor"] = builtinFunc{
  137. fType: ast.FuncTypeScalar,
  138. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  139. v1, err := cast.ToInt(args[0], cast.STRICT)
  140. if err != nil {
  141. return fmt.Errorf("Expect int type for the first operand but got %v", args[0]), false
  142. }
  143. v2, err := cast.ToInt(args[1], cast.STRICT)
  144. if err != nil {
  145. return fmt.Errorf("Expect int type for the second operand but got %v", args[1]), false
  146. }
  147. return v1 | v2, true
  148. },
  149. val: ValidateTwoIntArg,
  150. check: returnNilIfHasAnyNil,
  151. }
  152. builtins["bitxor"] = builtinFunc{
  153. fType: ast.FuncTypeScalar,
  154. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  155. v1, err := cast.ToInt(args[0], cast.STRICT)
  156. if err != nil {
  157. return fmt.Errorf("Expect int type for the first operand but got %v", args[0]), false
  158. }
  159. v2, err := cast.ToInt(args[1], cast.STRICT)
  160. if err != nil {
  161. return fmt.Errorf("Expect int type for the second operand but got %v", args[1]), false
  162. }
  163. return v1 ^ v2, true
  164. },
  165. val: ValidateTwoIntArg,
  166. check: returnNilIfHasAnyNil,
  167. }
  168. builtins["bitnot"] = builtinFunc{
  169. fType: ast.FuncTypeScalar,
  170. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  171. v1, err := cast.ToInt(args[0], cast.STRICT)
  172. if err != nil {
  173. return fmt.Errorf("Expect int type for operand but got %v", args[0]), false
  174. }
  175. return ^v1, true
  176. },
  177. val: func(_ api.FunctionContext, args []ast.Expr) error {
  178. if err := ValidateLen(1, len(args)); err != nil {
  179. return err
  180. }
  181. if ast.IsFloatArg(args[0]) || ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  182. return ProduceErrInfo(0, "int")
  183. }
  184. return nil
  185. },
  186. check: returnNilIfHasAnyNil,
  187. }
  188. builtins["ceiling"] = builtinFunc{
  189. fType: ast.FuncTypeScalar,
  190. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  191. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  192. return math.Ceil(v), true
  193. } else {
  194. return e, false
  195. }
  196. },
  197. val: ValidateOneNumberArg,
  198. check: returnNilIfHasAnyNil,
  199. }
  200. builtins["ceil"] = builtins["ceiling"] // Synonym for CEILING.
  201. builtins["cos"] = builtinFunc{
  202. fType: ast.FuncTypeScalar,
  203. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  204. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  205. r := math.Cos(v)
  206. if math.IsNaN(r) {
  207. return nil, true
  208. } else {
  209. return r, true
  210. }
  211. } else {
  212. return e, false
  213. }
  214. },
  215. val: ValidateOneNumberArg,
  216. check: returnNilIfHasAnyNil,
  217. }
  218. builtins["cosh"] = builtinFunc{
  219. fType: ast.FuncTypeScalar,
  220. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  221. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  222. r := math.Cosh(v)
  223. if math.IsNaN(r) {
  224. return nil, true
  225. } else {
  226. return r, true
  227. }
  228. } else {
  229. return e, false
  230. }
  231. },
  232. val: ValidateOneNumberArg,
  233. check: returnNilIfHasAnyNil,
  234. }
  235. builtins["exp"] = builtinFunc{
  236. fType: ast.FuncTypeScalar,
  237. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  238. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  239. r := math.Exp(v)
  240. if math.IsNaN(r) {
  241. return nil, true
  242. } else {
  243. return r, true
  244. }
  245. } else {
  246. return e, false
  247. }
  248. },
  249. val: ValidateOneNumberArg,
  250. check: returnNilIfHasAnyNil,
  251. }
  252. builtins["floor"] = builtinFunc{
  253. fType: ast.FuncTypeScalar,
  254. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  255. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  256. return math.Floor(v), true
  257. } else {
  258. return e, false
  259. }
  260. },
  261. val: ValidateOneNumberArg,
  262. check: returnNilIfHasAnyNil,
  263. }
  264. builtins["ln"] = builtinFunc{
  265. fType: ast.FuncTypeScalar,
  266. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  267. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  268. r := math.Log2(v)
  269. if math.IsNaN(r) {
  270. return nil, true
  271. } else {
  272. return r, true
  273. }
  274. } else {
  275. return e, false
  276. }
  277. },
  278. val: ValidateOneNumberArg,
  279. check: returnNilIfHasAnyNil,
  280. }
  281. builtins["log"] = builtinFunc{
  282. fType: ast.FuncTypeScalar,
  283. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  284. v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND)
  285. if e != nil {
  286. return e, false
  287. }
  288. var r float64
  289. if len(args) == 1 {
  290. r = math.Log10(v)
  291. } else {
  292. x, e := cast.ToFloat64(args[1], cast.CONVERT_SAMEKIND)
  293. if e != nil {
  294. return e, false
  295. }
  296. r = math.Log(x) / math.Log(v)
  297. }
  298. if !math.IsNaN(r) {
  299. return r, true
  300. }
  301. return nil, true
  302. },
  303. val: func(_ api.FunctionContext, args []ast.Expr) error {
  304. if len(args) != 1 && len(args) != 2 {
  305. return errors.New("Expect 1 or 2 arguments only")
  306. }
  307. if ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  308. return ProduceErrInfo(0, "number - float or int")
  309. }
  310. return nil
  311. },
  312. check: returnNilIfHasAnyNil,
  313. }
  314. builtins["mod"] = builtinFunc{
  315. fType: ast.FuncTypeScalar,
  316. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  317. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  318. if v1, e1 := cast.ToFloat64(args[1], cast.CONVERT_SAMEKIND); e == nil {
  319. return math.Mod(v, v1), true
  320. } else {
  321. return e1, false
  322. }
  323. } else {
  324. return e, false
  325. }
  326. },
  327. val: ValidateTwoNumberArg,
  328. check: returnNilIfHasAnyNil,
  329. }
  330. builtins["pi"] = builtinFunc{
  331. fType: ast.FuncTypeScalar,
  332. exec: func(_ api.FunctionContext, _ []interface{}) (interface{}, bool) {
  333. return math.Pi, true
  334. },
  335. val: ValidateNoArg,
  336. check: returnNilIfHasAnyNil,
  337. }
  338. builtins["power"] = builtinFunc{
  339. fType: ast.FuncTypeScalar,
  340. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  341. if v1, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  342. if v2, e2 := cast.ToFloat64(args[1], cast.CONVERT_SAMEKIND); e2 == nil {
  343. return math.Pow(v1, v2), true
  344. } else {
  345. return e2, false
  346. }
  347. } else {
  348. return e, false
  349. }
  350. },
  351. val: ValidateTwoNumberArg,
  352. check: returnNilIfHasAnyNil,
  353. }
  354. builtins["pow"] = builtins["power"] // Synonym for POWER.
  355. builtins["rand"] = builtinFunc{
  356. fType: ast.FuncTypeScalar,
  357. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  358. return rand.Float64(), true
  359. },
  360. val: ValidateNoArg,
  361. }
  362. builtins["round"] = builtinFunc{
  363. fType: ast.FuncTypeScalar,
  364. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  365. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  366. return math.Round(v), true
  367. } else {
  368. return e, false
  369. }
  370. },
  371. val: ValidateOneNumberArg,
  372. check: returnNilIfHasAnyNil,
  373. }
  374. builtins["sign"] = builtinFunc{
  375. fType: ast.FuncTypeScalar,
  376. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  377. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  378. if v > 0 {
  379. return 1, true
  380. } else if v < 0 {
  381. return -1, true
  382. } else {
  383. return 0, true
  384. }
  385. } else {
  386. return e, false
  387. }
  388. },
  389. val: ValidateOneNumberArg,
  390. check: returnNilIfHasAnyNil,
  391. }
  392. builtins["sin"] = builtinFunc{
  393. fType: ast.FuncTypeScalar,
  394. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  395. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  396. r := math.Sin(v)
  397. if math.IsNaN(r) {
  398. return nil, true
  399. } else {
  400. return r, true
  401. }
  402. } else {
  403. return e, false
  404. }
  405. },
  406. val: ValidateOneNumberArg,
  407. check: returnNilIfHasAnyNil,
  408. }
  409. builtins["sinh"] = builtinFunc{
  410. fType: ast.FuncTypeScalar,
  411. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  412. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  413. r := math.Sinh(v)
  414. if math.IsNaN(r) {
  415. return nil, true
  416. } else {
  417. return r, true
  418. }
  419. } else {
  420. return e, false
  421. }
  422. },
  423. val: ValidateOneNumberArg,
  424. check: returnNilIfHasAnyNil,
  425. }
  426. builtins["sqrt"] = builtinFunc{
  427. fType: ast.FuncTypeScalar,
  428. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  429. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  430. r := math.Sqrt(v)
  431. if math.IsNaN(r) {
  432. return nil, true
  433. } else {
  434. return r, true
  435. }
  436. } else {
  437. return e, false
  438. }
  439. },
  440. val: ValidateOneNumberArg,
  441. check: returnNilIfHasAnyNil,
  442. }
  443. builtins["tan"] = builtinFunc{
  444. fType: ast.FuncTypeScalar,
  445. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  446. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  447. r := math.Tan(v)
  448. if math.IsNaN(r) {
  449. return nil, true
  450. } else {
  451. return r, true
  452. }
  453. } else {
  454. return e, false
  455. }
  456. },
  457. val: ValidateOneNumberArg,
  458. check: returnNilIfHasAnyNil,
  459. }
  460. builtins["tanh"] = builtinFunc{
  461. fType: ast.FuncTypeScalar,
  462. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  463. if v, e := cast.ToFloat64(args[0], cast.CONVERT_SAMEKIND); e == nil {
  464. r := math.Tanh(v)
  465. if math.IsNaN(r) {
  466. return nil, true
  467. } else {
  468. return r, true
  469. }
  470. } else {
  471. return e, false
  472. }
  473. },
  474. val: ValidateOneNumberArg,
  475. check: returnNilIfHasAnyNil,
  476. }
  477. }