funcs_math.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // Copyright 2022 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. "github.com/lf-edge/ekuiper/pkg/api"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. "math"
  20. "math/rand"
  21. )
  22. func registerMathFunc() {
  23. builtins["abs"] = builtinFunc{
  24. fType: ast.FuncTypeScalar,
  25. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  26. if v, ok := args[0].(int); ok {
  27. t := float64(v)
  28. var ret = int(math.Abs(t))
  29. return ret, true
  30. } else if v, ok := args[0].(float64); ok {
  31. return math.Abs(v), true
  32. } else {
  33. return fmt.Errorf("only float64 & int type are supported"), false
  34. }
  35. },
  36. val: ValidateOneNumberArg,
  37. }
  38. builtins["acos"] = builtinFunc{
  39. fType: ast.FuncTypeScalar,
  40. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  41. if v, e := toF64(args[0]); e == nil {
  42. return math.Acos(v), true
  43. } else {
  44. return e, false
  45. }
  46. },
  47. val: ValidateOneNumberArg,
  48. }
  49. builtins["asin"] = builtinFunc{
  50. fType: ast.FuncTypeScalar,
  51. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  52. if v, e := toF64(args[0]); e == nil {
  53. return math.Asin(v), true
  54. } else {
  55. return e, false
  56. }
  57. },
  58. val: ValidateOneNumberArg,
  59. }
  60. builtins["atan"] = builtinFunc{
  61. fType: ast.FuncTypeScalar,
  62. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  63. if v, e := toF64(args[0]); e == nil {
  64. return math.Atan(v), true
  65. } else {
  66. return e, false
  67. }
  68. },
  69. val: ValidateOneNumberArg,
  70. }
  71. builtins["atan2"] = builtinFunc{
  72. fType: ast.FuncTypeScalar,
  73. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  74. if v1, e := toF64(args[0]); e == nil {
  75. if v2, e1 := toF64(args[1]); e1 == nil {
  76. return math.Atan2(v1, v2), true
  77. } else {
  78. return e1, false
  79. }
  80. } else {
  81. return e, false
  82. }
  83. },
  84. val: ValidateTwoNumberArg,
  85. }
  86. builtins["bitand"] = builtinFunc{
  87. fType: ast.FuncTypeScalar,
  88. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  89. v1, ok1 := args[0].(int)
  90. v2, ok2 := args[1].(int)
  91. if ok1 && ok2 {
  92. return v1 & v2, true
  93. } else {
  94. return fmt.Errorf("Expect int type for both operands."), false
  95. }
  96. },
  97. val: ValidateTwoIntArg,
  98. }
  99. builtins["bitor"] = builtinFunc{
  100. fType: ast.FuncTypeScalar,
  101. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  102. v1, ok1 := args[0].(int)
  103. v2, ok2 := args[1].(int)
  104. if ok1 && ok2 {
  105. return v1 | v2, true
  106. } else {
  107. return fmt.Errorf("Expect int type for both operands."), false
  108. }
  109. },
  110. val: ValidateTwoIntArg,
  111. }
  112. builtins["bitxor"] = builtinFunc{
  113. fType: ast.FuncTypeScalar,
  114. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  115. v1, ok1 := args[0].(int)
  116. v2, ok2 := args[1].(int)
  117. if ok1 && ok2 {
  118. return v1 ^ v2, true
  119. } else {
  120. return fmt.Errorf("Expect int type for both operands."), false
  121. }
  122. },
  123. val: ValidateTwoIntArg,
  124. }
  125. builtins["bitnot"] = builtinFunc{
  126. fType: ast.FuncTypeScalar,
  127. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  128. v1, ok1 := args[0].(int)
  129. if ok1 {
  130. return ^v1, true
  131. } else {
  132. return fmt.Errorf("Expect int type for operand."), false
  133. }
  134. },
  135. val: func(_ api.FunctionContext, args []ast.Expr) error {
  136. if err := ValidateLen(1, len(args)); err != nil {
  137. return err
  138. }
  139. if ast.IsFloatArg(args[0]) || ast.IsStringArg(args[0]) || ast.IsTimeArg(args[0]) || ast.IsBooleanArg(args[0]) {
  140. return ProduceErrInfo(0, "int")
  141. }
  142. return nil
  143. },
  144. }
  145. builtins["ceil"] = builtinFunc{
  146. fType: ast.FuncTypeScalar,
  147. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  148. if v, e := toF64(args[0]); e == nil {
  149. return math.Ceil(v), true
  150. } else {
  151. return e, false
  152. }
  153. },
  154. val: ValidateOneNumberArg,
  155. }
  156. builtins["cos"] = builtinFunc{
  157. fType: ast.FuncTypeScalar,
  158. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  159. if v, e := toF64(args[0]); e == nil {
  160. return math.Cos(v), true
  161. } else {
  162. return e, false
  163. }
  164. },
  165. val: ValidateOneNumberArg,
  166. }
  167. builtins["cosh"] = builtinFunc{
  168. fType: ast.FuncTypeScalar,
  169. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  170. if v, e := toF64(args[0]); e == nil {
  171. return math.Cosh(v), true
  172. } else {
  173. return e, false
  174. }
  175. },
  176. val: ValidateOneNumberArg,
  177. }
  178. builtins["exp"] = builtinFunc{
  179. fType: ast.FuncTypeScalar,
  180. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  181. if v, e := toF64(args[0]); e == nil {
  182. return math.Exp(v), true
  183. } else {
  184. return e, false
  185. }
  186. },
  187. val: ValidateOneNumberArg,
  188. }
  189. builtins["ln"] = builtinFunc{
  190. fType: ast.FuncTypeScalar,
  191. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  192. if v, e := toF64(args[0]); e == nil {
  193. return math.Log2(v), true
  194. } else {
  195. return e, false
  196. }
  197. },
  198. val: ValidateOneNumberArg,
  199. }
  200. builtins["log"] = builtinFunc{
  201. fType: ast.FuncTypeScalar,
  202. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  203. if v, e := toF64(args[0]); e == nil {
  204. return math.Log10(v), true
  205. } else {
  206. return e, false
  207. }
  208. },
  209. val: ValidateOneNumberArg,
  210. }
  211. builtins["mod"] = builtinFunc{
  212. fType: ast.FuncTypeScalar,
  213. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  214. if v, e := toF64(args[0]); e == nil {
  215. if v1, e1 := toF64(args[1]); e == nil {
  216. return math.Mod(v, v1), true
  217. } else {
  218. return e1, false
  219. }
  220. } else {
  221. return e, false
  222. }
  223. },
  224. val: ValidateTwoNumberArg,
  225. }
  226. builtins["power"] = builtinFunc{
  227. fType: ast.FuncTypeScalar,
  228. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  229. if v1, e := toF64(args[0]); e == nil {
  230. if v2, e2 := toF64(args[1]); e2 == nil {
  231. return math.Pow(v1, v2), true
  232. } else {
  233. return e2, false
  234. }
  235. } else {
  236. return e, false
  237. }
  238. },
  239. val: ValidateTwoNumberArg,
  240. }
  241. builtins["rand"] = builtinFunc{
  242. fType: ast.FuncTypeScalar,
  243. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  244. return rand.Float64(), true
  245. },
  246. val: ValidateOneArg,
  247. }
  248. builtins["round"] = builtinFunc{
  249. fType: ast.FuncTypeScalar,
  250. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  251. if v, e := toF64(args[0]); e == nil {
  252. return math.Round(v), true
  253. } else {
  254. return e, false
  255. }
  256. },
  257. val: ValidateOneNumberArg,
  258. }
  259. builtins["sign"] = builtinFunc{
  260. fType: ast.FuncTypeScalar,
  261. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  262. if v, e := toF64(args[0]); e == nil {
  263. if v > 0 {
  264. return 1, true
  265. } else if v < 0 {
  266. return -1, true
  267. } else {
  268. return 0, true
  269. }
  270. } else {
  271. return e, false
  272. }
  273. },
  274. val: ValidateOneNumberArg,
  275. }
  276. builtins["sin"] = builtinFunc{
  277. fType: ast.FuncTypeScalar,
  278. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  279. if v, e := toF64(args[0]); e == nil {
  280. return math.Sin(v), true
  281. } else {
  282. return e, false
  283. }
  284. },
  285. val: ValidateOneNumberArg,
  286. }
  287. builtins["sinh"] = builtinFunc{
  288. fType: ast.FuncTypeScalar,
  289. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  290. if v, e := toF64(args[0]); e == nil {
  291. return math.Sinh(v), true
  292. } else {
  293. return e, false
  294. }
  295. },
  296. val: ValidateOneNumberArg,
  297. }
  298. builtins["sqrt"] = builtinFunc{
  299. fType: ast.FuncTypeScalar,
  300. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  301. if v, e := toF64(args[0]); e == nil {
  302. return math.Sqrt(v), true
  303. } else {
  304. return e, false
  305. }
  306. },
  307. val: ValidateOneNumberArg,
  308. }
  309. builtins["tan"] = builtinFunc{
  310. fType: ast.FuncTypeScalar,
  311. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  312. if v, e := toF64(args[0]); e == nil {
  313. return math.Tan(v), true
  314. } else {
  315. return e, false
  316. }
  317. },
  318. val: ValidateOneNumberArg,
  319. }
  320. builtins["tanh"] = builtinFunc{
  321. fType: ast.FuncTypeScalar,
  322. exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
  323. if v, e := toF64(args[0]); e == nil {
  324. return math.Tanh(v), true
  325. } else {
  326. return e, false
  327. }
  328. },
  329. val: ValidateOneNumberArg,
  330. }
  331. }
  332. func toF64(arg interface{}) (float64, error) {
  333. if v, ok := arg.(float64); ok {
  334. return v, nil
  335. } else if v, ok := arg.(int); ok {
  336. return float64(v), nil
  337. }
  338. return 0, fmt.Errorf("only float64 & int type are supported")
  339. }