Skip to content

feat: add free-fall motion calculations#233

Open
nickzerjeski wants to merge 1 commit intoTheAlgorithms:masterfrom
nickzerjeski:feat-free-fall-distance-194
Open

feat: add free-fall motion calculations#233
nickzerjeski wants to merge 1 commit intoTheAlgorithms:masterfrom
nickzerjeski:feat-free-fall-distance-194

Conversation

@nickzerjeski
Copy link
Copy Markdown

Summary

  • add free-fall helper methods using constant acceleration formulas
  • implement both distance from time and time from distance calculations
  • add input validation and tests

Testing

  • ruby maths/free_fall_distance_test.rb

Fixes #194

Copilot AI review requested due to automatic review settings April 13, 2026 08:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:) implementing d = 0.5 * g * t^2
  • Added FreeFallDistance.time(distance:, gravity:) implementing t = 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.

Comment on lines +5 to +7
class << self
STANDARD_GRAVITY = 9.80665

Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
class << self
STANDARD_GRAVITY = 9.80665
STANDARD_GRAVITY = 9.80665
class << self

Copilot uses AI. Check for mistakes.
Comment on lines +10 to +26
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
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment on lines +9 to +12
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?

Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Free Fall Distance

2 participants