Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d59e722c554b
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: fd45d7734957
Choose a head ref
  • 4 commits
  • 3 files changed
  • 2 contributors

Commits on Apr 20, 2017

  1. Implement Feature #12245: Optional digits argument for Integer#ceil,

    Integer#floor, and Integer#truncate
    whwilder committed Apr 20, 2017
    Copy the full SHA
    b576e22 View commit details

Commits on May 18, 2017

  1. Merge branch '2.4-integer-ceil-floor-truncate' of github.com:whwilder…

    …/jruby into 2.4-integer-ceil-floor-truncate
    whwilder committed May 18, 2017
    Copy the full SHA
    451b12c View commit details

Commits on May 24, 2017

  1. Fixed some style issues:

    * changed "args" to "arg" in ceil, floor, and truncate
    * changed type of "ndigits" in RubyBignum from BigInteger to int
    whwilder committed May 24, 2017
    Copy the full SHA
    30a34a8 View commit details

Commits on Jun 13, 2017

  1. Merge pull request #4616 from whwilder/2.4-integer-ceil-floor-truncate

    [ruby 2.4] Implemented Integer#ceil, #floor, #truncate
    headius authored Jun 13, 2017
    Copy the full SHA
    fd45d77 View commit details
Showing with 140 additions and 1 deletion.
  1. +57 −0 core/src/main/java/org/jruby/RubyBignum.java
  2. +58 −0 core/src/main/java/org/jruby/RubyFixnum.java
  3. +25 −1 core/src/main/java/org/jruby/RubyInteger.java
57 changes: 57 additions & 0 deletions core/src/main/java/org/jruby/RubyBignum.java
Original file line number Diff line number Diff line change
@@ -236,6 +236,63 @@ public static BigInteger long2big(long arg) {
* ================
*/

/** rb_big_ceil
*
*/
@Override
public IRubyObject ceil(ThreadContext context, IRubyObject arg){
int ndigits = arg.convertToInteger().getIntValue();
BigInteger self = value;
if (ndigits > 0){
return convertToFloat();
} else if (ndigits == 0){
return this;
} else {
int posdigits = Math.abs(ndigits);
BigInteger exp = BigInteger.TEN.pow(posdigits);
BigInteger mod = self.mod(exp);
BigInteger res = self;
if (mod.compareTo(BigInteger.ZERO) != 0) {
res = self.add( exp.subtract(mod) );// self + (exp - (mod));
}
return newBignum(context.runtime, res);
}
}

/** rb_big_floor
*
*/
@Override
public IRubyObject floor(ThreadContext context, IRubyObject arg){
int ndigits = arg.convertToInteger().getIntValue();
BigInteger self = value;
if (ndigits > 0){
return convertToFloat();
} else if (ndigits == 0){
return this;
} else {
int posdigits = Math.abs(ndigits);
BigInteger exp = BigInteger.TEN.pow(posdigits);
BigInteger res = self.subtract(self.mod(exp));
return newBignum(context.runtime, res);
}
}

/** rb_big_truncate
*
*/
@Override
public IRubyObject truncate(ThreadContext context, IRubyObject arg){
BigInteger self = value;
if (self.compareTo(BigInteger.ZERO) == 1){
return floor(context, arg);
} else if (self.compareTo(BigInteger.ZERO) == -1){
return ceil(context, arg);
} else {
return this;
}
}

/** rb_big_digits
*
*/
58 changes: 58 additions & 0 deletions core/src/main/java/org/jruby/RubyFixnum.java
Original file line number Diff line number Diff line change
@@ -301,6 +301,63 @@ public IRubyObject times(ThreadContext context, Block block) {
}
return RubyEnumerator.enumeratorizeWithSize(context, this, "times", timesSizeFn(context.runtime));
}
/** rb_fix_ceil
*
*/
@Override
public IRubyObject ceil(ThreadContext context, IRubyObject arg){
long ndigits = arg.convertToInteger().getLongValue();
long self = getLongValue();
if (ndigits > 0) {
return convertToFloat();
} else if (ndigits == 0){
return this;
} else {
long posdigits = Math.abs(ndigits);
long exp = (long) Math.pow(10, posdigits);
long mod = (self % exp + exp) % exp;
long res = self;
if (mod != 0) {
res = self + (exp - (mod));
}
return newFixnum(context.runtime, res);
}
}

/** rb_fix_floor
*
*/
@Override
public IRubyObject floor(ThreadContext context, IRubyObject arg){
long ndigits = (arg).convertToInteger().getLongValue();
long self = getLongValue();
if (ndigits > 0) {
return convertToFloat();
} else if (ndigits == 0){
return this;
} else {
long posdigits = Math.abs(ndigits);
long exp = (long) Math.pow(10, posdigits);
long mod = (self % exp + exp) % exp;
long res = self - mod;
return newFixnum(context.runtime, res);
}
}

/** rb_fix_truncate
*
*/
@Override
public IRubyObject truncate(ThreadContext context, IRubyObject arg) {
long self = getLongValue();
if (self > 0){
return floor(context, arg);
} else if (self < 0){
return ceil(context, arg);
} else {
return this;
}
}

/** rb_fix_digits
*
@@ -350,6 +407,7 @@ public RubyArray digits(ThreadContext context, IRubyObject base) {
return res;
}


/** fix_to_s
*
*/
26 changes: 25 additions & 1 deletion core/src/main/java/org/jruby/RubyInteger.java
Original file line number Diff line number Diff line change
@@ -394,11 +394,35 @@ public IRubyObject ord(ThreadContext context) {
/** int_to_i
*
*/
@JRubyMethod(name = {"to_i", "to_int", "floor", "ceil", "truncate"})
@JRubyMethod(name = {"to_i", "to_int"})
public IRubyObject to_i() {
return this;
}

@JRubyMethod(name = "ceil")
public IRubyObject ceil(){
return this;
}

@JRubyMethod(name = "ceil", required = 1)
public abstract IRubyObject ceil(ThreadContext context, IRubyObject arg);

@JRubyMethod(name = "floor")
public IRubyObject floor(){
return this;
}

@JRubyMethod(name = "floor", required = 1)
public abstract IRubyObject floor(ThreadContext context, IRubyObject arg);

@JRubyMethod(name = "truncate")
public IRubyObject truncate(){
return this;
}

@JRubyMethod(name = "truncate", required = 1)
public abstract IRubyObject truncate(ThreadContext context, IRubyObject arg);

@Override
public IRubyObject round() {
return this;