mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-25 21:58:57 +01:00
4bb534f312
Add maths to directory
26 lines
523 B
Ruby
26 lines
523 B
Ruby
# frozen_string_literal: true
|
|
|
|
# Calculates the square root of a number
|
|
class SquareRoot
|
|
class << self
|
|
EPSILON = 1E-10
|
|
|
|
def call(number)
|
|
raise DomainError, 'Cannot find square root of negative number' if number.negative?
|
|
return 0 if number.zero?
|
|
|
|
find_root(number)
|
|
end
|
|
|
|
private
|
|
|
|
def find_root(x0, xn = x0)
|
|
xn1 = xn - ((xn * xn - x0) / (2.0 * xn))
|
|
return xn1 if (xn1 - xn).abs <= EPSILON
|
|
|
|
find_root(x0, xn1)
|
|
end
|
|
end
|
|
end
|
|
|
|
class DomainError < StandardError; end
|