Class: Numeric
- Includes:
- Comparable
- Defined in:
- opal/opal/corelib/numeric.rb
Instance Method Summary collapse
- #%(other) ⇒ Object (also: #modulo)
- #+@ ⇒ Object
- #-@ ⇒ Object
- #<=>(other) ⇒ Object
- #__coerced__(method, other) ⇒ Object
- #abs ⇒ Object (also: #magnitude)
- #abs2 ⇒ Object
- #angle ⇒ Object (also: #arg)
- #ceil ⇒ Object
- #clone(freeze: true) ⇒ Object
- #coerce(other) ⇒ Object
- #conj ⇒ Object (also: #conjugate)
- #denominator ⇒ Object
- #div(other) ⇒ Object
- #divmod(other) ⇒ Object
- #dup ⇒ Object
- #fdiv(other) ⇒ Object
- #floor ⇒ Object
- #i ⇒ Object
- #imag ⇒ Object (also: #imaginary)
- #integer? ⇒ Boolean
- #negative? ⇒ Boolean
- #nonzero? ⇒ Boolean
- #numerator ⇒ Object
- #polar ⇒ Object
- #positive? ⇒ Boolean
- #quo(other) ⇒ Object
- #real ⇒ Object
- #real? ⇒ Boolean
- #rect ⇒ Object (also: #rectangular)
- #round(digits = undefined) ⇒ Object
- #to_c ⇒ Object
- #to_int ⇒ Object
- #truncate ⇒ Object
- #zero? ⇒ Boolean
Methods included from Comparable
#<, #<=, #==, #>, #>=, #between?, #clamp, normalize
Instance Method Details
#%(other) ⇒ Object Also known as: modulo
46 47 48 |
# File 'opal/opal/corelib/numeric.rb', line 46 def %(other) self - other * self.div(other) end |
#+@ ⇒ Object
38 39 40 |
# File 'opal/opal/corelib/numeric.rb', line 38 def +@ self end |
#-@ ⇒ Object
42 43 44 |
# File 'opal/opal/corelib/numeric.rb', line 42 def -@ 0 - self end |
#<=>(other) ⇒ Object
30 31 32 33 34 35 36 |
# File 'opal/opal/corelib/numeric.rb', line 30 def <=>(other) if equal? other return 0 end nil end |
#__coerced__(method, other) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'opal/opal/corelib/numeric.rb', line 14 def __coerced__(method, other) begin a, b = other.coerce(self) rescue case method when :+, :-, :*, :/, :%, :&, :|, :^, :** raise TypeError, "#{other.class} can't be coerce into Numeric" when :>, :>=, :<, :<=, :<=> raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" end end a.__send__ method, b end |
#abs ⇒ Object Also known as: magnitude
50 51 52 |
# File 'opal/opal/corelib/numeric.rb', line 50 def abs self < 0 ? -self : self end |
#abs2 ⇒ Object
54 55 56 |
# File 'opal/opal/corelib/numeric.rb', line 54 def abs2 self * self end |
#angle ⇒ Object Also known as: arg
58 59 60 |
# File 'opal/opal/corelib/numeric.rb', line 58 def angle self < 0 ? Math::PI : 0 end |
#ceil ⇒ Object
64 65 66 |
# File 'opal/opal/corelib/numeric.rb', line 64 def ceil to_f.ceil end |
#clone(freeze: true) ⇒ Object
178 179 180 |
# File 'opal/opal/corelib/numeric.rb', line 178 def clone(freeze: true) self end |
#coerce(other) ⇒ Object
6 7 8 9 10 11 12 |
# File 'opal/opal/corelib/numeric.rb', line 6 def coerce(other) if other.instance_of? self.class return [other, self] end [Float(other), Float(self)] end |
#conj ⇒ Object Also known as: conjugate
68 69 70 |
# File 'opal/opal/corelib/numeric.rb', line 68 def conj self end |
#denominator ⇒ Object
74 75 76 |
# File 'opal/opal/corelib/numeric.rb', line 74 def denominator to_r.denominator end |
#div(other) ⇒ Object
78 79 80 81 82 |
# File 'opal/opal/corelib/numeric.rb', line 78 def div(other) raise ZeroDivisionError, "divided by o" if other == 0 (self / other).floor end |
#divmod(other) ⇒ Object
84 85 86 |
# File 'opal/opal/corelib/numeric.rb', line 84 def divmod(other) [div(other), self % other] end |
#dup ⇒ Object
174 175 176 |
# File 'opal/opal/corelib/numeric.rb', line 174 def dup self end |
#fdiv(other) ⇒ Object
88 89 90 |
# File 'opal/opal/corelib/numeric.rb', line 88 def fdiv(other) self.to_f / other end |
#floor ⇒ Object
92 93 94 |
# File 'opal/opal/corelib/numeric.rb', line 92 def floor to_f.floor end |
#imag ⇒ Object Also known as: imaginary
100 101 102 |
# File 'opal/opal/corelib/numeric.rb', line 100 def imag 0 end |
#integer? ⇒ Boolean
106 107 108 |
# File 'opal/opal/corelib/numeric.rb', line 106 def integer? false end |
#negative? ⇒ Boolean
170 171 172 |
# File 'opal/opal/corelib/numeric.rb', line 170 def negative? self < 0 end |
#nonzero? ⇒ Boolean
114 115 116 |
# File 'opal/opal/corelib/numeric.rb', line 114 def nonzero? zero? ? nil : self end |
#numerator ⇒ Object
118 119 120 |
# File 'opal/opal/corelib/numeric.rb', line 118 def numerator to_r.numerator end |
#polar ⇒ Object
124 125 126 |
# File 'opal/opal/corelib/numeric.rb', line 124 def polar return abs, arg end |
#positive? ⇒ Boolean
166 167 168 |
# File 'opal/opal/corelib/numeric.rb', line 166 def positive? self > 0 end |
#quo(other) ⇒ Object
128 129 130 |
# File 'opal/opal/corelib/numeric.rb', line 128 def quo(other) Opal.coerce_to!(self, Rational, :to_r) / other end |
#real ⇒ Object
132 133 134 |
# File 'opal/opal/corelib/numeric.rb', line 132 def real self end |
#rect ⇒ Object Also known as: rectangular
140 141 142 |
# File 'opal/opal/corelib/numeric.rb', line 140 def rect [self, 0] end |
#round(digits = undefined) ⇒ Object
146 147 148 |
# File 'opal/opal/corelib/numeric.rb', line 146 def round(digits = undefined) to_f.round(digits) end |
#to_c ⇒ Object
150 151 152 |
# File 'opal/opal/corelib/numeric.rb', line 150 def to_c Complex(self, 0) end |
#to_int ⇒ Object
154 155 156 |
# File 'opal/opal/corelib/numeric.rb', line 154 def to_int to_i end |
#truncate ⇒ Object
158 159 160 |
# File 'opal/opal/corelib/numeric.rb', line 158 def truncate to_f.truncate end |
#zero? ⇒ Boolean
162 163 164 |
# File 'opal/opal/corelib/numeric.rb', line 162 def zero? self == 0 end |