calcpace 1.14.0 → 1.15.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3fb2c004f6564fef1686e1c92db831e7c54a47360dd9c540a90bb334582ee95
4
- data.tar.gz: 31c20dd413fe2df72d57f7516bb613c7661724bba196ff81da3af3b2c17c0ee0
3
+ metadata.gz: 5eefef398417bfe2d045b2afd7ee78991b443c7c9bc1eabd536e81d03789908c
4
+ data.tar.gz: 7b676576e7e3af9b9d4ce75430af80835a35535b2c8b971ed5ef764ddd608853
5
5
  SHA512:
6
- metadata.gz: 784831b687993538654becf398906fc798ff089e8344ed1cd8768f3b42735111b490f99f185b0535f09d84ad15b0d207703e49d494c6e61ca1c3311630016b9a
7
- data.tar.gz: 3f4170b7d5a3c4f71ed40ab732fb02be5ea2f1daf494c179aedab9b63e1837b90631ebf8db8f1484e83fbd9b55b569b7c9db31f66d915ca3228dae745eb7d619
6
+ metadata.gz: 2b7481ff8c0c4b91f724af723eacfd9357905fa2207adc0871dd745d7fc4a4beac52349f0051703a91288c724dd2b6c00298beb585d08949b156833e9fc70af7
7
+ data.tar.gz: 7d58f1c452924bbc9c7a895a5ed5102e1ae298b002984d9708aea23b4f54f7339fa2b4dd6708b678cf5f1dd45d5cfd8cc8dd5c3e869df6b07856f08eaf6c720c
data/.rubocop.yml CHANGED
@@ -80,3 +80,11 @@ Naming/MethodParameterName:
80
80
  AllowedNames:
81
81
  - _
82
82
  - e
83
+
84
+ # The documented-examples test evaluates snippets scraped from our own README
85
+ # and docstrings — that is the entire point of the file, and the cop's required
86
+ # comment format cannot be satisfied without it then reporting the disable as
87
+ # redundant.
88
+ Style/DocumentDynamicEvalDefinition:
89
+ Exclude:
90
+ - 'test/calcpace/test_documented_examples.rb'
data/CHANGELOG.md CHANGED
@@ -7,6 +7,130 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [1.15.0] - 2026-08-30
11
+
12
+ ### Added
13
+ - Every method that takes a race now accepts a **plain distance in kilometers**,
14
+ not only one of the eight standard race names. Most races are not a 5K or a
15
+ marathon, and a formula does not care what a distance is called:
16
+
17
+ ```ruby
18
+ calc.race_time_clock('05:00', 7.79) # => "00:38:57"
19
+ calc.race_pace_clock('00:26:59', 7.79) # => "00:03:27"
20
+ calc.predict_time_clock(7.79, '00:26:59', 'half_marathon') # => "01:17:34"
21
+ calc.predict_time_clock('10k', '00:42:00', 15) # => "01:04:33"
22
+ calc.predict_time_clock(7.79, '00:26:59', 15) # => "00:54:02"
23
+ calc.predict_time_cameron_clock(7.79, '00:26:59', 'half_marathon') # => "01:13:44"
24
+ calc.race_splits(7.79, target_time: '00:26:59', split_distance: '1k')
25
+ # => ["00:03:28", "00:06:56", "00:10:23", "00:13:51", "00:17:19", "00:20:47", "00:24:15", "00:26:59"]
26
+ ```
27
+
28
+ Both ends of a prediction follow the same rule, so all four combinations work:
29
+ name to name, name to number, number to name, number to number. The methods
30
+ that reach a distance through `race_distance` inherit it: `race_time`,
31
+ `race_pace`, their `_clock` variants, `predict_time`, `predict_pace`,
32
+ `equivalent_performance`, the Cameron equivalents, the `_adjusted` variants,
33
+ `race_splits`, and `race_times_from_vo2max`.
34
+
35
+ A numeric **string** counts as a distance: `'7.79'` and `7.79` mean the same
36
+ thing. This is not a new convention — `training_paces_from_race` and
37
+ `predict_time_from_vo2max` have read numeric strings as distances since they
38
+ were written, and `race_distance` was the one place that disagreed. A string
39
+ that is not a number is still a race name, so `'7.79k'` remains
40
+ `ArgumentError: Unknown race`. Only `Numeric` and `String` are read as
41
+ distances; a Symbol is always a name, as in the two methods above.
42
+
43
+ A numeric distance must be positive, and reports itself the way every other
44
+ distance in the gem does:
45
+
46
+ ```ruby
47
+ calc.race_time(300, 0) # => Calcpace::NonPositiveInputError: Distance must be a positive number
48
+ ```
49
+
50
+ Before this release those calls raised `ArgumentError: Unknown race: 0`. The
51
+ input was an error either way; only the class and the message changed.
52
+
53
+ ### Breaking
54
+ - **A non-positive distance now raises `Calcpace::NonPositiveInputError`
55
+ instead of `ArgumentError`.** Calls like `race_time(300, 0)` or
56
+ `race_splits('0', ...)` used to fail with `ArgumentError: Unknown race: 0`,
57
+ because `0` was not a race name; now `0` is read as a distance and rejected
58
+ as one. `Calcpace::NonPositiveInputError` inherits from `Calcpace::Error`,
59
+ **not** from `ArgumentError`, so a caller that wraps this library in
60
+ `rescue ArgumentError` — a form field arriving as `"0"`, for example — will
61
+ see the exception escape instead of being caught.
62
+ The alternative was to make this one path raise `ArgumentError` for
63
+ consistency with the old behaviour, which would have made the library
64
+ inconsistent with itself: every other non-positive input in the gem already
65
+ raises `NonPositiveInputError`. Wrapping in `rescue Calcpace::Error,
66
+ ArgumentError` handles both this and any future version.
67
+
68
+ ### Changed
69
+ - The "from and to must be different distances" guard in `predict_time` and
70
+ `predict_time_cameron` no longer compares distances with `==`. Two distances
71
+ now count as the same race when they differ by less than
72
+ `PaceCalculator::SAME_DISTANCE_TOLERANCE_RATIO` (1e-9, relative), so a
73
+ distance that only differs by floating-point noise still raises instead of
74
+ returning a prediction of the same time back:
75
+
76
+ ```ruby
77
+ calc.predict_time(10.0, 2520, '10k')
78
+ # => ArgumentError: From and to races must be different distances (both are 10.0km)
79
+ ```
80
+
81
+ The window is deliberately narrow: it absorbs representation noise and
82
+ nothing else. `predict_time(10.0, 2520, 10.2)` is a legitimate 200 m
83
+ extrapolation and still answers.
84
+
85
+ - Age grading matches a numeric distance to a standard within **2%**, up from
86
+ 0.5% (`AgeGrading::STANDARD_DISTANCE_TOLERANCE_RATIO`, previously an
87
+ unnamed literal). GPS rarely reads a 5K as exactly 5.000 km, and 2% is the
88
+ window calcpace.app already uses to decide a run "is a 5K" — the two used to
89
+ disagree about the same run:
90
+
91
+ ```ruby
92
+ calc.age_grade_percent(5.0, '00:25:00', age: 40, sex: :male) # => 51.9
93
+ calc.age_grade_percent(5.0374, '00:25:00', age: 40, sex: :male) # => 51.9
94
+ ```
95
+
96
+ Nothing else about age grading changed, on purpose. A distance outside the
97
+ window still raises, and that is the intended answer rather than a missing
98
+ feature: the WMA 2023 tables publish a factor per *specific* distance, so
99
+ there is no standard for 7.79 km to compare against, and interpolating one
100
+ would produce a number with the look of an official standard and none of the
101
+ authority.
102
+
103
+ ```ruby
104
+ calc.age_grade(7.79, '00:26:59', age: 36, sex: :male)
105
+ # => ArgumentError: Unsupported distance 7.79km. Supported: 5.0, 10.0, 21.0975, 42.195 km
106
+ ```
107
+
108
+ Riegel and Cameron both degrade as the jump between distances grows — a
109
+ marathon predicted from a 1 km time is arithmetic, not a forecast. This
110
+ release adds no guard against that: the gem never warned about it for the
111
+ standard race names either, and inventing a threshold now would be a new
112
+ opinion, not a fix. The note is here and in the README so the caller can
113
+ weigh it.
114
+
115
+ ### Fixed
116
+ - Documentation only, no behavior change: several `@example` values in
117
+ `PaceCalculator`, `RacePredictor` and `CameronPredictor`, and their
118
+ counterparts in the README, had drifted from what the code returns — the
119
+ worst of them by more than six minutes (`predict_time_cameron_clock` was
120
+ documented as `'00:02:32'` where it returns `'00:04:15'`). Two `@example`
121
+ lines also used `:5k`, which is not valid Ruby syntax, and now use `'5k'`.
122
+ - An adversarial review of this release found three more that the first pass
123
+ had missed, including the README block for `equivalent_performance` — which
124
+ contradicted the docstring corrected in this very release — and the main
125
+ age-grading example, wrong in four of its eight fields. Every example was
126
+ then executed and compared line by line, README and `@example` alike. The
127
+ lesson was acted on rather than recorded: `test_documented_examples.rb` now
128
+ executes every single-line example in the README and in the docstrings and
129
+ compares it with what the code returns, so a drifted example fails the suite
130
+ instead of reaching a reader. It pins two numbers — how many examples it
131
+ finds and how many it actually compares — because a scanner that silently
132
+ matches nothing would pass forever.
133
+
10
134
  ## [1.14.0] - 2026-08-29
11
135
 
12
136
  ### Added
@@ -381,7 +505,8 @@ predictors are untouched.
381
505
 
382
506
  See git history for changes in earlier versions.
383
507
 
384
- [Unreleased]: https://github.com/0jonjo/calcpace/compare/v1.14.0...HEAD
508
+ [Unreleased]: https://github.com/0jonjo/calcpace/compare/v1.15.0...HEAD
509
+ [1.15.0]: https://github.com/0jonjo/calcpace/compare/v1.14.0...v1.15.0
385
510
  [1.14.0]: https://github.com/0jonjo/calcpace/compare/v1.13.0...v1.14.0
386
511
  [1.13.0]: https://github.com/0jonjo/calcpace/compare/v1.12.1...v1.13.0
387
512
  [1.12.0]: https://github.com/0jonjo/calcpace/compare/v1.11.0...v1.12.0
data/README.md CHANGED
@@ -22,7 +22,7 @@ calc = Calcpace.new
22
22
  ```ruby
23
23
  calc.velocity(3625, 12275) # => 3.386 (distance / time)
24
24
  calc.pace(3665, 12) # => 305.4 (time / distance)
25
- calc.time(210, 12) # => 2520.0 (pace × distance)
25
+ calc.time(210, 12) # => 2520 (pace × distance)
26
26
  calc.distance(9660, 120) # => 80.5 (velocity × time)
27
27
 
28
28
  # Clocktime input/output (HH:MM:SS or MM:SS)
@@ -109,12 +109,24 @@ calc.convert_pace('05:00', :km_to_mi, compact: true) # => "8:02"
109
109
 
110
110
  ### Race Pace & Time
111
111
 
112
+ Every method that takes a race accepts either a standard race name or a plain
113
+ distance in kilometers — most races are not a 5K or a marathon.
114
+
112
115
  ```ruby
113
116
  calc.race_time_clock('05:00', 'marathon') # => "03:30:58"
114
117
  calc.race_pace_clock('04:00:00', 'marathon') # => "00:05:41"
115
118
  calc.list_races # => { '5k' => 5.0, '10k' => 10.0, 'half_marathon' => 21.0975, 'marathon' => 42.195, '100k' => 100.0, ... }
119
+
120
+ # Any distance, named or not — 7.79 km and '7.79' mean the same thing
121
+ calc.race_time_clock('05:00', 7.79) # => "00:38:57"
122
+ calc.race_time_clock('05:00', '7.79') # => "00:38:57"
123
+ calc.race_pace_clock('00:26:59', 7.79) # => "00:03:27"
116
124
  ```
117
125
 
126
+ A distance must be positive (`Calcpace::NonPositiveInputError` otherwise), and
127
+ anything that is neither a number nor a known race name still raises
128
+ `ArgumentError` — `'7.79k'` is a typo, not a distance.
129
+
118
130
  ---
119
131
 
120
132
  ### Race Splits
@@ -127,6 +139,10 @@ calc.race_splits('half_marathon', target_time: '01:30:00', split_distance: '5k')
127
139
  # Strategies: :even (default), :negative (second half faster), :positive (first half faster)
128
140
  calc.race_splits('10k', target_time: '00:40:00', split_distance: '5k', strategy: :negative)
129
141
  # => ["00:20:48", "00:40:00"]
142
+
143
+ # The race may be a plain distance too; the last split is always the finish
144
+ calc.race_splits(7.79, target_time: '00:26:59', split_distance: '1k')
145
+ # => ["00:03:28", "00:06:56", "00:10:23", "00:13:51", "00:17:19", "00:20:47", "00:24:15", "00:26:59"]
130
146
  ```
131
147
 
132
148
  ---
