github.com/matrixorigin/matrixone@v1.2.0/pkg/vectorize/momath/math.go (about) 1 // Copyright 2022 Matrix Origin 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 15 package momath 16 17 import ( 18 "golang.org/x/exp/constraints" 19 "math" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 ) 23 24 func Acos(v float64) (float64, error) { 25 if v < -1 || v > 1 { 26 // MySQL is totally F***ed. 27 return 0, moerr.NewInvalidArgNoCtx("acos", v) 28 } else { 29 return math.Acos(v), nil 30 } 31 } 32 33 func Atan(v float64) (float64, error) { 34 return math.Atan(v), nil 35 } 36 37 func Cos(v float64) (float64, error) { 38 return math.Cos(v), nil 39 } 40 41 func Cot(v float64) (float64, error) { 42 if v == 0 { 43 return 0, moerr.NewInvalidArgNoCtx("cot", "0") 44 } else { 45 return math.Tan(math.Pi/2.0 - v), nil 46 } 47 } 48 49 func Exp(v float64) (float64, error) { 50 return math.Exp(v), nil 51 } 52 53 func Sqrt(v float64) (float64, error) { 54 if v < 0 { 55 return 0, moerr.NewInvalidArgNoCtx("Sqrt", v) 56 } else { 57 return math.Sqrt(v), nil 58 } 59 } 60 61 func Ln(v float64) (float64, error) { 62 if v <= 0 { 63 return 0, moerr.NewInvalidArgNoCtx("ln", v) 64 } else { 65 return math.Log(v), nil 66 } 67 } 68 69 func Log2(v float64) (float64, error) { 70 if v <= 0 { 71 return 0, moerr.NewInvalidArgNoCtx("log2", v) 72 } else { 73 return math.Log2(v), nil 74 } 75 } 76 77 func Lg(v float64) (float64, error) { 78 if v <= 0 { 79 return 0, moerr.NewInvalidArgNoCtx("log10", v) 80 } else { 81 return math.Log10(v), nil 82 } 83 } 84 85 func Sin(v float64) (float64, error) { 86 return math.Sin(v), nil 87 } 88 89 func Sinh(v float64) (float64, error) { 90 r := math.Sinh(v) 91 if math.IsInf(r, 0) { 92 return 0, moerr.NewOutOfRangeNoCtx("float64", "DOUBLE value is out of range in 'sinh(%v)'", v) 93 } 94 return r, nil 95 } 96 97 func Tan(v float64) (float64, error) { 98 return math.Tan(v), nil 99 } 100 101 func AbsSigned[T constraints.Signed | constraints.Float](v T) (T, error) { 102 //NOTE: AbsSigned specifically deals with int and float and not uint. 103 // If we have uint, we return the value as such. 104 if v < 0 { 105 v = -v 106 } 107 if v < 0 { 108 // This could occur for int8 (-128 to 127) 109 // If the v is -128 and if we multiply by -1 = 128, which is out of range of int8. It could give a -ve value for such case. 110 return 0, moerr.NewOutOfRangeNoCtx("int", "'%v'", v) 111 } 112 return v, nil 113 }