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(ndigits = 0) ⇒ 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
- #finite? ⇒ Boolean
- #floor(ndigits = 0) ⇒ Object
- #i ⇒ Object
- #imag ⇒ Object (also: #imaginary)
- #infinite? ⇒ Boolean
- #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(ndigits = 0) ⇒ Object
- #zero? ⇒ Boolean
Methods included from Comparable
#<, #<=, #==, #>, #>=, #between?, #clamp
Instance Method Details
#%(other) ⇒ Object Also known as: modulo
| 44 45 46 | # File 'opal/opal/corelib/numeric.rb', line 44 def %(other) self - other * div(other) end | 
#+@ ⇒ Object
| 36 37 38 | # File 'opal/opal/corelib/numeric.rb', line 36 def +@ self end | 
#-@ ⇒ Object
| 40 41 42 | # File 'opal/opal/corelib/numeric.rb', line 40 def -@ 0 - self end | 
#<=>(other) ⇒ Object
| 28 29 30 31 32 33 34 | # File 'opal/opal/corelib/numeric.rb', line 28 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 | # File 'opal/opal/corelib/numeric.rb', line 14 def __coerced__(method, other) if other.respond_to?(:coerce) a, b = other.coerce(self) a.__send__ method, b else case method when :+, :-, :*, :/, :%, :&, :|, :^, :** raise TypeError, "#{other.class} can't be coerced into Numeric" when :>, :>=, :<, :<=, :<=> raise ArgumentError, "comparison of #{self.class} with #{other.class} failed" end end end | 
#abs ⇒ Object Also known as: magnitude
| 48 49 50 | # File 'opal/opal/corelib/numeric.rb', line 48 def abs self < 0 ? -self : self end | 
#abs2 ⇒ Object
| 52 53 54 | # File 'opal/opal/corelib/numeric.rb', line 52 def abs2 self * self end | 
#angle ⇒ Object Also known as: arg
| 56 57 58 | # File 'opal/opal/corelib/numeric.rb', line 56 def angle self < 0 ? Math::PI : 0 end | 
#ceil(ndigits = 0) ⇒ Object
| 62 63 64 | # File 'opal/opal/corelib/numeric.rb', line 62 def ceil(ndigits = 0) to_f.ceil(ndigits) end | 
#clone(freeze: true) ⇒ Object
| 176 177 178 | # File 'opal/opal/corelib/numeric.rb', line 176 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
| 66 67 68 | # File 'opal/opal/corelib/numeric.rb', line 66 def conj self end | 
#denominator ⇒ Object
| 72 73 74 | # File 'opal/opal/corelib/numeric.rb', line 72 def denominator to_r.denominator end | 
#div(other) ⇒ Object
| 76 77 78 79 80 | # File 'opal/opal/corelib/numeric.rb', line 76 def div(other) raise ZeroDivisionError, 'divided by o' if other == 0 (self / other).floor end | 
#divmod(other) ⇒ Object
| 82 83 84 | # File 'opal/opal/corelib/numeric.rb', line 82 def divmod(other) [div(other), self % other] end | 
#dup ⇒ Object
| 172 173 174 | # File 'opal/opal/corelib/numeric.rb', line 172 def dup self end | 
#fdiv(other) ⇒ Object
| 86 87 88 | # File 'opal/opal/corelib/numeric.rb', line 86 def fdiv(other) to_f / other end | 
#floor(ndigits = 0) ⇒ Object
| 90 91 92 | # File 'opal/opal/corelib/numeric.rb', line 90 def floor(ndigits = 0) to_f.floor(ndigits) end | 
#imag ⇒ Object Also known as: imaginary
| 98 99 100 | # File 'opal/opal/corelib/numeric.rb', line 98 def imag 0 end | 
#infinite? ⇒ Boolean
| 184 185 186 | # File 'opal/opal/corelib/numeric.rb', line 184 def infinite? nil end | 
#integer? ⇒ Boolean
| 104 105 106 | # File 'opal/opal/corelib/numeric.rb', line 104 def integer? false end | 
#negative? ⇒ Boolean
| 168 169 170 | # File 'opal/opal/corelib/numeric.rb', line 168 def negative? self < 0 end | 
#nonzero? ⇒ Boolean
| 112 113 114 | # File 'opal/opal/corelib/numeric.rb', line 112 def nonzero? zero? ? nil : self end | 
#numerator ⇒ Object
| 116 117 118 | # File 'opal/opal/corelib/numeric.rb', line 116 def numerator to_r.numerator end | 
#polar ⇒ Object
| 122 123 124 | # File 'opal/opal/corelib/numeric.rb', line 122 def polar [abs, arg] end | 
#positive? ⇒ Boolean
| 164 165 166 | # File 'opal/opal/corelib/numeric.rb', line 164 def positive? self > 0 end | 
#quo(other) ⇒ Object
| 126 127 128 | # File 'opal/opal/corelib/numeric.rb', line 126 def quo(other) Opal.coerce_to!(self, Rational, :to_r) / other end | 
#real ⇒ Object
| 130 131 132 | # File 'opal/opal/corelib/numeric.rb', line 130 def real self end | 
#rect ⇒ Object Also known as: rectangular
| 138 139 140 | # File 'opal/opal/corelib/numeric.rb', line 138 def rect [self, 0] end | 
#round(digits = undefined) ⇒ Object
| 144 145 146 | # File 'opal/opal/corelib/numeric.rb', line 144 def round(digits = undefined) to_f.round(digits) end | 
#to_c ⇒ Object
| 148 149 150 | # File 'opal/opal/corelib/numeric.rb', line 148 def to_c Complex(self, 0) end | 
#to_int ⇒ Object
| 152 153 154 | # File 'opal/opal/corelib/numeric.rb', line 152 def to_int to_i end | 
#truncate(ndigits = 0) ⇒ Object
| 156 157 158 | # File 'opal/opal/corelib/numeric.rb', line 156 def truncate(ndigits = 0) to_f.truncate(ndigits) end | 
#zero? ⇒ Boolean
| 160 161 162 | # File 'opal/opal/corelib/numeric.rb', line 160 def zero? self == 0 end |