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

Improve docs of big module overloads #6336

Merged
merged 1 commit into from
Jul 15, 2018
Merged
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
12 changes: 12 additions & 0 deletions src/big/big_decimal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ struct Int
include Comparable(BigDecimal)

# Converts `self` to `BigDecimal`.
# ```
# require "big"
# 1212341515125412412412421.to_big_d
# ```
def to_big_d
BigDecimal.new(self)
end
Expand Down Expand Up @@ -462,6 +466,10 @@ struct Float
#
# NOTE: Floats are fundamentally less precise than BigDecimals,
# which makes conversion to them risky.
# ```
# require "big"
# 1212341515125412412412421.0.to_big_d
# ```
def to_big_d
BigDecimal.new(self)
end
Expand All @@ -482,6 +490,10 @@ end

class String
# Converts `self` to `BigDecimal`.
# ```
# require "big"
# "1212341515125412412412421".to_big_d
# ```
def to_big_d
BigDecimal.new(self)
end
Expand Down
12 changes: 12 additions & 0 deletions src/big/big_float.cr
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ struct Number
end

class String
# Converts `self` to a `BigFloat`.
#
# ```
# require "big"
# "1234.0".to_big_f
# ```
def to_big_f
BigFloat.new(self)
end
Expand All @@ -298,6 +304,12 @@ module Math
{frac, exp}
end

# Returns the sqrt of a `BigFloat`.
#
# ```
# require "big"
# Math.sqrt((1000_000_000_0000.to_big_f*1000_000_000_00000.to_big_f))
# ```
def sqrt(value : BigFloat)
BigFloat.new { |mpf| LibGMP.mpf_sqrt(mpf, value) }
end
Expand Down
18 changes: 18 additions & 0 deletions src/big/big_int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ struct Int
end

# Returns a `BigInt` representing this integer.
# ```
# require "big"
# 123.to_big_i
# ```
def to_big_i : BigInt
BigInt.new(self)
end
Expand All @@ -535,6 +539,10 @@ struct Float
end

# Returns a `BigInt` representing this float (rounded using `floor`).
# ```
# require "big"
# 1212341515125412412412421.0.to_big_i
# ```
def to_big_i : BigInt
BigInt.new(self)
end
Expand All @@ -544,12 +552,22 @@ class String
# Returns a `BigInt` from this string, in the given *base*.
#
# Raises `ArgumentError` if this string doesn't denote a valid integer.
# ```
# require "big"
# "3a060dbf8d1a5ac3e67bc8f18843fc48".to_big_i(16)
# ```
def to_big_i(base = 10) : BigInt
BigInt.new(self, base)
end
end

module Math
# Returns the sqrt of a `BigInt`.
#
# ```
# require "big"
# Math.sqrt((1000_000_000_0000.to_big_i*1000_000_000_00000.to_big_i))
# ```
def sqrt(value : BigInt)
sqrt(value.to_big_f)
end
Expand Down
13 changes: 13 additions & 0 deletions src/big/big_rational.cr
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ struct Int
include Comparable(BigRational)

# Returns a `BigRational` representing this integer.
# ```
# require "big"
# 123.to_big_r
# ```
def to_big_r
BigRational.new(self, 1)
end
Expand Down Expand Up @@ -263,6 +267,10 @@ struct Float
include Comparable(BigRational)

# Returns a `BigRational` representing this float.
# ```
# require "big"
# 123.0.to_big_r
# ```
def to_big_r
BigRational.new(self)
end
Expand All @@ -273,6 +281,11 @@ struct Float
end

module Math
# Returns the sqrt of a `BigRational`.
# ```
# require "big"
# Math.sqrt((1000_000_000_0000.to_big_r*1000_000_000_00000.to_big_r))
# ```
def sqrt(value : BigRational)
sqrt(value.to_big_f)
end
Expand Down