roots 2.1.0 → 2.2.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 +9 -10
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd63244d7b73fd41061b8ba024846116fcacd95e
4
- data.tar.gz: 23fd715e6b991c71adcd74043f18e2ba4f165ba2
3
+ metadata.gz: aa49c39432ae20839016d0beb515b8f623cc9d00
4
+ data.tar.gz: 41c2a43899fc9f13fa4370bc289ea8f8e6c2f568
5
5
  SHA512:
6
- metadata.gz: daf32159f9103e28e2c5bfa1c9330869a677e0d586542f8e8ca1ac74b01ec414556a521c35cab7555026d3088fff5c3d98173eb87b72e23692693b3282191f2c
7
- data.tar.gz: abb72e384abeda80bb8ef6c4595785961e87a73217635d8c02e57749e33226ec305aa9b0eb75c43e31a8699ae4dba2cdeb29398bdea7550ae222895a588d777b
6
+ metadata.gz: 409ff9cda40258bca3e9733a4b0ff240256df9f12e3fd542e872a0765a1fc1607f437df7b998e084c3dfa514cc42ceeeb665eeabf06584ff4a9299950a55778a
7
+ data.tar.gz: '089bc4812353949dbc022dea855015f8dc813644c9e94de48e691c26b51eb156482751f3a9402361f29542e61897704353f7745d53109675214cb01576df7a80'
@@ -193,26 +193,25 @@ end
193
193
  class Numeric; include Roots end
194
194
 
195
195
  module IntRoots
196
- def irootn(n)
196
+ def irootn(n) # Newton's method for nth root
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
- num = self.abs
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
203
- root |= bitn_mask
204
- root ^= bitn_mask if (numb = root**n) > num
199
+ return self if self == 0 || (self == -1 && n.odd?)
200
+ num = self.abs
201
+ e, x = n-1, 1 << (num.bit_length-1)/n + 1
202
+ while (t = (e * x + num / x ** e)/n) < x
203
+ x = t
205
204
  end
206
- root *= (self < 0 ? -1 : 1)
205
+ x *= self < 0 ? -1 : 1
207
206
  end
208
-
207
+
209
208
  alias iroot irootn # to provide more syntactic choice
210
209
 
211
210
  def iroot2 # Newton's method version used in Ruby for Integer#sqrt
212
211
  return nil if (n = self) < 0
213
212
  return n if n < 2
214
213
  b = n.bit_length
215
- x = 1 << (b-1)/2 | n >> (b/2 + 1) # optimum initial root estimate
214
+ x = 1 << (b-1)/2 | n >> (b/2 + 1) # optimum initial root estimat
216
215
  while (t = n / x) < x; x = ((x + t) >> 1) end
217
216
  x
218
217
  end
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.1.0
4
+ version: 2.2.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-06-01 00:00:00.000000000 Z
11
+ date: 2017-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,7 +39,7 @@ 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.root(n) or ival.irootn(n); Roots.digits_to_show
42
+ [opt]); For Integer ival: ival.iroot2, and ival.iroot(n) or ival.irootn(n); Roots.digits_to_show
43
43
  to see/change number of digts to show'
44
44
  email:
45
45
  - jzakiya@gmail.com
@@ -50,7 +50,7 @@ files:
50
50
  - lib/roots.rb
51
51
  homepage: https://github.com/jzakiya/roots
52
52
  licenses:
53
- - GPLv2+
53
+ - GPL-2.0+
54
54
  metadata: {}
55
55
  post_install_message:
56
56
  rdoc_options: []