feat: add free-fall motion calculations#233
feat: add free-fall motion calculations#233nickzerjeski wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new maths helper for free-fall motion calculations (constant acceleration under gravity), addressing Issue #194 by providing both “distance from time” and “time from distance” APIs with basic domain validation.
Changes:
- Added
FreeFallDistance.distance(time:, gravity:)implementingd = 0.5 * g * t^2 - Added
FreeFallDistance.time(distance:, gravity:)implementingt = sqrt(2d / g) - Added Minitest coverage for the core formulas and negative time/distance validation
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| maths/free_fall_distance.rb | Introduces free-fall distance/time helpers, gravity constant default, and domain validation. |
| maths/free_fall_distance_test.rb | Adds unit tests validating the formulas and negative-input error behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| class << self | ||
| STANDARD_GRAVITY = 9.80665 | ||
|
|
There was a problem hiding this comment.
STANDARD_GRAVITY is defined inside class << self, so it is not accessible as FreeFallDistance::STANDARD_GRAVITY (it ends up on the singleton class). If you want this constant to be part of the public API (or avoid confusion), define it in the FreeFallDistance class body (outside class << self) and reference it from the class methods.
| class << self | |
| STANDARD_GRAVITY = 9.80665 | |
| STANDARD_GRAVITY = 9.80665 | |
| class << self |
| raise DomainError, 'time must be non-negative' if time.negative? | ||
| raise DomainError, 'gravity must be positive' unless gravity.positive? | ||
|
|
||
| 0.5 * gravity * time * time | ||
| end | ||
|
|
||
| # t = sqrt(2d / g) | ||
| def time(distance:, gravity: STANDARD_GRAVITY) | ||
| raise DomainError, 'distance must be non-negative' if distance.negative? | ||
| raise DomainError, 'gravity must be positive' unless gravity.positive? | ||
|
|
||
| Math.sqrt((2.0 * distance) / gravity) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| class DomainError < StandardError; end |
There was a problem hiding this comment.
This file defines a top-level DomainError class, which will be redefined (and can produce warnings/override behavior) if users require multiple maths helpers that also define DomainError (e.g., maths/square_root.rb). Prefer using a built-in exception like ArgumentError, or namespace the error under FreeFallDistance (or define DomainError once in a shared file and require it).
| raise DomainError, 'time must be non-negative' if time.negative? | |
| raise DomainError, 'gravity must be positive' unless gravity.positive? | |
| 0.5 * gravity * time * time | |
| end | |
| # t = sqrt(2d / g) | |
| def time(distance:, gravity: STANDARD_GRAVITY) | |
| raise DomainError, 'distance must be non-negative' if distance.negative? | |
| raise DomainError, 'gravity must be positive' unless gravity.positive? | |
| Math.sqrt((2.0 * distance) / gravity) | |
| end | |
| end | |
| end | |
| class DomainError < StandardError; end | |
| raise ArgumentError, 'time must be non-negative' if time.negative? | |
| raise ArgumentError, 'gravity must be positive' unless gravity.positive? | |
| 0.5 * gravity * time * time | |
| end | |
| # t = sqrt(2d / g) | |
| def time(distance:, gravity: STANDARD_GRAVITY) | |
| raise ArgumentError, 'distance must be non-negative' if distance.negative? | |
| raise ArgumentError, 'gravity must be positive' unless gravity.positive? | |
| Math.sqrt((2.0 * distance) / gravity) | |
| end | |
| end | |
| end |
| def distance(time:, gravity: STANDARD_GRAVITY) | ||
| raise DomainError, 'time must be non-negative' if time.negative? | ||
| raise DomainError, 'gravity must be positive' unless gravity.positive? | ||
|
|
There was a problem hiding this comment.
Gravity validation (gravity must be positive) is new behavior but is not covered by tests. Add test cases asserting a DomainError is raised for zero/negative gravity in both .distance and .time (or at least one shared validation path if you refactor).
Summary
Testing
ruby maths/free_fall_distance_test.rbFixes #194