Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions maths/delta_x.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

# Computes displacement from uniformly accelerated motion:
# delta_x = v0 * t + 0.5 * a * t^2
class DeltaX
class << self
def call(initial_velocity:, time:, acceleration:)
(initial_velocity * time) + (0.5 * acceleration * time * time)
end
end
end
18 changes: 18 additions & 0 deletions maths/delta_x_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'minitest/autorun'
require_relative './delta_x'

class DeltaXTest < Minitest::Test
def test_zero_acceleration
assert_in_delta 15.0, DeltaX.call(initial_velocity: 3.0, time: 5.0, acceleration: 0.0), 1E-12
end

def test_positive_acceleration
assert_in_delta 20.0, DeltaX.call(initial_velocity: 2.0, time: 4.0, acceleration: 1.5), 1E-12
end

def test_negative_acceleration
assert_in_delta 2.0, DeltaX.call(initial_velocity: 5.0, time: 2.0, acceleration: -4.0), 1E-12
end
end
Loading