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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9e8c696801667af06718bf68800c0bc364372971f6e9708fa3bc99f878bd37a
4
- data.tar.gz: '0813db15d0b31c395c14060af5947830fcd5d6c2a95627a83cc4cd9671414de6'
3
+ metadata.gz: d5bffde79d311243876f44ee4b1ff147ba20af8a9e4ba58350ded7c08d1b2813
4
+ data.tar.gz: 39d401c261d8db364ef0c2ccd90c670950a80c31b93601713b4c45e8fe69ebac
5
5
  SHA512:
6
- metadata.gz: 80eda9901b98547e6aef9813d3b737f79de8af71577220c7f4bc34f8c8cef02e4d00dfcb3ada2bd199fd90cf5bf6049de8d7a838dad176fdce3e4d1cfd4f26b4
7
- data.tar.gz: dbfff2f96f8889fb21a97847e8b318ad77e2216940ffcca57ec41bfd58767e1c4123df03e9eaf90f92bf4b8ca7bad7ff0e6558c20c9c8620cb02b6489860c5f3
6
+ metadata.gz: b115f6a582dff2eb3cbd81c4f02d1327a2399e52841d231422e2590a8f13ec36fd5db72a1079881ca3daacd7eb2e7d0ee63db9bc729e1cf442d9a2458d22a118
7
+ data.tar.gz: b40e5458449e832a20d4f251c4191c4f1264400fb476c8cc3971e8a048f73b5f91f103543bf6e443ea5f41d3dd5586fd2dbb08a6c95760a3b7229682d5c30727
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
- # HpSqrt
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
- ## Overview
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
- 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
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
- require 'hpsqrt/core_ext'
53
-
54
- # to Float
55
- p Sqrt(5).to_f
56
- # => 2.23606797749979
57
-
58
- # to Integer
59
- p Sqrt(5).to_i
60
- # => 2
61
-
62
- # to Rational
63
- p Sqrt(4).to_r
64
- # => (2/1)
65
-
66
- # to Complex
67
- p Sqrt(-1i).to_c
68
- # => (0.7071067811865476-0.7071067811865476i)
69
-
70
- # to Complex real number
71
- p Sqrt(-1i).real
72
- # => 0.7071067811865476
73
-
74
- # to Complex imaginary number
75
- p Sqrt(1i).imag
76
- # => -0.7071067811865476
77
-
78
- p (Sqrt(5) + Sqrt(7)).expr
79
- # => "√5 + 7"
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
- require 'hpsqrt/core_ext'
84
-
85
- # real? returns true if imaginary number is 0
86
- p Sqrt(5).real?
87
- # => true
88
- p Sqrt(1i).real?
89
- # => false
90
-
91
- # imag? returns true if imaginary number is not 0
92
- p Sqrt(5).imag?
93
- # => false
94
- p Sqrt(1i).imag?
95
- # => true
96
-
97
- # int? return true if after the real decimal point is 0 and imaginary number is 0
98
- p Sqrt(2).int?
99
- # => false
100
- p Sqrt(4).int?
101
- # => true
102
-
103
- # float? return true if after the real decimal point is not 0 and imaginary number is 0
104
- p Sqrt(2).float?
105
- # => true
106
- p Sqrt(4).float?
107
- # => false
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 self==other
123
+ if !(Numeric===other)
124
+ nil
125
+ elsif self==other
123
126
  0
124
- elsif !self.real? || !other.real?
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
@@ -1,3 +1,3 @@
1
1
  class HpSqrt < Numeric
2
- VERSION = "1.6.0"
2
+ VERSION = "1.7.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hpsqrt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshida Tetsuya