Class: Int
- Inherits:
-
Numeric
- Object
- Numeric
- Int
- Defined in:
- lib/int.rb,
lib/int/version.rb
Constant Summary collapse
- VERSION =
"0.0.1.interval"
Instance Attribute Summary collapse
-
#begin ⇒ Object
readonly
Returns the value of attribute begin.
-
#end ⇒ Object
readonly
Returns the value of attribute end.
-
#median ⇒ Object
readonly
Returns the value of attribute median.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #>=(other) ⇒ Object
- #coerce(other) ⇒ Object
- #comparable?(other) ⇒ Boolean
-
#initialize(beg, encl = nil, error: nil) ⇒ Int
constructor
A new instance of Int.
- #inspect ⇒ Object
- #round(n) ⇒ Object
- #to_s ⇒ Object
- #with_error(e) ⇒ Object
- #with_error_percent(e) ⇒ Object
Constructor Details
#initialize(beg, encl = nil, error: nil) ⇒ Int
Returns a new instance of Int.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/int.rb', line 9 def initialize(beg, encl = nil, error: nil) if error raise ArgumentError, 'do not specify 2nd arg when `error:`' if encl raise ArgumentError, 'error value should be non-negtative' if error.negative? med = beg beg, encl = med-error, med+error else raise ArgumentError, 'specify 2nd arg when no `error:`' unless encl raise ArgumentError, "needs begin <= end: #{beg} <=> #{encl}" unless beg <= encl end @begin, @end = beg, encl @values = [beg, encl] @median = (beg + encl) / 2.0 end |
Instance Attribute Details
#begin ⇒ Object (readonly)
Returns the value of attribute begin.
6 7 8 |
# File 'lib/int.rb', line 6 def begin @begin end |
#end ⇒ Object (readonly)
Returns the value of attribute end.
6 7 8 |
# File 'lib/int.rb', line 6 def end @end end |
#median ⇒ Object (readonly)
Returns the value of attribute median.
6 7 8 |
# File 'lib/int.rb', line 6 def median @median end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
7 8 9 |
# File 'lib/int.rb', line 7 def values @values end |
Class Method Details
.error(error) ⇒ Object
40 41 42 |
# File 'lib/int.rb', line 40 def self.error(error) new(1, error:) end |
Instance Method Details
#*(other) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/int.rb', line 63 def *(other) other = ensure_coerced(other) mul = values.product(other.values).map{_1.inject(:*)} raise "got a NaN; I don't know what to do" if mul.any?{|x| x.respond_to?(:nan?) && x.nan?} new(*mul.minmax) end |
#+(other) ⇒ Object
53 54 55 56 |
# File 'lib/int.rb', line 53 def +(other) other = ensure_coerced(other) new(@begin + other.begin, @end + other.end) end |
#-(other) ⇒ Object
58 59 60 61 |
# File 'lib/int.rb', line 58 def -(other) other = ensure_coerced(other) new(@begin - other.end, @end - other.begin) end |
#/(other) ⇒ Object
70 71 72 |
# File 'lib/int.rb', line 70 def /(other) self * ensure_coerced(other).invert end |
#<=(other) ⇒ Object
82 83 84 85 |
# File 'lib/int.rb', line 82 def <=(other) other = ensure_coerced(other) @end <= other.begin end |
#<=>(other) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/int.rb', line 74 def <=>(other) other = ensure_coerced(other) return 0 if @begin == @end && @begin == other.begin && @begin == other.end return 1 if @begin > other.end return -1 if @end < other.begin nil end |
#>=(other) ⇒ Object
87 88 89 90 |
# File 'lib/int.rb', line 87 def >=(other) other = ensure_coerced(other) @begin >= other.end end |
#coerce(other) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/int.rb', line 44 def coerce(other) if scala?(other) other = new(*[other]*2) else raise "Unknown class #{other.class}: #{other}" end [other, self] end |
#comparable?(other) ⇒ Boolean
92 93 94 95 |
# File 'lib/int.rb', line 92 def comparable?(other) other = ensure_coerced(other) rescue (return false) !!(self <=> other) || @end == other.begin || @begin == other.end end |
#inspect ⇒ Object
105 |
# File 'lib/int.rb', line 105 def inspect = self.class.name + to_s.sub('..', "..(#{median})..") |
#round(n) ⇒ Object
97 98 99 |
# File 'lib/int.rb', line 97 def round(n) new(*[self.begin,self.end].map{_1.round(n)}).round_median(n) end |
#to_s ⇒ Object
104 |
# File 'lib/int.rb', line 104 def to_s = "(#{self.begin}..#{self.end})" |
#with_error(e) ⇒ Object
101 |
# File 'lib/int.rb', line 101 def with_error(e) = self * self.class.error(e) |
#with_error_percent(e) ⇒ Object
102 |
# File 'lib/int.rb', line 102 def with_error_percent(e) = with_error(e * 0.01) |