roots 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/roots.rb +47 -2
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9dff74a275635f6271bef7cf4055be31311e97e6
4
- data.tar.gz: 62db9885cfc473e764e879be0cdb626c207fcfbb
3
+ metadata.gz: 713ce2176c4046036288cae2d0b5bddbe07cd170
4
+ data.tar.gz: 1ea2b5fd2e8ccb26520f175a7f9fc10398ba5681
5
5
  SHA512:
6
- metadata.gz: '0806626fff63d16ecca34ffeaf2abe565fe67f03d16f36ecc4fe889ae2c64ed122515cfb33ccce363096eff5b9d0b8e39aeb47f7b41862f3a19ae4be6bfcfce7'
7
- data.tar.gz: 551ac3a4c43eadb96065b1006feb1f667f76825d169368b29caa571ebeb7bd722bfe52f199cbd21fdd41920d678d7b6ab9a5d4c726913f4940837d10afda6a34
6
+ metadata.gz: ae80ce3d2898c4cb29e0227b3ba852df316b3465bf238505213932082e5237d855073a93f5d5c75fb7194c2f3fe546c956c3db11dcbc4f91c7072de88559b4ed
7
+ data.tar.gz: 04ad6fda5f12885b138aab35f530d8427dd0a86735f209d10d7fc3b0fb8c80e6f5da6dbb95fbf8fa92199c4e9f0064dea769f4c0a76b1430ae3ebbc962896962
data/lib/roots.rb CHANGED
@@ -105,6 +105,32 @@ These all should be 0.0, which causes incorrect root values there.
105
105
  I 'fix' these errors by setting aliases of sin|cos to 0.0 if the
106
106
  absolute value of the other function equals 1.0 so they produce
107
107
  the correct root values.
108
+
109
+ Added: 2017-2-20
110
+
111
+ Module 'IntRoots' provides the two methods 'iroot2' and 'irootn(n)'
112
+ to find the real squareroot and nth roots of arbitrary sized integers.
113
+
114
+ Use syntax: ival.iroot2
115
+ Return the largest Integer +root+ value such that root**2 <= ival
116
+ A negative ival will result in 'nil' being returned.
117
+
118
+ 9.iroot2 => 3
119
+ -9.iroot2 => nil
120
+
121
+ 120.iroot2 => 10
122
+ 121.iroot2 => 11
123
+
124
+ Use syntax: ival.irootn(n), where n is an Integer > 1
125
+ Return the largest Integer +root+ value such that root**n <= ival
126
+ A negative ival for an even n value will result in 'nil' being returned.
127
+
128
+ 81.irootn(2) => 9
129
+ 81.irootn(3) => 4
130
+ 81.irootn(4) => 3
131
+
132
+ -81.irootn(3) => -4
133
+ -81.irootn(4) => nil
108
134
  =end
109
135
 
110
136
  # file roots.rb
@@ -161,7 +187,26 @@ module Roots
161
187
  x = mag*Complex(cosine(angle_n),sine(angle_n))
162
188
  Complex(x.real.round(Roots.digits_to_show), x.imag.round(Roots.digits_to_show))
163
189
  end
190
+ end
191
+
192
+ # Mixin 'root' and 'roots' as methods for all number classes.
193
+ class Numeric; include Roots end
194
+
195
+ module IntRoots
196
+ def irootn(n)
197
+ return nil if self < 0 && n.even?
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 = 0, 1 << (num.bit_length/n + 2)
201
+ until (bitn_mask >>= 1) == 0
202
+ root |= bitn_mask
203
+ root ^= bitn_mask if root**n > num
204
+ end
205
+ root *= (self < 0 ? -1 : 1)
206
+ end
207
+
208
+ def iroot2; irootn(2) end
164
209
  end
165
210
 
166
- # Mixin 'root' and 'roots' as methods for all number classes.
167
- class Numeric; include Roots end
211
+ # Mixin 'iroot2' and 'irootn(n)' methods for class Integer
212
+ class Integer; include IntRoots 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: 1.1.1
4
+ version: 2.0.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: 2016-07-01 00:00:00.000000000 Z
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,9 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  version: '0'
68
68
  requirements: []
69
69
  rubyforge_project:
70
- rubygems_version: 2.5.1
70
+ rubygems_version: 2.6.10
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: two methods 'root' and 'roots' to compute all n roots of real/complex numbers
74
74
  test_files: []
75
- has_rdoc: