vector_number 0.2.4 → 0.2.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8a4ba56677fa1eab6fc6dba7e8a7cc9d4afd769d9d0a4186ee51b2f400f2bab
4
- data.tar.gz: 57f4d03d9495c2f6e98afb25008f659f63cf5cf0723664f1a3e7633568a8a179
3
+ metadata.gz: 60948b232b4ab095c808a54d3ba32748893db4c660a2ab553ad216d677906814
4
+ data.tar.gz: b3ff8e996c8617c7c6a5c0592f18480d78cd5f7c520ab47194ae965eb21406a5
5
5
  SHA512:
6
- metadata.gz: 020ae7c79a25d4d18b6c2798d1c903e5f3a9699f8a2822ad7617ef3ada0ed4e2ef158a324548dfdc68de1a2bd70789487929d8d68def9b47133e077a4654d277
7
- data.tar.gz: d34fb46be17a6cec0916257afcaccb842cdd24b7aa055377c1dbd343e6ffd90295df2a9725bf549709f6997276f88e1c1766f079c4848dc002b645180678e912
6
+ metadata.gz: d40eb5b5c6de69bd5db2579e9f6eb9f872abd67b68b49983c7d5c23d4a2726fbbaad128819631baf8e21e094d77986a4c69d53bcc408e2f838faffed9875c2bc
7
+ data.tar.gz: 4656e1b5a136191bede67f3c3203b3a43110e4c9700328b61d3802abfca3805ff39d7718d774a70eb0e0c20ea0a9e687fbdf6ce19594171148998b95ffcda9bd
data/CHANGELOG.md CHANGED
@@ -6,6 +6,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
8
  ## [Next]
9
+ ## [v0.2.6] — 2025-04-30
10
+
11
+ **Added**
12
+ - Add `#div`, `#%` (aliased as `#modulo`), `#divmod` and `#remainder` methods.
13
+ - Add `#quo` alias to `#/`.
14
+
15
+ **Fixed**
16
+ - `#/`, `#fdiv` as well as new division methods now properly check for division by zero.
17
+ VectorNumber does not support this as not all Numeric classes do.
18
+
19
+ ## [v0.2.5] — 2025-02-26
20
+
21
+ Technical update after release to rubygems.org.
22
+ README was updated to reflect this change.
23
+
9
24
  ## [v0.2.4] — 2025-02-26
10
25
 
11
26
  **Added**
@@ -17,7 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
17
32
  - Make `VectorNumber.new` accept options when initializing from a VectorNumber
18
33
  instead of only copying. Options will be merged.
19
34
  - Remove `Initializing` module, move its methods to the actual class.
20
- - Updated development gem versions.
35
+ - Update development gems' versions.
21
36
 
22
37
  **Fixed**
23
38
  - `#dup` and `#clone` now behave exactly like Numeric versions, preventing unfreezing.
data/README.md CHANGED
@@ -16,12 +16,15 @@ Similar projects:
16
16
 
17
17
  ## Installation
18
18
 
19
- Add gem to your Gemfile:
20
- ```ruby
21
- gem "vector_number", git: "https://github.com/trinistr/vector_number.git"
19
+ Install with `gem`:
20
+ ```sh
21
+ gem install vector_number
22
22
  ```
23
23
 
24
- Installation through `gem` is not currently supported.
24
+ If using `bundler`, add gem to your Gemfile:
25
+ ```ruby
26
+ gem "vector_number"
27
+ ```
25
28
 
26
29
  ## Usage
27
30
 
@@ -63,7 +66,7 @@ VectorNumber is developed on MRI (CRuby) but should work on other engines too.
63
66
 
64
67
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests, `rake rubocop` to check code, `rake steep` to check typing or just `rake` to do everything. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
68
 
66
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, change Next version in `CHANGELOG.md`, commit changes and tag the commit. Alternatively, an appropriate `rake bump:` command can be used.
69
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, change Next version in `CHANGELOG.md`, commit changes and tag the commit. Alternatively, an appropriate `rake bump:{major|minor|patch}` command can be used.
67
70
 
68
71
  ## Contributing
69
72
 
@@ -3,10 +3,10 @@
3
3
  class VectorNumber
4
4
  # Various mathematical operations that are also conversions.
5
5
  module MathConverting
6
- # Return the absolute value of a vector, i.e. its length.
6
+ # Return the absolute value of the vector, i.e. its length.
7
7
  # @return [Float]
8
8
  def abs
9
- Math.sqrt(coefficients.reduce(0.0) { |result, coefficient| result + coefficient.abs2 })
9
+ Math.sqrt(coefficients.sum(&:abs2)) # rubocop:disable Naming/VariableNumber
10
10
  end
11
11
 
12
12
  alias magnitude abs
@@ -64,12 +64,13 @@ class VectorNumber
64
64
  end
65
65
 
66
66
  # Divide all coefficients by a real number, returning new vector.
67
- # This effectively multiplies magnitude by reciprocal of the specified factor.
67
+ # This effectively multiplies magnitude by reciprocal of +other+.
68
68
  # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
69
69
  # @return [VectorNumber]
70
70
  # @raise [RangeError] if +other+ is not a number or is not a real number
71
+ # @raise [ZeroDivisionError] if +other+ is zero
71
72
  def /(other)
72
- raise RangeError, "can't divide #{self} by #{other}" unless real_number?(other)
73
+ check_divisibility(other)
73
74
 
74
75
  other = other.real
75
76
  # Prevent integer division, but without loss of accuracy.
@@ -78,16 +79,87 @@ class VectorNumber
78
79
  new { _1 / other }
79
80
  end
80
81
 
82
+ alias quo /
83
+
81
84
  # Divide all coefficients by a real number using +fdiv+, returning new vector
82
85
  # with Float coefficients.
83
86
  # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
84
87
  # @return [VectorNumber]
85
88
  # @raise [RangeError] if +other+ is not a number or is not a real number
89
+ # @raise [ZeroDivisionError] if +other+ is zero
86
90
  def fdiv(other)
87
- raise RangeError, "can't divide #{self} by #{other}" unless real_number?(other)
91
+ check_divisibility(other)
88
92
 
89
93
  other = other.real
90
94
  new { _1.fdiv(other) }
91
95
  end
96
+
97
+ # Divide all coefficients by +other+, rounding results with {#floor}.
98
+ # This is requal to +(self / other).floor+.
99
+ # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
100
+ # @return [VectorNumber]
101
+ # @raise [RangeError] if +other+ is not a number or is not a real number
102
+ # @raise [ZeroDivisionError] if +other+ is zero
103
+ # @see #divmod
104
+ # @see #%
105
+ def div(other)
106
+ check_divisibility(other)
107
+
108
+ other = other.real
109
+ new { _1.div(other) }
110
+ end
111
+
112
+ # Return the modulus of dividing self by +other+ as a vector.
113
+ # This is equal to +self - other * (self/other).floor+.
114
+ # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
115
+ # @return [VectorNumber]
116
+ # @raise [RangeError] if +other+ is not a number or is not a real number
117
+ # @raise [ZeroDivisionError] if +other+ is zero
118
+ # @see #divmod
119
+ # @see #div
120
+ # @see #remainder for alternative
121
+ # @see Numeric#% for examples
122
+ def %(other)
123
+ check_divisibility(other)
124
+
125
+ other = other.real
126
+ new { _1 % other }
127
+ end
128
+
129
+ alias modulo %
130
+
131
+ # Return the quotient and modulus of dividing self by +other+.
132
+ # There is no performance benefit compared to calling {#div} and {#%} separately.
133
+ # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
134
+ # @return [Array(VectorNumber, VectorNumber)]
135
+ # @raise [RangeError] if +other+ is not a number or is not a real number
136
+ # @raise [ZeroDivisionError] if +other+ is zero
137
+ # @see #div
138
+ # @see #%
139
+ def divmod(other)
140
+ [div(other), modulo(other)]
141
+ end
142
+
143
+ # Return the remainder of dividing self by +other+ as a vector.
144
+ # This is equal to +self - other * (self/other).truncate+.
145
+ # @param other [Integer, Float, Rational, BigDecimal, VectorNumber]
146
+ # @return [VectorNumber]
147
+ # @raise [RangeError] if +other+ is not a number or is not a real number
148
+ # @raise [ZeroDivisionError] if +other+ is zero
149
+ # @see #% for alternative
150
+ # @see Numeric#remainder for examples
151
+ def remainder(other)
152
+ check_divisibility(other)
153
+
154
+ other = other.real
155
+ new { _1.remainder(other) }
156
+ end
157
+
158
+ private
159
+
160
+ def check_divisibility(other)
161
+ raise RangeError, "can't divide #{self} by #{other}", caller unless real_number?(other)
162
+ raise ZeroDivisionError, "divided by 0", caller if other.zero?
163
+ end
92
164
  end
93
165
  end
@@ -65,14 +65,14 @@ class VectorNumber
65
65
  !zero? && all? { |_u, c| c.negative? }
66
66
  end
67
67
 
68
- # Always returns +false+.
68
+ # Always returns +false+, as vectors are never real numbers.
69
69
  # @see #numeric?
70
70
  # @return [false]
71
71
  def real?
72
72
  false
73
73
  end
74
74
 
75
- # Always returns +false+.
75
+ # Always returns +false+, as vectors are not +Integer+s.
76
76
  # @return [false]
77
77
  def integer?
78
78
  false
@@ -48,7 +48,7 @@ class VectorNumber
48
48
  # @raise [ArgumentError] if +mult+ is not in {MULT_STRINGS}'s keys
49
49
  def value_to_s(unit, coefficient, mult:)
50
50
  if !mult.is_a?(String) && !MULT_STRINGS.key?(mult)
51
- raise ArgumentError, "unknown key :#{mult}", caller
51
+ raise ArgumentError, "unknown key #{mult.inspect}", caller
52
52
  end
53
53
 
54
54
  case unit
@@ -2,5 +2,5 @@
2
2
 
3
3
  class VectorNumber # rubocop:disable Style/StaticClass
4
4
  # @return [String]
5
- VERSION = "0.2.4"
5
+ VERSION = "0.2.6"
6
6
  end
@@ -1,3 +1,19 @@
1
+ type vector_type = VectorNumber
2
+ type real_number = Integer | Float | Rational | BigDecimal
3
+
4
+ type in_value_type = untyped
5
+ type unit_type = untyped
6
+ type coefficient_type = real_number
7
+
8
+ type list[T] = Array[T]
9
+
10
+ type plain_vector_type = Hash[unit_type, coefficient_type]
11
+ type units_list_type = list[unit_type]
12
+ type coefficients_list_type = list[coefficient_type]
13
+ type each_value_type = [unit_type, coefficient_type]
14
+
15
+ type options_type = Hash[Symbol, untyped]
16
+
1
17
  interface _BaseMethods
2
18
  def size: -> Integer
3
19
  def options: -> options_type
@@ -82,8 +98,22 @@ class VectorNumber
82
98
  def *: (real_number | vector_type) -> vector_type
83
99
 
84
100
  def /: (real_number | vector_type) -> vector_type
101
+ alias quo /
85
102
 
86
103
  def fdiv: (real_number | vector_type) -> vector_type
104
+
105
+ def div: (real_number | vector_type) -> vector_type
106
+
107
+ def %: (real_number | vector_type) -> vector_type
108
+ alias modulo %
109
+
110
+ def divmod: (real_number | vector_type) -> [vector_type, vector_type]
111
+
112
+ def remainder: (real_number | vector_type) -> vector_type
113
+
114
+ private
115
+
116
+ def check_divisibility: (real_number | vector_type) -> void
87
117
  end
88
118
 
89
119
  # Various mathematical operations that are also conversions.
metadata CHANGED
@@ -1,17 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vector_number
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Bulancov
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2025-02-25 00:00:00.000000000 Z
10
+ date: 2025-04-30 00:00:00.000000000 Z
12
11
  dependencies: []
13
- description:
14
- email:
15
12
  executables: []
16
13
  extensions: []
17
14
  extra_rdoc_files:
@@ -30,7 +27,6 @@ files:
30
27
  - lib/vector_number/querying.rb
31
28
  - lib/vector_number/stringifying.rb
32
29
  - lib/vector_number/version.rb
33
- - sig/definitions.rbs
34
30
  - sig/vector_number.rbs
35
31
  homepage: https://github.com/trinistr/vector_number
36
32
  licenses:
@@ -40,7 +36,6 @@ metadata:
40
36
  source_code_uri: https://github.com/trinistr/vector_number
41
37
  changelog_uri: https://github.com/trinistr/vector_number/CHANGELOG.md
42
38
  rubygems_mfa_required: 'true'
43
- post_install_message:
44
39
  rdoc_options:
45
40
  - "--main"
46
41
  - README.md
@@ -57,8 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
52
  - !ruby/object:Gem::Version
58
53
  version: '0'
59
54
  requirements: []
60
- rubygems_version: 3.5.21
61
- signing_key:
55
+ rubygems_version: 3.6.5
62
56
  specification_version: 4
63
57
  summary: A library to add together anything.
64
58
  test_files: []
data/sig/definitions.rbs DELETED
@@ -1,15 +0,0 @@
1
- type vector_type = VectorNumber
2
- type real_number = Integer | Float | Rational | BigDecimal
3
-
4
- type in_value_type = untyped
5
- type unit_type = untyped
6
- type coefficient_type = real_number
7
-
8
- type list[T] = Array[T]
9
-
10
- type plain_vector_type = Hash[unit_type, coefficient_type]
11
- type units_list_type = list[unit_type]
12
- type coefficients_list_type = list[coefficient_type]
13
- type each_value_type = [unit_type, coefficient_type]
14
-
15
- type options_type = Hash[Symbol, untyped]