Class: Numeric

Inherits:
Object show all
Includes:
Comparable
Defined in:
mruby/mrblib/numeric.rb,
mruby/src/numeric.c,
mruby/mrbgems/mruby-complex/mrblib/complex.rb,
mruby/mrbgems/mruby-rational/mrblib/rational.rb,
mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb

Overview

Numeric

ISO 15.2.7

Direct Known Subclasses

Complex, Float, Integer, Rational

Instance Method Summary collapse

Methods included from Comparable

#<, #<=, #==, #>, #>=, #between?, #clamp

Instance Method Details

#+@Object

Returns the receiver simply.

ISO 15.2.7.4.1



11
12
13
# File 'mruby/mrblib/numeric.rb', line 11

def +@
  self
end

#-@Object

Returns the receiver’s value, negated.

ISO 15.2.7.4.2



19
20
21
# File 'mruby/mrblib/numeric.rb', line 19

def -@
  0 - self
end

#absObject

Returns the absolute value of the receiver.

ISO 15.2.7.4.3



27
28
29
30
31
32
33
# File 'mruby/mrblib/numeric.rb', line 27

def abs
  if self < 0
    -self
  else
    self
  end
end

#allbits?(mask) ⇒ Boolean

call-seq:

  int.allbits?(mask)  ->  true or false

Returns +true+ if all bits of <code>+int+ & +mask+</code> are 1.

Returns:

  • (Boolean)


56
57
58
# File 'mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 56

def allbits?(mask)
  (self & mask) == mask
end

#anybits?(mask) ⇒ Boolean

call-seq:

  int.anybits?(mask)  ->  true or false

Returns +true+ if any bits of <code>+int+ & +mask+</code> are 1.

Returns:

  • (Boolean)


66
67
68
# File 'mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 66

def anybits?(mask)
  (self & mask) != 0
end

#eql?(numeric) ⇒ Boolean

Returns true if num and numeric are the same type and have equal values.

1 == 1.0          #=> true
1.eql?(1.0)       #=> false
(1.0).eql?(1.0)   #=> true

Returns:

  • (Boolean)


557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'mruby/src/numeric.c', line 557

static mrb_value
num_eql(mrb_state *mrb, mrb_value x)
{
  mrb_value y = mrb_get_arg1(mrb);

#ifdef MRB_USE_BIGINT
  if (mrb_bigint_p(x)) {
    return mrb_bool_value(mrb_bint_cmp(mrb, x, y) == 0);
  }
#endif
#ifndef MRB_NO_FLOAT
  if (mrb_float_p(x)) {
    if (!mrb_float_p(y)) return mrb_false_value();
    return mrb_bool_value(mrb_float(x) == mrb_float(y));
  }
#endif
  if (mrb_integer_p(x)) {
    if (!mrb_integer_p(y)) return mrb_false_value();
    return mrb_bool_value(mrb_integer(x) == mrb_integer(y));
  }
  return mrb_bool_value(mrb_equal(mrb, x, y));
}

#finite?Boolean

15.2.7

Returns:

  • (Boolean)


2151
2152
2153
2154
2155
# File 'mruby/src/numeric.c', line 2151

static mrb_value
num_finite_p(mrb_state *mrb, mrb_value self)
{
  return mrb_true_value();
}

#infinite?Boolean

Returns:

  • (Boolean)


2157
2158
2159
2160
2161
# File 'mruby/src/numeric.c', line 2157

static mrb_value
num_infinite_p(mrb_state *mrb, mrb_value self)
{
  return mrb_false_value();
}

#negative?Boolean

call-seq:

  negative? -> true or false

Returns +true+ if +self+ is less than 0, +false+ otherwise.

Returns:

  • (Boolean)


46
47
48
# File 'mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 46

def negative?
  self < 0
end

#nobits?(mask) ⇒ Boolean

call-seq:

  int.nobits?(mask)  ->  true or false

Returns +true+ if no bits of <code>+int+ & +mask+</code> are 1.

Returns:

  • (Boolean)


76
77
78
# File 'mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 76

def nobits?(mask)
  (self & mask) == 0
end

#nonzero?Boolean

call-seq:

  nonzero?  ->  self or nil

Returns +self+ if +self+ is not a zero value, +nil+ otherwise;
uses method <tt>zero?</tt> for the evaluation.

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 22

def nonzero?
  if self == 0
    nil
  else
    self
  end
end

#positive?Boolean

call-seq:

  positive? -> true or false

Returns +true+ if +self+ is greater than 0, +false+ otherwise.

Returns:

  • (Boolean)


36
37
38
# File 'mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 36

def positive?
  self > 0
end

#to_cObject



79
80
81
# File 'mruby/mrbgems/mruby-complex/mrblib/complex.rb', line 79

def to_c
  Complex(self, 0)
end

#to_rObject



12
13
14
# File 'mruby/mrbgems/mruby-rational/mrblib/rational.rb', line 12

def to_r
  Rational(self, 1)
end

#zero?Boolean

call-seq:

  zero? -> true or false

Returns +true+ if +zero+ has a zero value, +false+ otherwise.

Of the Core and Standard Library classes,
only Rational and Complex use this implementation.

Returns:

  • (Boolean)


11
12
13
# File 'mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 11

def zero?
  self == 0
end