Module: Cel::Extensions::Math
- Defined in:
- lib/cel/extensions/math.rb
Class Method Summary collapse
- .__check(funcall, checker:) ⇒ Object
- .abs(num, program:) ⇒ Object
- .bitAnd(lhs, rhs, program:) ⇒ Object
- .bitNot(num, program:) ⇒ Object
- .bitOr(lhs, rhs, program:) ⇒ Object
- .bitShiftLeft(num, amount_of_bits, program:) ⇒ Object
- .bitShiftRight(num, amount_of_bits, program:) ⇒ Object
- .bitXor(lhs, rhs, program:) ⇒ Object
- .ceil(num, program:) ⇒ Object
- .floor(num, program:) ⇒ Object
- .greatest(*args, program:) ⇒ Object
- .implicit? ⇒ Boolean
- .isFinite(num, program:) ⇒ Object
- .isInf(num, program:) ⇒ Object
- .isNaN(num, program:) ⇒ Object
- .least(*args, program:) ⇒ Object
- .round(num, program:) ⇒ Object
- .sign(num, program:) ⇒ Object
- .trunc(num, program:) ⇒ Object
Class Method Details
.__check(funcall, checker:) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/cel/extensions/math.rb', line 12 def __check(funcall, checker:) func = funcall.func args = funcall.args case func when :round, :trunc, :floor, :ceil checker.check_arity(func, args, 1) arg = checker.call(args.first) return TYPES[:double] if checker.find_match_all_types(%i[double], arg) when :isInf, :isNaN, :isFinite checker.check_arity(func, args, 1) arg = checker.call(args.first) return TYPES[:bool] if checker.find_match_all_types(%i[double], arg) when :bitNot checker.check_arity(func, args, 1) arg = checker.call(args.first) return TYPES[:int] if checker.find_match_all_types(%i[int uint], arg) when :bitOr, :bitAnd, :bitXor checker.check_arity(func, args, 2) args = args.map(&checker.method(:call)) type = checker.find_match_all_types(%i[int uint], args) return type if type when :sign, :abs checker.check_arity(func, args, 1) arg = checker.call(args.first) type = checker.find_match_all_types(%i[double int uint], arg) return if type when :least, :greatest checker.check_arity_any(func, args) args = args.map(&checker.method(:call)) return TYPES[:any] if args.all? do |arg| case arg when TYPES[:any], TYPES[:int], TYPES[:uint], TYPES[:double], TYPES[:list, :int], TYPES[:list, :uint], TYPES[:list, :double], TYPES[:list, :any] true else false end end when :bitShiftRight, :bitShiftLeft checker.check_arity(func, args, 2) num, amount_of_bits = args.map(&checker.method(:call)) return num if checker.find_match_all_types(%i[int uint], num) && checker.find_match_all_types(%i[int], amount_of_bits) else checker.unsupported_operation(funcall) end checker.unsupported_operation(funcall) end |
.abs(num, program:) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/cel/extensions/math.rb', line 66 def abs(num, program:) num = program.call(num) raise EvaluateError, "out of range" unless num.value.between?(-MAX_INT + 1, MAX_INT - 1) Number.new(num.type, num.value.abs) end |
.bitAnd(lhs, rhs, program:) ⇒ Object
115 116 117 118 119 |
# File 'lib/cel/extensions/math.rb', line 115 def bitAnd(lhs, rhs, program:) lhs = program.call(lhs) rhs = program.call(rhs) Number.new(lhs.type, lhs & rhs) end |
.bitNot(num, program:) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cel/extensions/math.rb', line 98 def bitNot(num, program:) val = program.call(num).value case num.type when TYPES[:int] Number.new(:int, ~val) when TYPES[:uint] Number.new(:uint, ((2**64) - 1) - val) end end |
.bitOr(lhs, rhs, program:) ⇒ Object
109 110 111 112 113 |
# File 'lib/cel/extensions/math.rb', line 109 def bitOr(lhs, rhs, program:) lhs = program.call(lhs) rhs = program.call(rhs) Number.new(lhs.type, lhs | rhs) end |
.bitShiftLeft(num, amount_of_bits, program:) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/cel/extensions/math.rb', line 182 def bitShiftLeft(num, amount_of_bits, program:) num = program.call(num) amount_of_bits = program.call(amount_of_bits).value raise EvaluateError, "math.#{__method__}() negative offset: #{amount_of_bits}" if amount_of_bits.negative? # When the second parameter is 64 or greater, 0 will always be returned return Number.new(num.type, 0) if amount_of_bits >= 64 Number.new(num.type, num.value << amount_of_bits) end |
.bitShiftRight(num, amount_of_bits, program:) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/cel/extensions/math.rb', line 168 def bitShiftRight(num, amount_of_bits, program:) num = program.call(num) amount_of_bits = program.call(amount_of_bits).value raise EvaluateError, "math.#{__method__}() negative offset: #{amount_of_bits}" if amount_of_bits.negative? # When the second parameter is 64 or greater, 0 will always be returned return Number.new(num.type, 0) if amount_of_bits >= 64 value = num.value.negative? ? ((2**64) - 1) & num.value : num.value Number.new(num.type, value >> amount_of_bits) end |
.bitXor(lhs, rhs, program:) ⇒ Object
121 122 123 124 125 |
# File 'lib/cel/extensions/math.rb', line 121 def bitXor(lhs, rhs, program:) lhs = program.call(lhs) rhs = program.call(rhs) Number.new(lhs.type, lhs ^ rhs) end |
.ceil(num, program:) ⇒ Object
92 93 94 95 96 |
# File 'lib/cel/extensions/math.rb', line 92 def ceil(num, program:) num = program.call(num) Number.new(:double, num.value.ceil) end |
.floor(num, program:) ⇒ Object
86 87 88 89 90 |
# File 'lib/cel/extensions/math.rb', line 86 def floor(num, program:) num = program.call(num) Number.new(:double, num.value.floor) end |
.greatest(*args, program:) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/cel/extensions/math.rb', line 153 def greatest(*args, program:) args = args.map(&program.method(:call)) return args.max if args.size > 1 arg = args.first case arg when List greatest(*arg.value, program: program) else arg end end |
.implicit? ⇒ Boolean
8 9 10 |
# File 'lib/cel/extensions/math.rb', line 8 def implicit? false end |
.isFinite(num, program:) ⇒ Object
208 209 210 211 212 |
# File 'lib/cel/extensions/math.rb', line 208 def isFinite(num, program:) val = program.call(num).value Bool.cast(!val.infinite? && !val.nan?) end |
.isInf(num, program:) ⇒ Object
194 195 196 197 198 |
# File 'lib/cel/extensions/math.rb', line 194 def isInf(num, program:) val = program.call(num).value Bool.cast(val.infinite?) end |
.isNaN(num, program:) ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/cel/extensions/math.rb', line 200 def isNaN(num, program:) val = program.call(num).value Bool.cast(val.nan?) rescue FloatDomainError Bool.cast(true) end |
.least(*args, program:) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/cel/extensions/math.rb', line 138 def least(*args, program:) args = args.map(&program.method(:call)) return args.min if args.size > 1 arg = args.first case arg when List least(*arg.value, program: program) else arg end end |
.round(num, program:) ⇒ Object
74 75 76 77 78 |
# File 'lib/cel/extensions/math.rb', line 74 def round(num, program:) num = program.call(num) Number.new(:double, num.value.round) end |