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: 35a4547d4760
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 60cc2ef5d7a5
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Dec 18, 2017

  1. Implement Integer.sqrt

    For more information, please see feature #13219.
    nomadium committed Dec 18, 2017
    Copy the full SHA
    8d83df2 View commit details
  2. Copy the full SHA
    6182db8 View commit details
  3. Merge pull request #4904 from nomadium/implement-integer-sqrt

    Implement Integer.sqrt
    enebo authored Dec 18, 2017
    Copy the full SHA
    60cc2ef View commit details
Showing with 47 additions and 0 deletions.
  1. +13 −0 core/src/main/java/org/jruby/RubyInteger.java
  2. +34 −0 test/mri/ruby/test_integer.rb
13 changes: 13 additions & 0 deletions core/src/main/java/org/jruby/RubyInteger.java
Original file line number Diff line number Diff line change
@@ -122,6 +122,19 @@ public IRubyObject isPositive(ThreadContext context) {
return context.runtime.newBoolean(signum() > 0);
}

/* =============
* Class Methods
* =============
*/

/** rb_int_s_isqrt
*
*/
@JRubyMethod(meta = true)
public static IRubyObject sqrt(ThreadContext context, IRubyObject self, IRubyObject arg) {
return RubyMath.sqrt(context, self, arg.convertToInteger()).convertToInteger();
}

/* ================
* Instance Methods
* ================
34 changes: 34 additions & 0 deletions test/mri/ruby/test_integer.rb
Original file line number Diff line number Diff line change
@@ -464,4 +464,38 @@ def to_int
end
assert_equal([0, 1], 10.digits(o))
end

def test_square_root
assert_raise(TypeError) {Integer.sqrt("x")}
assert_raise(Math::DomainError) {Integer.sqrt(-1)}
assert_equal(0, Integer.sqrt(0))
(1...4).each {|i| assert_equal(1, Integer.sqrt(i))}
(4...9).each {|i| assert_equal(2, Integer.sqrt(i))}
(9...16).each {|i| assert_equal(3, Integer.sqrt(i))}
(1..40).each do |i|
mesg = "10**#{i}"
s = Integer.sqrt(n = 10**i)
if i.even?
assert_equal(10**(i/2), Integer.sqrt(n), mesg)
else
assert_include((s**2)...(s+1)**2, n, mesg)
end
end
50.step(400, 10) do |i|
exact = 10**(i/2)
x = 10**i
assert_equal(exact, Integer.sqrt(x), "10**#{i}")
assert_equal(exact, Integer.sqrt(x+1), "10**#{i}+1")
assert_equal(exact-1, Integer.sqrt(x-1), "10**#{i}-1")
end

bug13440 = '[ruby-core:80696] [Bug #13440]'
failures = []
0.step(to: 50, by: 0.05) do |i|
n = (10**i).to_i
root = Integer.sqrt(n)
failures << n unless root*root <= n && (root+1)*(root+1) > n
end
assert_empty(failures, bug13440)
end
end