roots 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/roots.rb +15 -5
  3. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 888f6b068017a2b3b43dff7c8a2ee62d2b389f02
4
- data.tar.gz: 1607eecadf5c42807d873e9f0bb755987750ec59
3
+ metadata.gz: bd63244d7b73fd41061b8ba024846116fcacd95e
4
+ data.tar.gz: 23fd715e6b991c71adcd74043f18e2ba4f165ba2
5
5
  SHA512:
6
- metadata.gz: d4d2c80e2227ab29c92d7e496d18b47fbe45f68de42efab37584c4bbe9076b0426fc2eee6f67162a10238c3aa3c192400a27a119ee36647a49a9eba7d68153e8
7
- data.tar.gz: 04bc29ac7b446db4f8672fc65b238e1dfb27aaa1ee06ac12fddb2ddc5888f59c073dcdca687a6a3b1716bbe4c3bc06515c57fc1b26ca871d29a60bd0c7341a91
6
+ metadata.gz: daf32159f9103e28e2c5bfa1c9330869a677e0d586542f8e8ca1ac74b01ec414556a521c35cab7555026d3088fff5c3d98173eb87b72e23692693b3282191f2c
7
+ data.tar.gz: abb72e384abeda80bb8ef6c4595785961e87a73217635d8c02e57749e33226ec305aa9b0eb75c43e31a8699ae4dba2cdeb29398bdea7550ae222895a588d777b
@@ -188,7 +188,7 @@ module Roots
188
188
  Complex(x.real.round(Roots.digits_to_show), x.imag.round(Roots.digits_to_show))
189
189
  end
190
190
  end
191
-
191
+
192
192
  # Mixin 'root' and 'roots' as methods for all number classes.
193
193
  class Numeric; include Roots end
194
194
 
@@ -197,15 +197,25 @@ module IntRoots
197
197
  return nil if self < 0 && n.even?
198
198
  raise "root n is < 2 or not an Integer" unless n.is_a?(Integer) && n > 1
199
199
  num = self.abs
200
- root, bitn_mask = 0, 1 << (num.bit_length/n + 2)
201
- until (bitn_mask >>= 1) == 0
200
+ root = bitn_mask = 1 << b = (num.bit_length - 1)/n
201
+ numb = 1 << b*n # make initial numb = root**n >= num
202
+ until ((bitn_mask >>= 1) == 0) || numb == num
202
203
  root |= bitn_mask
203
- root ^= bitn_mask if root**n > num
204
+ root ^= bitn_mask if (numb = root**n) > num
204
205
  end
205
206
  root *= (self < 0 ? -1 : 1)
206
207
  end
207
208
 
208
- def iroot2; irootn(2) end
209
+ alias iroot irootn # to provide more syntactic choice
210
+
211
+ def iroot2 # Newton's method version used in Ruby for Integer#sqrt
212
+ return nil if (n = self) < 0
213
+ return n if n < 2
214
+ b = n.bit_length
215
+ x = 1 << (b-1)/2 | n >> (b/2 + 1) # optimum initial root estimate
216
+ while (t = n / x) < x; x = ((x + t) >> 1) end
217
+ x
218
+ end
209
219
  end
210
220
 
211
221
  # Mixin 'iroot2' and 'irootn(n)' methods for class Integer
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roots
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jabari Zakiya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-21 00:00:00.000000000 Z
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,8 +39,8 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  description: 'For val (real/complex) and root n: val.root(n,[1-n]) and val.roots(n,
42
- [opt]); For Integer ival: ival.iroot2 and ival.irootn(n); Roots.digits_to_show to
43
- see/change number of digts to show'
42
+ [opt]); For Integer ival: ival.iroot2, and ival.root(n) or ival.irootn(n); Roots.digits_to_show
43
+ to see/change number of digts to show'
44
44
  email:
45
45
  - jzakiya@gmail.com
46
46
  executables: []
@@ -68,8 +68,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  version: '0'
69
69
  requirements: []
70
70
  rubyforge_project:
71
- rubygems_version: 2.6.10
71
+ rubygems_version: 2.6.11
72
72
  signing_key:
73
73
  specification_version: 4
74
- summary: two methods 'root' and 'roots' to compute all n roots of real/complex numbers
74
+ summary: methods to compute all n roots of real/complex numbers, and real integer
75
+ roots
75
76
  test_files: []