hpsqrt 1.6.0 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +79 -74
- data/lib/hpsqrt.rb +5 -2
- data/lib/hpsqrt/term.rb +7 -0
- data/lib/hpsqrt/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5bffde79d311243876f44ee4b1ff147ba20af8a9e4ba58350ded7c08d1b2813
|
4
|
+
data.tar.gz: 39d401c261d8db364ef0c2ccd90c670950a80c31b93601713b4c45e8fe69ebac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b115f6a582dff2eb3cbd81c4f02d1327a2399e52841d231422e2590a8f13ec36fd5db72a1079881ca3daacd7eb2e7d0ee63db9bc729e1cf442d9a2458d22a118
|
7
|
+
data.tar.gz: b40e5458449e832a20d4f251c4191c4f1264400fb476c8cc3971e8a048f73b5f91f103543bf6e443ea5f41d3dd5586fd2dbb08a6c95760a3b7229682d5c30727
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
- master: [![Build Status](https://travis-ci.org/yoshida-eth0/ruby-sqrt.svg?branch=master)](https://travis-ci.org/yoshida-eth0/ruby-sqrt)
|
1
|
+
[![Build Status](https://travis-ci.org/yoshida-eth0/ruby-sqrt.svg?branch=master)](https://travis-ci.org/yoshida-eth0/ruby-sqrt)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/hpsqrt.svg)](https://badge.fury.io/rb/hpsqrt)
|
4
3
|
|
5
|
-
|
4
|
+
# HpSqrt
|
6
5
|
|
7
6
|
High precision square root library for Ruby.
|
8
7
|
|
@@ -26,85 +25,91 @@ Or install it yourself as:
|
|
26
25
|
|
27
26
|
High precision calculation:
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
28
|
+
```ruby
|
29
|
+
require 'hpsqrt/core_ext'
|
30
|
+
|
31
|
+
p Sqrt(2)
|
32
|
+
# => 1.4142135623730951+0.0i
|
33
|
+
|
34
|
+
p Sqrt(2) ** 2
|
35
|
+
# => 2.0+0.0i
|
36
|
+
|
37
|
+
p Sqrt(3) * Sqrt(5) * Sqrt(15)
|
38
|
+
# => 15.0+0.0i
|
39
|
+
|
40
|
+
p Sqrt(1i) ** 4
|
41
|
+
# => -1.0+0.0i
|
42
|
+
|
43
|
+
p (Sqrt(7) + Sqrt(11)) * (Sqrt(7) - Sqrt(11))
|
44
|
+
# => -4.0+0.0i
|
45
|
+
```
|
45
46
|
|
46
47
|
Support operators:
|
47
48
|
|
48
|
-
+, -, *, /, %, **,
|
49
|
+
+, -, *, /, %, **, ==, <=>
|
49
50
|
|
50
51
|
Type casting:
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
53
|
+
```ruby
|
54
|
+
require 'hpsqrt/core_ext'
|
55
|
+
|
56
|
+
# to Float
|
57
|
+
p Sqrt(5).to_f
|
58
|
+
# => 2.23606797749979
|
59
|
+
|
60
|
+
# to Integer
|
61
|
+
p Sqrt(5).to_i
|
62
|
+
# => 2
|
63
|
+
|
64
|
+
# to Rational
|
65
|
+
p Sqrt(4).to_r
|
66
|
+
# => (2/1)
|
67
|
+
|
68
|
+
# to Complex
|
69
|
+
p Sqrt(-1i).to_c
|
70
|
+
# => (0.7071067811865476-0.7071067811865476i)
|
71
|
+
|
72
|
+
# to Complex real number
|
73
|
+
p Sqrt(-1i).real
|
74
|
+
# => 0.7071067811865476
|
75
|
+
|
76
|
+
# to Complex imaginary number
|
77
|
+
p Sqrt(1i).imag
|
78
|
+
# => -0.7071067811865476
|
79
|
+
|
80
|
+
p (Sqrt(5) + Sqrt(7)).expr
|
81
|
+
# => "√5 + √7"
|
82
|
+
```
|
80
83
|
|
81
84
|
Type check:
|
82
85
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
86
|
+
```ruby
|
87
|
+
require 'hpsqrt/core_ext'
|
88
|
+
|
89
|
+
# real? returns true if imaginary number is 0
|
90
|
+
p Sqrt(5).real?
|
91
|
+
# => true
|
92
|
+
p Sqrt(1i).real?
|
93
|
+
# => false
|
94
|
+
|
95
|
+
# imag? returns true if imaginary number is not 0
|
96
|
+
p Sqrt(5).imag?
|
97
|
+
# => false
|
98
|
+
p Sqrt(1i).imag?
|
99
|
+
# => true
|
100
|
+
|
101
|
+
# int? return true if after the real decimal point is 0 and imaginary number is 0
|
102
|
+
p Sqrt(2).int?
|
103
|
+
# => false
|
104
|
+
p Sqrt(4).int?
|
105
|
+
# => true
|
106
|
+
|
107
|
+
# float? return true if after the real decimal point is not 0 and imaginary number is 0
|
108
|
+
p Sqrt(2).float?
|
109
|
+
# => true
|
110
|
+
p Sqrt(4).float?
|
111
|
+
# => false
|
112
|
+
```
|
108
113
|
|
109
114
|
## Contributing
|
110
115
|
|
data/lib/hpsqrt.rb
CHANGED
@@ -23,6 +23,7 @@ class HpSqrt < Numeric
|
|
23
23
|
@cache = {}
|
24
24
|
freeze
|
25
25
|
end
|
26
|
+
private :initialize
|
26
27
|
|
27
28
|
def -@
|
28
29
|
terms = @terms.map{|t,c| [t, -c]}.to_h
|
@@ -119,9 +120,11 @@ class HpSqrt < Numeric
|
|
119
120
|
end
|
120
121
|
|
121
122
|
def <=>(other)
|
122
|
-
if
|
123
|
+
if !(Numeric===other)
|
124
|
+
nil
|
125
|
+
elsif self==other
|
123
126
|
0
|
124
|
-
elsif !self.real? || !other.
|
127
|
+
elsif !self.real? || !other.imag.zero?
|
125
128
|
nil
|
126
129
|
else
|
127
130
|
self.real <=> other.real
|
data/lib/hpsqrt/term.rb
CHANGED
@@ -5,6 +5,13 @@ class HpSqrt < Numeric
|
|
5
5
|
attr_reader :sqrt
|
6
6
|
|
7
7
|
def initialize(number: 1, sqrt: 1)
|
8
|
+
unless Numeric===number
|
9
|
+
raise TypeError, "can't convert %s into %s: %s" % [number.class.name, self.class.name, number.inspect]
|
10
|
+
end
|
11
|
+
unless Numeric===sqrt
|
12
|
+
raise TypeError, "can't convert %s into %s: %s" % [sqrt.class.name, self.class.name, sqrt.inspect]
|
13
|
+
end
|
14
|
+
|
8
15
|
@number = number
|
9
16
|
@sqrt = sqrt
|
10
17
|
freeze
|
data/lib/hpsqrt/version.rb
CHANGED