Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/src/main/java/org/jruby/RubyFixnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -1452,8 +1452,7 @@ public IRubyObject sqrt(ThreadContext context) {
}

long n = value;
// FIXME: this is obviously not super efficient, but floorSqrt(long) did not pass tests
long sq = floorSqrt(BigInteger.valueOf(n)).longValue();
long sq = floorSqrt(n);

return RubyFixnum.newFixnum(runtime, sq);
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/org/jruby/RubyInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ public static IRubyObject sqrt(ThreadContext context, IRubyObject self, IRubyObj

public abstract IRubyObject sqrt(ThreadContext context);

// floorSqrt :: unsigned long -> unsigned int
// floorSqrt :: unsigned long -> unsigned long
// Gives the exact floor of the square root of x, treated as unsigned.
// Public domain code from http://www.codecodex.com/wiki/Calculate_an_integer_square_root
public static final int floorSqrt(final long x) {
if ((x & 0xfff0000000000000L) == 0L) return (int) StrictMath.sqrt(x);
final long result = (long) StrictMath.sqrt(2.0d*(x >>> 1));
return result*result - x > 0L ? (int) result - 1 : (int) result;
public static final long floorSqrt(final long x) {
if ((x & 0xfff0000000000000L) == 0L) return (long) StrictMath.sqrt(x);
final long result = (long) StrictMath.sqrt(2.0d*(x >>> 1));
return result*result - x > 0L ? (long) result - 1 : (long) result;
}

// floorSqrt :: BigInteger -> BigInteger
Expand Down