Project

General

Profile

Feature #6973 ยป integral.patch

regularfry (Alex Young), 09/04/2012 06:03 AM

View differences:

numeric.c
523 523
    return Qfalse;
524 524
}
525 525

  
526

  
527

  
526 528
/*
527 529
 *  call-seq:
528 530
 *     num.abs        ->  numeric
......
598 600
}
599 601

  
600 602

  
603
/*
604
 *  call-seq:
605
 *     x.integral?    -> true or false
606
 *
607
 *  Returns <code>true</code> iff <i>x</i> has an integer value.
608
 * 
609
 *     1.integral?    #=> true
610
 *     1.0.integral?  #=> true
611
 *     1.2.integral?  #=> false
612
 */
613

  
614
static VALUE
615
num_integral_p(VALUE x)
616
{
617
    VALUE y;
618

  
619
    if (rb_respond_to(x, rb_intern("to_i"))) {
620
        y = num_to_int(x);
621
        return rb_funcall(x, id_eq, 1, y);
622
    } 
623
    else {
624
        return Qfalse;
625
    }
626
}
627

  
628

  
601 629
/********************************************************************
602 630
 *
603 631
 * Document-class: Float
......
3626 3654

  
3627 3655
    rb_define_method(rb_cNumeric, "real?", num_real_p, 0);
3628 3656
    rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
3657
    rb_define_method(rb_cNumeric, "integral?", num_integral_p, 0);
3629 3658
    rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
3630 3659
    rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
3631 3660

  
test/ruby/test_integer.rb
104 104
    assert(1.integer?)
105 105
  end
106 106

  
107
  def test_integral_p
108
    assert(1.integral?)
109
  end
110

  
107 111
  def test_odd_p_even_p
108 112
    Fixnum.class_eval do
109 113
      alias odd_bak odd?
test/ruby/test_numeric.rb
87 87
    assert(!Numeric.new.integer?)
88 88
  end
89 89

  
90
  def test_integral_p
91
    assert(!Numeric.new.integral?)
92
  end
93

  
90 94
  def test_abs
91 95
    a = DummyNumeric.new
92 96
    DummyNumeric.class_eval do
......
232 236
    assert(!(1.eql?(1.0)))
233 237
    assert(!(1.eql?(2)))
234 238
  end
239

  
240
  def test_integral?
241
    assert(1.integral?, "1 is not integral")
242
    assert(1.0.integral?, "1.0 is not integral")
243
    assert(!1.2.integral?, "1.2 is integral")
244
  end
235 245
end