Module: Factbase::Math
- Included in:
- Term
- Defined in:
- lib/factbase/terms/math.rb
Overview
Math terms.
- Author
-
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
- #_arithmetic(op, fact, maps, fb) ⇒ Object
- #_cmp(op, fact, maps, fb) ⇒ Object
- #div(fact, maps, fb) ⇒ Object
- #eq(fact, maps, fb) ⇒ Object
- #gt(fact, maps, fb) ⇒ Object
- #gte(fact, maps, fb) ⇒ Object
- #lt(fact, maps, fb) ⇒ Object
- #lte(fact, maps, fb) ⇒ Object
- #minus(fact, maps, fb) ⇒ Object
- #plus(fact, maps, fb) ⇒ Object
- #times(fact, maps, fb) ⇒ Object
- #zero(fact, maps, fb) ⇒ Object
Instance Method Details
permalink #_arithmetic(op, fact, maps, fb) ⇒ Object
[View source]
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/factbase/terms/math.rb', line 72 def _arithmetic(op, fact, maps, fb) assert_args(2) lefts = _values(0, fact, maps, fb) return nil if lefts.nil? raise 'Too many values at first position, one expected' unless lefts.size == 1 rights = _values(1, fact, maps, fb) return nil if rights.nil? raise 'Too many values at second position, one expected' unless rights.size == 1 v = lefts[0] r = rights[0] if v.is_a?(Time) && r.is_a?(String) (num, units) = r.split num = num.to_i r = case units when 'seconds', 'second' num when 'minutes', 'minute' num * 60 when 'hours', 'hour' num * 60 * 60 when 'days', 'day' num * 60 * 60 * 24 when 'weeks', 'week' num * 60 * 60 * 24 * 7 else raise "Unknown time unit '#{units}' in '#{r}" end end v.send(op, r) end |
permalink #_cmp(op, fact, maps, fb) ⇒ Object
[View source]
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/factbase/terms/math.rb', line 57 def _cmp(op, fact, maps, fb) assert_args(2) lefts = _values(0, fact, maps, fb) return false if lefts.nil? rights = _values(1, fact, maps, fb) return false if rights.nil? lefts.any? do |l| l = l.floor if l.is_a?(Time) && op == :== rights.any? do |r| r = r.floor if r.is_a?(Time) && op == :== l.send(op, r) end end end |
permalink #div(fact, maps, fb) ⇒ Object
[View source]
26 27 28 |
# File 'lib/factbase/terms/math.rb', line 26 def div(fact, maps, fb) _arithmetic(:/, fact, maps, fb) end |
permalink #eq(fact, maps, fb) ⇒ Object
[View source]
37 38 39 |
# File 'lib/factbase/terms/math.rb', line 37 def eq(fact, maps, fb) _cmp(:==, fact, maps, fb) end |
permalink #gt(fact, maps, fb) ⇒ Object
[View source]
45 46 47 |
# File 'lib/factbase/terms/math.rb', line 45 def gt(fact, maps, fb) _cmp(:>, fact, maps, fb) end |
permalink #gte(fact, maps, fb) ⇒ Object
[View source]
53 54 55 |
# File 'lib/factbase/terms/math.rb', line 53 def gte(fact, maps, fb) _cmp(:>=, fact, maps, fb) end |
permalink #lt(fact, maps, fb) ⇒ Object
[View source]
41 42 43 |
# File 'lib/factbase/terms/math.rb', line 41 def lt(fact, maps, fb) _cmp(:<, fact, maps, fb) end |
permalink #lte(fact, maps, fb) ⇒ Object
[View source]
49 50 51 |
# File 'lib/factbase/terms/math.rb', line 49 def lte(fact, maps, fb) _cmp(:<=, fact, maps, fb) end |
permalink #minus(fact, maps, fb) ⇒ Object
[View source]
18 19 20 |
# File 'lib/factbase/terms/math.rb', line 18 def minus(fact, maps, fb) _arithmetic(:-, fact, maps, fb) end |
permalink #plus(fact, maps, fb) ⇒ Object
[View source]
14 15 16 |
# File 'lib/factbase/terms/math.rb', line 14 def plus(fact, maps, fb) _arithmetic(:+, fact, maps, fb) end |
permalink #times(fact, maps, fb) ⇒ Object
[View source]
22 23 24 |
# File 'lib/factbase/terms/math.rb', line 22 def times(fact, maps, fb) _arithmetic(:*, fact, maps, fb) end |
permalink #zero(fact, maps, fb) ⇒ Object
[View source]
30 31 32 33 34 35 |
# File 'lib/factbase/terms/math.rb', line 30 def zero(fact, maps, fb) assert_args(1) vv = _values(0, fact, maps, fb) return false if vv.nil? vv.any? { |v| (v.is_a?(Integer) || v.is_a?(Float)) && v.zero? } end |