roots 1.0.2 → 1.1.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 +37 -4
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18862dba5718e378faf54cb34b1f1a24c45e457d
4
- data.tar.gz: 0365b694e904c3abf2cabd692099e546df4f52af
3
+ metadata.gz: 621674bdf8c59b430f02c803c10a5800e25b2614
4
+ data.tar.gz: bad4f31ed0d85622a2cfc4b62c574f9f36d51d5b
5
5
  SHA512:
6
- metadata.gz: a99f7fcd3269ac65c3f8960d2335cbad9f85c40f72fc1e5746a74611ad0291179ed9d5ad61c20f9f8077ad6e2626f0fee9ffc519b12990ce446a491aa58cf214
7
- data.tar.gz: 5033fd32270c400ef0b7babfa80adedc9603f097e47cf84e175b666b32d3946f316203853002e87b4888944ce1f189da7cb80821fd5830dbd723bd9d6afd3407
6
+ metadata.gz: 6bca29dc614a4096eedfad0a94222517086517a9ab9605c247183d404bdd498004789a681e7139133e7d404fb4e8f9e25589e199c4bb64ce99bc81327bb9117a
7
+ data.tar.gz: ce3b88891a24f6c3d5f0a46e58db2271c7e686708fc98299c227d4ff67fd3d6d8e38239b74a65605b4277e3f9ca0445b5d9bc995ca4f094b7933e33315b3f31c
data/lib/roots.rb CHANGED
@@ -47,7 +47,36 @@ For Ruby 1.9.x can also use symbol as option: 384.roots 4,:r
47
47
  Can ask: How many complex roots of x: x.roots(n,'c').size
48
48
  What's the 3rd 5th root of (4+9i): Complex(4,9).root(5,3)
49
49
 
50
+ Use syntax: Roots.digits_to_show
51
+ With gem version 1.1.0 this method was added to allow users to set
52
+ and see the number of decimal digits displayed. If no input is
53
+ given, the number of digits previously set is shown. The default
54
+ value is 8. Providing an input value sets the number of digits to
55
+ display. An input value < 1 will be set to a value of 1, to
56
+ display at least one decimal digit. An input greater than the
57
+ maximum digits shown for a given Ruby will display that maximum
58
+
59
+ Roots.digits_to_show => 8
60
+
61
+ 10.root 5 => 1.58489319
62
+
63
+ Roots.digits_to_show 11 => 11
64
+
65
+ 10.root 5 => 1.58489319246
66
+
67
+ Roots.digits_to_show 16 => 16
68
+
69
+ 10.root 5 => 1.5848931924611136
70
+
71
+ Roots.digits_to_show 17 => 17
72
+
73
+ 10.root 5 => 1.5848931924611136
74
+
75
+ Roots.digits_to_show 0 => 1
76
+
77
+ 10.root 5 => 1.6
50
78
  ---------------
79
+
51
80
  Mathematical Foundations
52
81
 
53
82
  For complex number (x+iy) = a*e^(i*arg) = a*[cos(arg) + i*sin(arg)]
@@ -91,8 +120,8 @@ module Roots
91
120
  mag = abs**n**-1 ; theta = arg/n ; delta = 2*PI/n
92
121
  return rootn(mag,theta,delta,k>1 ? k-1:0) if kind_of?(Complex)
93
122
  return rootn(mag,theta,delta,k-1) if k>0 # kth root of n for any real
94
- return mag if self > 0 # pos real default
95
- return -mag if n&1 == 1 # neg real default, n odd
123
+ return mag.round(Roots.digits_to_show) if self > 0 # pos real default
124
+ return -mag.round(Roots.digits_to_show) if n&1 == 1 # neg real default, n odd
96
125
  return rootn(mag,theta) # neg real default, n even, 1st ccw root
97
126
  end
98
127
 
@@ -123,12 +152,16 @@ module Roots
123
152
  # Alias sin|cos to fix C lib errors to get 0.0 values for X|Y axis angles.
124
153
  def sine(x); cos(x).abs == 1 ? 0 : sin(x) end
125
154
  def cosine(x); sin(x).abs == 1 ? 0 : cos(x) end
155
+ def self.digits_to_show(n = @digits_to_show || 8)
156
+ @digits_to_show = n < 1 ? 1 : n
157
+ end
126
158
 
127
159
  def rootn(mag,theta,delta=0,k=0) # root k of n of real|complex
128
160
  angle_n = theta + k*delta
129
- mag*Complex(cosine(angle_n),sine(angle_n))
161
+ x = mag*Complex(cosine(angle_n),sine(angle_n))
162
+ Complex(x.real.round(Roots.digits_to_show), x.imag.round(Roots.digits_to_show))
130
163
  end
131
164
  end
132
165
 
133
- # Mixin 'root' and 'roots' as methods for all number classes..
166
+ # Mixin 'root' and 'roots' as methods for all number classes.
134
167
  class Numeric; include Roots 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.0.2
4
+ version: 1.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: 2015-07-25 00:00:00.000000000 Z
11
+ date: 2016-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  version: '0'
68
68
  requirements: []
69
69
  rubyforge_project:
70
- rubygems_version: 2.4.6
70
+ rubygems_version: 2.5.1
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