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

Allow underscores in the String passed to Big* constructors #7107

Merged
merged 1 commit into from
Nov 26, 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
3 changes: 3 additions & 0 deletions spec/std/big/big_decimal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ describe BigDecimal do
BigDecimal.new("42.0123")
.should eq(BigDecimal.new(BigInt.new(420123), 4))

BigDecimal.new("42_42_42_24.0123_456_789")
.should eq(BigDecimal.new(BigInt.new(424242240123456789), 10))

BigDecimal.new("0.0")
.should eq(BigDecimal.new(BigInt.new(0)))

Expand Down
1 change: 1 addition & 0 deletions spec/std/big/big_float_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe "BigFloat" do
bigfloat_of_float_value.to_s.should eq(string_of_float_value)
BigFloat.new("+#{string_of_integer_value}").to_s.should eq(string_of_integer_value)
BigFloat.new("-#{string_of_integer_value}").to_s.should eq("-#{string_of_integer_value}")
BigFloat.new("123_456_789.123_456_789").to_s.should eq("123456789.123456789")
end

it "raises an ArgumentError unless string denotes valid float" do
Expand Down
1 change: 1 addition & 0 deletions spec/std/big/big_int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe "BigInt" do

it "creates from string" do
BigInt.new("12345678").to_s.should eq("12345678")
BigInt.new("123_456_78").to_s.should eq("12345678")
BigInt.new("+12345678").to_s.should eq("12345678")
BigInt.new("-12345678").to_s.should eq("-12345678")
end
Expand Down
2 changes: 2 additions & 0 deletions src/big/big_decimal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct BigDecimal < Number
def initialize(str : String)
# Strip leading '+' char to smooth out cases with strings like "+123"
str = str.lchop('+')
# Strip '_' to make it compatible with int literals like "1_000_000"
str = str.delete('_')

raise InvalidBigDecimalException.new(str, "Zero size") if str.bytesize == 0

Expand Down
2 changes: 2 additions & 0 deletions src/big/big_float.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct BigFloat < Float
def initialize(str : String)
# Strip leading '+' char to smooth out cases with strings like "+123"
str = str.lchop('+')
# Strip '_' to make it compatible with int literals like "1_000_000"
str = str.delete('_')
if LibGMP.mpf_init_set_str(out @mpf, str, 10) == -1
raise ArgumentError.new("Invalid BigFloat: #{str.inspect}")
end
Expand Down
3 changes: 3 additions & 0 deletions src/big/big_int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ struct BigInt < Int
#
# ```
# BigInt.new("123456789123456789123456789123456789") # => 123456789123456789123456789123456789
# BigInt.new("123_456_789_123_456_789_123_456_789") # => 123456789012345678901234567890
# BigInt.new("1234567890ABCDEF", base: 16) # => 1311768467294899695
# ```
def initialize(str : String, base = 10)
# Strip leading '+' char to smooth out cases with strings like "+123"
str = str.lchop('+')
# Strip '_' to make it compatible with int literals like "1_000_000"
str = str.delete('_')
err = LibGMP.init_set_str(out @mpz, str, base)
if err == -1
raise ArgumentError.new("Invalid BigInt: #{str}")
Expand Down