Skip to content

Commit

Permalink
Adds code samples on the big module to methods that are extending cla…
Browse files Browse the repository at this point in the history
…sses that are part of the standard library
  • Loading branch information
laginha87 committed Jul 5, 2018
1 parent f446e1b commit d7fec28
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
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 `BigInt`.
#
# ```
# 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

0 comments on commit d7fec28

Please sign in to comment.