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

re-enable BigInt hexadecimal literals #23546

Merged
merged 6 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
re-enable BigInt hexadecimal literals
  • Loading branch information
rfourquet committed Oct 10, 2020
commit d67442d18c137234223a4f936c216932d582d064
30 changes: 27 additions & 3 deletions doc/src/manual/integers-and-floating-point-numbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,18 @@ the [GNU Multiple Precision Arithmetic Library (GMP)](https://gmplib.org) and th
respectively. The [`BigInt`](@ref) and [`BigFloat`](@ref) types are available in Julia for arbitrary
precision integer and floating point numbers respectively.

Constructors exist to create these types from primitive numerical types, and the [string literal](@ref non-standard-string-literals) [`@big_str`](@ref) or [`parse`](@ref)
can be used to construct them from `AbstractString`s. Once created, they participate in arithmetic
with all other numeric types thanks to Julia's [type promotion and conversion mechanism](@ref conversion-and-promotion):
Constructors exist to create these types from primitive numerical types, and the
[string literal](@ref non-standard-string-literals) [`@big_str`](@ref) or [`parse`](@ref)
can be used to construct them from `AbstractString`s.
`BigInt`s can also be input as integer literals when
they are too big for other built-in integer types. Note that as there
is no unsigned arbitrary-precision integer type in `Base` (`BigInt` is
sufficient in most cases), hexadecimal, octal and binary literals can
be used (in addition to decimal literals).

Once created, they participate in arithmetic
with all other numeric types thanks to Julia's
[type promotion and conversion mechanism](@ref conversion-and-promotion):

```jldoctest
julia> BigInt(typemax(Int64)) + 1
Expand All @@ -548,6 +557,18 @@ julia> big"123456789012345678901234567890" + 1
julia> parse(BigInt, "123456789012345678901234567890") + 1
123456789012345678901234567891

julia> string(big"2"^200, base=16)
"100000000000000000000000000000000000000000000000000"

julia> 0x100000000000000000000000000000000-1 == typemax(UInt128)
true

julia> 0x000000000000000000000000000000000
0

julia> typeof(ans)
BigInt

julia> big"1.23456789012345678901"
1.234567890123456789010000000000000000000000000000000000000000000000000000000004

Expand All @@ -557,6 +578,9 @@ julia> parse(BigFloat, "1.23456789012345678901")
julia> BigFloat(2.0^66) / 3
2.459565876494606882133333333333333333333333333333333333333333333333333333333344e+19

julia> big"1.2"
1.200000000000000000000000000000000000000000000000000000000000000000000000000007

julia> factorial(BigInt(40))
815915283247897734345611269596115894272000000000
```
Expand Down
4 changes: 2 additions & 2 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@
((<= l 32) (numchk n s) (uint32 n))
((<= l 64) (numchk n s) (uint64 n))
((<= l 128) `(macrocall (core @uint128_str) (null) ,s))
(else (error "Hex or binary literal too large for UInt128")))))
(else `(macrocall (core @big_str) (null) ,s)))))

(define (sized-uint-oct-literal n s)
(if (string.find s "o0")
Expand All @@ -449,7 +449,7 @@
(begin (if (equal? s "0o") (numchk n s))
(if (oct-within-uint128? s)
`(macrocall (core @uint128_str) (null) ,s)
(error "Octal literal too large for UInt128"))))))
`(macrocall (core @big_str) (null) ,s))))))

(define (strip-leading-0s s)
(define (loop i)
Expand Down