Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match Math.sqrt to default behaviour with Integers #43

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/ruby_units/math.rb
Expand Up @@ -7,11 +7,17 @@ module Math
alias :unit_sqrt :sqrt alias :unit_sqrt :sqrt
# @return [Numeric] # @return [Numeric]
def sqrt(n) def sqrt(n)
if Unit === n result = if Unit === n
(n**(Rational(1,2))).to_unit (n**(Rational(1,2))).to_unit
else else
unit_sqrt(n) unit_sqrt(n)
end end

if result.is_a?(Fixnum)
result.to_f
else
result
end
end end
# @return [Numeric] # @return [Numeric]
module_function :unit_sqrt module_function :unit_sqrt
Expand Down