@@ -139,16 +155,45 @@ calc.race_splits('10k', target_time: '00:40:00', split_distance: '5k', strategy:
139
155
  calc.predict_time_clock('5k', '00:20:00', 'marathon') # => "03:11:49"
140
156
  calc.predict_pace_clock('5k', '00:20:00', 'marathon') # => "00:04:32"
141
157
  calc.equivalent_performance('10k', '00:42:00', '5k')
142
- # => { time: 1209.0, time_clock: "00:20:09", pace: 241.8, pace_clock: "00:04:02" }
158
+ # => { time: 1208.67, time_clock: "00:20:08", pace: 241.73, pace_clock: "00:04:01" }
143
159
  ```
144
160
 
145
161
  **Cameron formula** (exponential correction — tends to be more conservative from short distances):
146
162
 
147
163
  ```ruby
148
- calc.predict_time_cameron_clock('10k', '00:42:00', 'marathon') # => "02:57:46"
149
- calc.predict_pace_cameron_clock('10k', '00:42:00', 'marathon') # => "00:04:13"
164
+ calc.predict_time_cameron_clock('10k', '00:42:00', 'marathon') # => "02:57:34"
165
+ calc.predict_pace_cameron_clock('10k', '00:42:00', 'marathon') # => "00:04:12"
150
166
  ```
151
167
 
168
+ **Any distance, on either end.** Both formulas are arithmetic on two distances,
169
+ so neither end has to be a standard race:
170
+
171
+ ```ruby
172
+ # From a 7.79 km club race in 26:59
173
+ calc.predict_time_clock(7.79, '00:26:59', 'half_marathon') # => "01:17:34"
174
+ calc.predict_time_cameron_clock(7.79, '00:26:59', 'half_marathon') # => "01:13:44"
175
+
176
+ # To an unnamed distance, and between two of them
177
+ calc.predict_time_clock('10k', '00:42:00', 15) # => "01:04:33"
178
+ calc.predict_time_clock(7.79, '00:26:59', 15) # => "00:54:02"
179
+
180
+ calc.equivalent_performance(7.79, '00:26:59', '10k')
181
+ # => { time: 2109.682710043339, time_clock: "00:35:09", pace: 210.96827100433387, pace_clock: "00:03:30" }
182
+ ```
183
+
184
+ Predicting a distance from itself has no answer, so it raises — for a name, a
185
+ number, or one of each:
186
+
187
+ ```ruby
188
+ calc.predict_time(10.0, 2520, '10k')
189
+ # => ArgumentError: From and to races must be different distances (both are 10.0km)
190
+ ```
191
+
192
+ Both formulas were fitted around race distances and degrade as the jump grows:
193
+ a marathon predicted from a 1 km time is arithmetic, not a forecast. The gem
194
+ computes what you ask for and does not second-guess the gap — that judgement is
195
+ the caller's, and it always was, standard race names included.
196
+
152
197
  ---
153
198
 
154
199
  ### GPS Track Analysis
@@ -202,22 +247,40 @@ age factors and open standards.
202
247
  result = calc.age_grade(10.0, '00:45:00', age: 55, sex: :male)
203
248
  # numeric distances also accepted in miles: calc.age_grade(6.21371, '00:45:00', age: 55, sex: :male, distance_unit: :mi)
204
249
  # => {
205
- # age_grade_percent: 64.6,
250
+ # age_grade_percent: 69.0,
206
251
  # category: "Local Class",
207
- # age_graded_time_seconds: 2376.0,
208
- # age_graded_time_clock: "00:39:36",
252
+ # age_graded_time_seconds: 2278.26,
253
+ # age_graded_time_clock: "00:37:58",
209
254
  # open_standard_seconds: 1571.0,
210
255
  # open_standard_clock: "00:26:11",
211
- # factor: 0.88,
256
+ # factor: 0.8438,
212
257
  # table_version: "WMA_2023_ONE_YEAR_FACTORS_V1"
213
258
  # }
214
259
 
215
- calc.age_grade_percent(5.0, '00:22:30', age: 40, sex: :female) # => 74.1
216
- calc.age_grade_label(74.1) # => "Regional Class"
260
+ calc.age_grade_percent(5.0, '00:22:30', age: 40, sex: :female) # => 65.2
261
+ calc.age_grade_label(65.2) # => "Local Class"
217
262
  ```
218
263
 
219
264
  Supported distances: 5K, 10K, half marathon, marathon.
220
265
 
266
+ A numeric distance within **2%** of one of those is graded as that standard —
267
+ a GPS watch rarely reads a 5K as exactly 5.000 km:
268
+
269
+ ```ruby
270
+ calc.age_grade_percent(5.0, '00:25:00', age: 40, sex: :male) # => 51.9
271
+ calc.age_grade_percent(5.0374, '00:25:00', age: 40, sex: :male) # => 51.9
272
+
273
+ calc.age_grade(7.79, '00:26:59', age: 36, sex: :male)
274
+ # => ArgumentError: Unsupported distance 7.79km. Supported: 5.0, 10.0, 21.0975, 42.195 km
275
+ ```
276
+
277
+ That refusal is deliberate, and it is where age grading parts ways with the
278
+ predictors above. A prediction is a formula and works at any distance; an age
279
+ grade is a lookup in the WMA table, which publishes a factor per *specific*
280
+ distance. There is no world standard for 7.79 km, so there is no honest
281
+ percentage to return — interpolating one would produce a number with the look
282
+ of an official standard and none of the authority.
283
+
221
284
  Age factors are based on WMA 2023 one-year age grading tables:
222
285
  https://world-masters-athletics.org/documents/competition-rules/
223
286
 
@@ -238,7 +301,7 @@ Estimate aerobic fitness from a race result using the **Daniels & Gilbert formul
238
301
 
239
302
  ```ruby
240
303
  calc.estimate_vo2max(10.0, '00:40:00') # => 51.9 ml/kg/min
241
- calc.estimate_vo2max(42.195, '03:30:00') # => 44.8
304
+ calc.estimate_vo2max(42.195, '03:30:00') # => 44.6
242
305
  calc.estimate_vo2max(5.0, 2400) # also accepts total seconds
243
306
  calc.estimate_vo2max(6.21371, '00:40:00', distance_unit: :mi) # => 51.9 (miles input)
244
307
 
@@ -47,6 +47,15 @@ module AgeGrading
47
47
 
48
48
  SUPPORTED_DISTANCES_KM = DISTANCE_TO_METERS.keys.freeze
49
49
 
50
+ # How far a distance may sit from a standard and still be graded as it. 2% is
51
+ # the same window calcpace.app uses to decide a run "is a 5K", so the gem and
52
+ # the site never disagree about the same run. It stays a matching tolerance,
53
+ # not an interpolation: a distance outside it has no WMA factor and is refused
54
+ STANDARD_DISTANCE_TOLERANCE_RATIO = 0.02
55
+
56
+ # Floor for the window above, so a future shorter standard still matches
57
+ MINIMUM_DISTANCE_TOLERANCE_KM = 0.001
58
+
50
59
  # Returns a full age-grading report for a race performance
51
60
  #
52
61
  # @param distance [Numeric, String, Symbol] race distance in kilometres
@@ -145,10 +154,11 @@ module AgeGrading
145
154
  end
146
155
  end
147
156
 
148
- # Runners write rounded distances (3.1 mi, 13.1 mi, 26.2 mi), so the match
149
- # window is relative 0.5% of the standard distance, never below 1 metre
157
+ # Runners write rounded distances (3.1 mi, 13.1 mi, 26.2 mi) and GPS watches
158
+ # rarely read a 5K as exactly 5.000 km, so the match window is relative
150
159
  def standard_distance?(distance, standard)
151
- (distance - standard).abs <= [0.001, standard * 0.005].max
160
+ (distance - standard).abs <= [MINIMUM_DISTANCE_TOLERANCE_KM,
161
+ standard * STANDARD_DISTANCE_TOLERANCE_RATIO].max
152
162
  end
153
163
 
154
164
  def parse_time_seconds(time)
@@ -24,27 +24,26 @@ module CameronPredictor
24
24
 
25
25
  # Predicts race time using the Cameron formula
26
26
  #
27
- # @param from_race [String, Symbol] known race distance ('5k', '10k', 'half_marathon', 'marathon', '100k', etc.)
27
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers (7.79,
28
+ # '7.79') or a standard race name ('5k', '10k', 'half_marathon', 'marathon', '100k', etc.)
28
29
  # @param from_time [String, Numeric] time achieved at known distance (HH:MM:SS or seconds)
29
- # @param to_race [String, Symbol] target race distance to predict
30
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
30
31
  # @return [Float] predicted time in seconds
31
- # @raise [ArgumentError] if races are invalid or distances are the same
32
+ # @raise [ArgumentError] if a race name is invalid or the distances are the same
33
+ # @raise [Calcpace::NonPositiveInputError] if a numeric distance is not positive
32
34
  #
33
35
  # @example Predict marathon time from 10K
34
36
  # predict_time_cameron('10k', '00:42:00', 'marathon')
35
- # #=> ~10,666 seconds (approximately 2:57:46)
37
+ # #=> ~10,654 seconds (approximately 2:57:34)
36
38
  #
37
39
  # @example Predict 10K time from 5K
38
40
  # predict_time_cameron('5k', '00:20:00', '10k')
39
- # #=> ~2,544 seconds (approximately 42:24)
41
+ # #=> ~2,546 seconds (approximately 42:26)
40
42
  def predict_time_cameron(from_race, from_time, to_race)
41
43
  from_distance = race_distance(from_race)
42
44
  to_distance = race_distance(to_race)
43
45
 
44
- if from_distance == to_distance
45
- raise ArgumentError,
46
- "From and to races must be different distances (both are #{from_distance}km)"
47
- end
46
+ ensure_different_distances!(from_distance, to_distance)
48
47
 
49
48
  time_seconds = from_time.is_a?(String) ? convert_to_seconds(from_time) : from_time
50
49
  check_positive(time_seconds, 'Time')
@@ -56,23 +55,23 @@ module CameronPredictor
56
55
 
57
56
  # Predicts race time using the Cameron formula, returned as a clock time string
58
57
  #
59
- # @param from_race [String, Symbol] known race distance
58
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
60
59
  # @param from_time [String, Numeric] time achieved at known distance
61
- # @param to_race [String, Symbol] target race distance to predict
60
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
62
61
  # @return [String] predicted time in HH:MM:SS format
63
62
  #
64
63
  # @example
65
64
  # predict_time_cameron_clock('10k', '00:42:00', 'marathon')
66
- # #=> '02:57:46'
65
+ # #=> '02:57:34'
67
66
  def predict_time_cameron_clock(from_race, from_time, to_race)
68
67
  convert_to_clocktime(predict_time_cameron(from_race, from_time, to_race))
69
68
  end
70
69
 
71
70
  # Predicts pace per kilometer using the Cameron formula
72
71
  #
73
- # @param from_race [String, Symbol] known race distance
72
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
74
73
  # @param from_time [String, Numeric] time achieved at known distance
75
- # @param to_race [String, Symbol] target race distance to predict
74
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
76
75
  # @return [Float] predicted pace in seconds per kilometer
77
76
  #
78
77
  # @example
@@ -84,23 +83,23 @@ module CameronPredictor
84
83
 
85
84
  # Predicts pace per kilometer using the Cameron formula, returned as a clock time string
86
85
  #
87
- # @param from_race [String, Symbol] known race distance
86
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
88
87
  # @param from_time [String, Numeric] time achieved at known distance
89
- # @param to_race [String, Symbol] target race distance to predict
88
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
90
89
  # @return [String] predicted pace in HH:MM:SS format
91
90
  #
92
91
  # @example
93
92
  # predict_pace_cameron_clock('5k', '00:20:00', 'marathon')
94
- # #=> '00:02:32'
93
+ # #=> '00:04:15'
95
94
  def predict_pace_cameron_clock(from_race, from_time, to_race)
96
95
  convert_to_clocktime(predict_pace_cameron(from_race, from_time, to_race))
97
96
  end
98
97
 
99
98
  # Predicts race time adjusted for environmental conditions using Cameron formula
100
99
  #
101
- # @param from_race [String, Symbol] known race distance
100
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
102
101
  # @param from_time [String, Numeric] time achieved at known distance
103
- # @param to_race [String, Symbol] target race distance to predict
102
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
104
103
  # @param options [Hash] environmental options (temperature, altitude, etc.)
105
104
  # @return [Hash] hash with adjusted prediction and penalty details
106
105
  def predict_time_cameron_adjusted(from_race, from_time, to_race, **)
@@ -17,15 +17,22 @@ module PaceCalculator
17
17
  '10mile' => 10 * Converter::Distance::MI_TO_KM
18
18
  }.freeze
19
19
 
20
+ # Relative window under which two distances count as the same race. It exists
21
+ # only to absorb floating-point noise, never to call two different distances
22
+ # equal — 10.0 km and 10.2 km are a legitimate prediction, not a mistake
23
+ SAME_DISTANCE_TOLERANCE_RATIO = 1e-9
24
+
20
25
  # Calculates the finish time for a race given a pace per kilometer
21
26
  #
22
27
  # @param pace_per_km [Numeric, String] pace in seconds per km or time string (MM:SS)
23
- # @param race [String, Symbol] race distance ('5k', '10k', 'half_marathon', 'marathon', '100k')
28
+ # @param race [Numeric, String, Symbol] distance in kilometers (7.79, '7.79') or a
29
+ # standard race name ('5k', '10k', 'half_marathon', 'marathon', '100k', ...)
24
30
  # @return [Float] total time in seconds
25
- # @raise [ArgumentError] if race distance is not recognized
31
+ # @raise [ArgumentError] if a race name is not recognized
32
+ # @raise [Calcpace::NonPositiveInputError] if a numeric distance is not positive
26
33
  #
27
34
  # @example
28
- # race_time(300, :5k) #=> 1500.0 (5:00/km pace for 5K = 25:00)
35
+ # race_time(300, '5k') #=> 1500.0 (5:00/km pace for 5K = 25:00)
29
36
  # race_time('05:00', :marathon) #=> 12658.5 (5:00/km pace for marathon = 3:30:58)
30
37
  def race_time(pace_per_km, race)
31
38
  distance = race_distance(race)
@@ -37,12 +44,13 @@ module PaceCalculator
37
44
  # Calculates the finish time for a race and returns it as a clock time string
38
45
  #
39
46
  # @param pace_per_km [Numeric, String] pace in seconds per km or time string (MM:SS)
40
- # @param race [String, Symbol] race distance ('5k', '10k', 'half_marathon', 'marathon', '100k')
47
+ # @param race [Numeric, String, Symbol] distance in kilometers (7.79, '7.79') or a
48
+ # standard race name ('5k', '10k', 'half_marathon', 'marathon', '100k', ...)
41
49
  # @return [String] finish time in HH:MM:SS format
42
50
  #
43
51
  # @example
44
52
  # race_time_clock('05:00', :marathon) #=> '03:30:58'
45
- # race_time_clock(300, :half_marathon) #=> '01:45:17'
53
+ # race_time_clock(300, :half_marathon) #=> '01:45:29'
46
54
  def race_time_clock(pace_per_km, race)
47
55
  convert_to_clocktime(race_time(pace_per_km, race))
48
56
  end
@@ -50,12 +58,13 @@ module PaceCalculator
50
58
  # Calculates the required pace per kilometer to finish a race in a target time
51
59
  #
52
60
  # @param target_time [Numeric, String] target finish time in seconds or time string (HH:MM:SS)
53
- # @param race [String, Symbol] race distance ('5k', '10k', 'half_marathon', 'marathon', '100k')
61
+ # @param race [Numeric, String, Symbol] distance in kilometers (7.79, '7.79') or a
62
+ # standard race name ('5k', '10k', 'half_marathon', 'marathon', '100k', ...)
54
63
  # @return [Float] required pace in seconds per kilometer
55
64
  #
56
65
  # @example
57
- # race_pace('03:30:00', :marathon) #=> 297.48... (4:57/km to finish in 3:30)
58
- # race_pace(1800, :5k) #=> 360.0 (6:00/km to finish in 30:00)
66
+ # race_pace('03:30:00', :marathon) #=> 298.61... (4:58/km to finish in 3:30)
67
+ # race_pace(1800, '5k') #=> 360.0 (6:00/km to finish in 30:00)
59
68
  def race_pace(target_time, race)
60
69
  distance = race_distance(race)
61
70
  time_seconds = target_time.is_a?(String) ? convert_to_seconds(target_time) : target_time
@@ -66,11 +75,12 @@ module PaceCalculator
66
75
  # Calculates the required pace and returns it as a clock time string
67
76
  #
68
77
  # @param target_time [Numeric, String] target finish time in seconds or time string (HH:MM:SS)
69
- # @param race [String, Symbol] race distance ('5k', '10k', 'half_marathon', 'marathon', '100k')
78
+ # @param race [Numeric, String, Symbol] distance in kilometers (7.79, '7.79') or a
79
+ # standard race name ('5k', '10k', 'half_marathon', 'marathon', '100k', ...)
70
80
  # @return [String] required pace in MM:SS format
71
81
  #
72
82
  # @example
73
- # race_pace_clock('03:30:00', :marathon) #=> '00:04:57'
83
+ # race_pace_clock('03:30:00', :marathon) #=> '00:04:58'
74
84
  def race_pace_clock(target_time, race)
75
85
  convert_to_clocktime(race_pace(target_time, race))
76
86
  end
@@ -87,18 +97,61 @@ module PaceCalculator
87
97
 
88
98
  private
89
99
 
90
- # Gets the distance for a standard race
100
+ # Resolves a race argument to a distance in kilometers
101
+ #
102
+ # A standard race name resolves through RACE_DISTANCES; a distance resolves
103
+ # to itself. Numeric strings ('7.79') count as distances — the convention
104
+ # TrainingZones#training_paces_from_race and FitnessPredictor already follow,
105
+ # so 7.79 and '7.79' can never mean two different things.
91
106
  #
92
- # @param race [String, Symbol] race name
107
+ # @param race [Numeric, String, Symbol] race name or distance in kilometers
93
108
  # @return [Float] distance in kilometers
94
- # @raise [ArgumentError] if race is not recognized
109
+ # @raise [ArgumentError] if a race name is not recognized
110
+ # @raise [Calcpace::NonPositiveInputError] if a numeric distance is not positive
95
111
  def race_distance(race)
112
+ numeric = numeric_distance(race)
113
+ return numeric if numeric
114
+
96
115
  RACE_DISTANCES.fetch(normalize_race_key(race)) do
97
116
  raise ArgumentError,
98
117
  "Unknown race: #{race}. Available races: #{RACE_DISTANCES.keys.join(', ')}"
99
118
  end
100
119
  end
101
120
 
121
+ # Reads a race argument as a plain distance, or nil when it is a race name
122
+ #
123
+ # @param race [Numeric, String, Symbol] race name or distance in kilometers
124
+ # @return [Float, nil] the distance in kilometers, nil for a non-numeric input
125
+ # @raise [Calcpace::NonPositiveInputError] if the distance is not positive
126
+ def numeric_distance(race)
127
+ value = race.is_a?(Numeric) ? race.to_f : Float(race, exception: false)
128
+ return nil unless value
129
+
130
+ check_positive(value, 'Distance')
131
+ value
132
+ end
133
+
134
+ # Guards the "predict a distance from itself" call, which has no answer to give
135
+ #
136
+ # @param from_distance [Float] known distance in kilometers
137
+ # @param to_distance [Float] target distance in kilometers
138
+ # @raise [ArgumentError] if both distances are the same race
139
+ def ensure_different_distances!(from_distance, to_distance)
140
+ return unless same_distance?(from_distance, to_distance)
141
+
142
+ raise ArgumentError,
143
+ "From and to races must be different distances (both are #{from_distance}km)"
144
+ end
145
+
146
+ # Two distances are the same race only when they differ by representation
147
+ # noise — a window narrow enough that any distance a runner actually types is
148
+ # outside it. Deciding when two genuinely different distances are close
149
+ # enough to count as the same race is age grading's business, and it has its
150
+ # own, much wider, explicit tolerance
151
+ def same_distance?(one, other)
152
+ (one - other).abs <= [one.abs, other.abs].max * SAME_DISTANCE_TOLERANCE_RATIO
153
+ end
154
+
102
155
  # Single normalization for every race-name lookup in the gem (here and in
103
156
  # AgeGrading), so ' 10K ' and :marathon always resolve like '10k' and 'marathon'
104
157
  #
@@ -19,27 +19,26 @@ module RacePredictor
19
19
  # - D2 = target distance
20
20
  # - 1.06 = endurance/fatigue factor (longer races require proportionally more time)
21
21
  #
22
- # @param from_race [String, Symbol] known race distance ('5k', '10k', 'half_marathon', 'marathon', '100k', etc.)
22
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers (7.79,
23
+ # '7.79') or a standard race name ('5k', '10k', 'half_marathon', 'marathon', '100k', etc.)
23
24
  # @param from_time [String, Numeric] time achieved at known distance (HH:MM:SS or seconds)
24
- # @param to_race [String, Symbol] target race distance to predict
25
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
25
26
  # @return [Float] predicted time in seconds
26
- # @raise [ArgumentError] if races are invalid or distances are the same
27
+ # @raise [ArgumentError] if a race name is invalid or the distances are the same
28
+ # @raise [Calcpace::NonPositiveInputError] if a numeric distance is not positive
27
29
  #
28
30
  # @example Predict marathon time from 5K
29
31
  # predict_time('5k', '00:20:00', 'marathon')
30
- # #=> 11123.4 (approximately 3:05:23)
32
+ # #=> 11509.32 (approximately 3:11:49)
31
33
  #
32
34
  # @example Predict 10K time from half marathon
33
35
  # predict_time('half_marathon', '01:30:00', '10k')
34
- # #=> 2565.8 (approximately 42:46)
36
+ # #=> 2447.42 (approximately 40:47)
35
37
  def predict_time(from_race, from_time, to_race)
36
38
  from_distance = race_distance(from_race)
37
39
  to_distance = race_distance(to_race)
38
40
 
39
- if from_distance == to_distance
40
- raise ArgumentError,
41
- "From and to races must be different distances (both are #{from_distance}km)"
42
- end
41
+ ensure_different_distances!(from_distance, to_distance)
43
42
 
44
43
  time_seconds = from_time.is_a?(String) ? convert_to_seconds(from_time) : from_time
45
44
  check_positive(time_seconds, 'Time')
@@ -50,14 +49,14 @@ module RacePredictor
50
49
 
51
50
  # Predicts race time and returns it as a clock time string
52
51
  #
53
- # @param from_race [String, Symbol] known race distance
52
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
54
53
  # @param from_time [String, Numeric] time achieved at known distance
55
- # @param to_race [String, Symbol] target race distance to predict
54
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
56
55
  # @return [String] predicted time in HH:MM:SS format
57
56
  #
58
57
  # @example
59
58
  # predict_time_clock('5k', '00:20:00', 'marathon')
60
- # #=> '03:05:23'
59
+ # #=> '03:11:49'
61
60
  def predict_time_clock(from_race, from_time, to_race)
62
61
  predicted_seconds = predict_time(from_race, from_time, to_race)
63
62
  convert_to_clocktime(predicted_seconds)
@@ -65,14 +64,14 @@ module RacePredictor
65
64
 
66
65
  # Predicts the pace per kilometer for a target race
67
66
  #
68
- # @param from_race [String, Symbol] known race distance
67
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
69
68
  # @param from_time [String, Numeric] time achieved at known distance
70
- # @param to_race [String, Symbol] target race distance to predict
69
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
71
70
  # @return [Float] predicted pace in seconds per kilometer
72
71
  #
73
72
  # @example
74
73
  # predict_pace('5k', '00:20:00', 'marathon')
75
- # #=> 263.6 (approximately 4:24/km)
74
+ # #=> 272.77 (approximately 4:32/km)
76
75
  def predict_pace(from_race, from_time, to_race)
77
76
  predicted_seconds = predict_time(from_race, from_time, to_race)
78
77
  to_distance = race_distance(to_race)
@@ -81,14 +80,14 @@ module RacePredictor
81
80
 
82
81
  # Predicts the pace per kilometer and returns it as a clock time string
83
82
  #
84
- # @param from_race [String, Symbol] known race distance
83
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
85
84
  # @param from_time [String, Numeric] time achieved at known distance
86
- # @param to_race [String, Symbol] target race distance to predict
85
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
87
86
  # @return [String] predicted pace in MM:SS format
88
87
  #
89
88
  # @example
90
89
  # predict_pace_clock('5k', '00:20:00', 'marathon')
91
- # #=> '00:04:24' (4:24/km)
90
+ # #=> '00:04:32' (4:32/km)
92
91
  def predict_pace_clock(from_race, from_time, to_race)
93
92
  pace_seconds = predict_pace(from_race, from_time, to_race)
94
93
  convert_to_clocktime(pace_seconds)
@@ -99,18 +98,18 @@ module RacePredictor
99
98
  # This is useful for comparing performances across different race distances.
100
99
  # For example, "My 10K time is equivalent to what 5K time?"
101
100
  #
102
- # @param from_race [String, Symbol] known race distance
101
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
103
102
  # @param from_time [String, Numeric] time achieved at known distance
104
- # @param to_race [String, Symbol] target race distance for comparison
103
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
105
104
  # @return [Hash] hash with :time (seconds), :time_clock (HH:MM:SS), :pace (/km), :pace_clock
106
105
  #
107
106
  # @example
108
107
  # equivalent_performance('10k', '00:42:00', '5k')
109
108
  # #=> {
110
- # time: 1228.5,
111
- # time_clock: "00:20:28",
112
- # pace: 245.7,
113
- # pace_clock: "00:04:06"
109
+ # time: 1208.67,
110
+ # time_clock: "00:20:08",
111
+ # pace: 241.73,
112
+ # pace_clock: "00:04:01"
114
113
  # }
115
114
  def equivalent_performance(from_race, from_time, to_race)
116
115
  predicted_time = predict_time(from_race, from_time, to_race)
@@ -126,9 +125,9 @@ module RacePredictor
126
125
 
127
126
  # Predicts race time adjusted for environmental conditions
128
127
  #
129
- # @param from_race [String, Symbol] known race distance
128
+ # @param from_race [Numeric, String, Symbol] known distance in kilometers or race name
130
129
  # @param from_time [String, Numeric] time achieved at known distance
131
- # @param to_race [String, Symbol] target race distance to predict
130
+ # @param to_race [Numeric, String, Symbol] target distance in kilometers or race name
132
131
  # @param options [Hash] environmental options:
133
132
  # - :temperature [Numeric]
134
133
  # - :temperature_unit [Symbol, String] :c or :f
@@ -137,7 +136,7 @@ module RacePredictor
137
136
  #
138
137
  # @example Predict marathon time from 5K adjusted for heat (25C)
139
138
  # predict_time_adjusted('5k', '00:20:00', 'marathon', temperature: 25)
140
- # #=> { adjusted_time: 12213.4, penalty_percent: 6.0, ... }
139
+ # #=> { adjusted_time: 13140.19, penalty_percent: 14.17, ... }
141
140
  def predict_time_adjusted(from_race, from_time, to_race, **)
142
141
  predicted_seconds = predict_time(from_race, from_time, to_race)
143
142
  adjust_time(predicted_seconds, **)
@@ -7,16 +7,18 @@
7
7
  module RaceSplits
8
8
  # Calculates split times for a race
9
9
  #
10
- # @param race [String, Symbol] race distance ('5k', '10k', 'half_marathon', 'marathon', '100k', etc.)
10
+ # @param race [Numeric, String, Symbol] distance in kilometers (7.79, '7.79') or a
11
+ # standard race name ('5k', '10k', 'half_marathon', 'marathon', '100k', etc.)
11
12
  # @param target_time [String] target finish time in HH:MM:SS or MM:SS format
12
13
  # @param split_distance [String, Numeric] distance for each split ('5k', '1k', '1mile', or numeric in km)
13
14
  # @param strategy [Symbol] pacing strategy - :even (default), :negative, or :positive
14
15
  # @return [Array<String>] array of cumulative split times in HH:MM:SS format
15
- # @raise [ArgumentError] if race or split_distance is invalid
16
+ # @raise [ArgumentError] if a race or split_distance name is invalid
17
+ # @raise [Calcpace::NonPositiveInputError] if a numeric distance is not positive
16
18
  #
17
19
  # @example Even pace splits for half marathon
18
20
  # race_splits('half_marathon', target_time: '01:30:00', split_distance: '5k')
19
- # #=> ["00:21:18", "00:42:35", "01:03:53", "01:30:00"]
21
+ # #=> ["00:21:20", "00:42:40", "01:03:59", "01:25:19", "01:30:00"]
20
22
  #
21
23
  # @example Negative splits (second half faster)
22
24
  # race_splits('10k', target_time: '00:40:00', split_distance: '5k', strategy: :negative)
@@ -48,8 +50,9 @@ module RaceSplits
48
50
  # Check if it's a standard race distance
49
51
  begin
50
52
  return race_distance(distance_key)
51
- rescue ArgumentError
52
- # Not a race distance, try to parse as numeric
53
+ rescue ArgumentError, Calcpace::NonPositiveInputError
54
+ # Not a usable race distance, try to parse as numeric so that the split
55
+ # rules below (positive, not longer than the race) report the problem
53
56
  end
54
57
 
55
58
  # Try to parse as number with optional 'k' or 'km'
@@ -68,7 +71,8 @@ module RaceSplits
68
71
  #
69
72
  # @param split_km [Float] split distance in kilometers
70
73
  # @param total_distance [Float] total race distance in kilometers
71
- # @raise [ArgumentError] if split distance is invalid
74
+ # @raise [ArgumentError] if a split distance name is invalid
75
+ # @raise [Calcpace::NonPositiveInputError] if a numeric split distance is not positive
72
76
  def validate_split_distance(split_km, total_distance)
73
77
  raise ArgumentError, "Split distance must be positive" if split_km <= 0
74
78
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Calcpace
4
- VERSION = '1.14.0'
4
+ VERSION = '1.15.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calcpace
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.14.0
4
+ version: 1.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gilberto Saraiva