- Mastering Julia
- Malcolm Sherrington
- 860字
- 2021-07-16 13:42:39
Integers, bits, bytes, and bools
Julia is a strongly typed language allowing the programmer to specify a variable's type precisely. However in common with most interpreted languages it does not require the type to be declared when a variable is declared, rather it infers it from the form of the declaration.
A variable in Julia is any combination of upper or lowercase letters, digits and the underscore (_
) and exclamation (!
) characters. It must start with a letter or an underscore _
. Conventionally variable names consist of lowercase letters with long names separated by underscores rather than using camel case.
To determine a variable type we can use the typeof()
function.
So typically:
julia> x = 2; typeof(x) # => gives Int64 julia> x = 2.0; typeof(x) # => gives Float64
Notice that the type (see the preceding code) starts with a capital letter and ends with a number which indicates the number of bit length of the variable. The bit length defaults to the word length of the operating system and this can be determined by examining the built-in constant WORD_SIZE
.
julia>WORD_SIZE # => 64 (on my computer)
In this section, we will be dealing with integer and boolean types.
Integers
The integer type can be any of Int8
, Int16
, Int32
, Int64
and Int128
, so the maximum integer can occupy 16 bytes of storage and can be anywhere within the range of -2^127
to (+2^127 - 1
).
If we need more precision than this Julia core implements the BigInt
type:
julia> x=BigInt(2^32); x^6 6277101735386680763835789423207666416102355444464034512896
There are a few more things to say about integers:
As well as the integer type, Julia provides the unsigned integer type Uint
. Again Uint
ranges from 8 to 128 bytes, so the maximum Uint
is (2^128 - 1
).
We can use the typemax()
and typemin()
functions to output the ranges of the Int
and Uint
types.
julia> for T = {Int8,Int16,Int32,Int64,Int128,Uint8,Uint16,Uint32,Uint64,Uint128}println("$(lpad(T,7)): [$(typemin(T)),$(typemax(T))]") end Int8: [-128,127] Int16: [-32768,32767] Int32: [-2147483648,2147483647] Int64: [-9223372036854775808,9223372036854775807] Int128: [-170141183460469231731687303715884105728, 170141183460469231731687303715884105727] Uint8: [0,255] Uint16: [0,65535] Uint32: [0,4294967295] Uint64: [0,18446744073709551615] Uint128: [0,340282366920938463463374607431768211455]
Particularly notice the use of the form of the for
statement which we will discuss when we deal with arrays and matrices later in this chapter.
Suppose we type:
julia> x = 2^32; x*x # => the answer 0
The reason is that integer overflow 'wraps' around, so squaring 2^32
gives 0
not 2^64
, since my WORD_SIZE
is 64:
julia> x = int128(2^32); x*x # => the answer we would expect 18446744073709551616
We can use the typeof()
function on a type such as Int64
to see what its parent type is.
So typeof(Int64)
gives DataType
and typeof(Uint128)
also gives DataType
.
The definition of DataType
is 'hinted' at in the core file boot.jl
; hinted at because the actual definition is implemented in C and the Julia equivalent is commented out.
The definitions of the integer types can also be found in boot.jl
, this time not commented out. Typically:
abstract Number abstract Real <: Number abstract Integer <: Real abstract Signed <: Integer bitstype 64 Int64 <: Signed
Where the <:
operator corresponds to a subclass of the parent and bitstype
again is defined in the core, nominally as #bitstype {32|64} Ptr{T}
.
If we type:
julia> x = 7; y = 5; x/y # =>this gives 1.4
So pision of two integers produces a real result. In interactive mode we can use the symbol ans
to correspond to the last answer, that is, typeof(ans)
gives Float64
.
To get the integer pisor we use the function p(x,y)
which gives 1
as expected and typeof(ans)
is Int64
. The remainder is obtained either by rem(x,y)
or by using the %
operator.
Julia has one curious operator the backslash, syntactically x\y
is equivalent to y/x
. So with x and y as before x\y
gives 0.71428
(to 5 decimal places).
Logical and arithmetic operators
As well as decimal arguments it is possible to assign binary, octal, and hexadecimal ones using the prefixes 0b
, 0o
and 0x
.
So x = 0b110101
creates the hexadecimal number 0x35
(that is, decimal 53) and typeof(ans)
is Uint8
, since 53 will 'fit' into a single byte. For larger values the type is correspondingly higher, that is x = 0b1000010110101
gives x = 0x10b5
and typeof(ans)
is Uint16
.
For operating on bits Julia provides the following: ~
(not
), |
(or
), &
(and
) and $
(xor
):
julia> x = 0xbb31; y = 0xaa5f; x$y # => 0x116e
Also we can perform arithmetic shifts using <<
(LEFT) and >>
(RIGHT) operators. Note because x
is of type Uint16
the shift operator retains that size, so:
julia> x = 0xbb31; x<<8
This gives 0x3100
(the top two nibbles being discarded) and typeof(ans)
is Uint16
.
Arithmetic shifts preserve the sign bit. This is not relevant when dealing with unsigned integers but bit-wise operators can be applied to signed integers too. Logical and arithmetic left shifts produce the same result but right shifts do not. In this case Julia provides the >>>
operator to apply a right logical shift:
julia> x = 0xbb31; y = int16(x) # => 17615
y>>4
gives -1101
and y>>>4
gives 2995
.
Booleans
Julia has the logical type Bool
. Dynamically a variable is assigned a type Bool
by equating it to the constant true
or false
(both lowercase) or to a logical expression such as:
julia> p = (2 < 3) # => true julia> typeof(p) # => Bool
Many languages treat 0, empty strings, NULLS as representing false and anything else as true. This is NOT the case in Julia however, there are cases where a Bool value may be promoted to an integer in which case true corresponds to unity.
That is, an expression such as x + p
(where x
is of type Int
and p
of type Bool
) will:
julia> x = 0xbb31; p = (2 < 3); x + p # => 0x000000000000bb32 julia>typeof(ans) # => Uint(64)
- 大學計算機基礎(第二版)
- Mastering AWS Lambda
- Beginning C++ Game Programming
- Backbone.js Blueprints
- Instant RubyMotion App Development
- Building Serverless Applications with Python
- Linux:Embedded Development
- 組態(tài)軟件技術與應用
- Swift細致入門與最佳實踐
- JavaScript腳本特效編程給力起飛
- 深入理解BootLoader
- 從0到1:HTML5 Canvas動畫開發(fā)
- Python數(shù)據(jù)科學實踐指南
- HTML5 Game Development by Example:Beginner's Guide(Second Edition)
- HTML5 and CSS3:Building Responsive Websites