Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Julia Data Types

Julia Data Types

Julia Numeric Type Hierarchy Tree

In programming languages there are basic mathematical operations and scientific computing, whose commonly used data types areIntegersandFloat (floating point number).

There is also the term **"literal"**; a literal is a notation for expressing a fixed value in source code — integers, floating-point numbers, strings, etc. are all literals.

For example:



a = 1     # a 是变量,1 是整型字面量

b = 1.0   # b 是变量,1.0 是浮点型字面量

Julia provides a rich set of primitive numeric types and defines a full set of arithmetic operations on them, as well as bitwise operators and some standard mathematical functions.


Integer type

The following table lists the integer types supported by Julia:

Type Signed? Number of bits Minimum value Maximum value
Int8 ✓ 8 -2^7 2^7 - 1
UInt8 8 0 2^8 - 1
Int16 ✓ 16 -2^15 2^15 - 1
UInt16 16 0 2^16 - 1
Int32 ✓ 32 -2^31 2^31 - 1
UInt32 32 0 2^32 - 1
Int64 ✓ 64 -2^63 2^63 - 1
UInt64 64 0 2^64 - 1
Int128 ✓ 128 -2^127 2^127 - 1
UInt128 128 0 2^128 - 1
Bool N/A 8 false (0) true (1)

Integer literal forms:

Example

julia

1

1

julia

1234

1234

The default type of integer literals depends on whether the target system is a 32-bit or 64-bit architecture (most systems today are 64-bit):

Example

64-bit systems:

julia

typeof

(

1

)

Int64

julia

Sys.

WORD_SIZE

64

Julia also defines the Int and UInt types, which are aliases for the system's signed and unsigned native integer types respectively; on 64-bit systems Int is Int64.

Literals in different bases

Julia supports hexadecimal, binary, and octal literals, all represented as unsigned integers whose type is determined by the number of digits in the literal:

Example

The 0x prefix denotes hexadecimal

julia

0x1

0x01

julia

typeof

(

0x1

)

   

1 hexadecimal digit corresponds to UInt8

UInt8

julia

typeof

(

0x123

)

 

3 hexadecimal digits correspond to UInt16

UInt16

The 0b prefix denotes binary

julia

0b10

0x02

The 0o prefix denotes octal

julia

0o010

0x08

Underscores can be used as separators to improve readability

julia

0xdead_beef

0xdeadbeef

Hexadecimal literals exceeding 128 bits automatically create a BigInt type.

typemin and typemax

The typemin() and typemax() functions query the minimum and maximum values of an integer type:

Example

julia

(

typemin

(

Int32

)

, typemax

(

Int32

)

)

(

2147483648

,

2147483647

)

julia

typemax

(

Int64

)

9223372036854775807

Overflow behavior

In Julia, exceeding the maximum value representable by a type results in wraparound behavior:

Example

julia

x = typemax

(

Int64

)

9223372036854775807

julia

x +

1

9223372036854775808

julia

x +

1

== typemin

(

Int64

)

true

Therefore, Julia's integer arithmetic is actually a form of modular arithmetic, reflecting how modern computers implement low-level arithmetic. In programs where overflow may occur, explicit checks on loops at the extreme value boundaries are necessary.

Arbitrary-precision arithmetic (BigInt)

If you need integers of unlimited range, use the BigInt type, created with the big() function, which does not overflow:

Example

10^19 overflows

julia

10

^

19

8446744073709551616

After converting to BigInt with big() the result is correct

julia

big

(

10

)

^

19

10000000000000000000

BigInt can also be created directly with the big"..." string literal

julia

big

"123456789012345678901234567890"

123456789012345678901234567890

Note:BigInt operations are much slower than native integers; use them only when very large integers are truly needed.

Division errors

Integer division (the div function) triggers a DivideError in the following two exceptional cases:

The rem (remainder) and mod (modulus) functions throw a DivideError on division by zero, as shown below:

Example

julia

mod

(

1

,

0

)

ERROR:

DivideError

: integer division error

Stacktrace:

 

[

...

]

julia

rem

(

1

,

0

)

ERROR:

DivideError

: integer division error

Stacktrace:

 

[

...

]


Floating-point types

The following table lists the floating-point types supported by Julia:

Type Precision Number of bits
Float16 Half precision 16
Float32 Single precision 32
Float64 Double precision 64

Moreover, full support for complex and rational numbers is built on top of these primitive data types.

Floating-point literal formats are as follows; E notation can be used when necessary:

Example

julia

1.0

1.0

julia

1

.

1.0

julia

0.5

0.5

julia

.5

0.5

julia

1.23

1.23

julia

1e10

1.0e10

julia

2.5e-4

0.00025

Note:In scientific notation, to keep formulas simple you can use a format with E. For example 1.03 times 10 to the 8th power can be written as "1.03E+08", where "E" is short for exponent.

The results above are all values of type Float64. Using f instead of e gives a Float32 literal:

Example

julia

x = 0.5f0

0.5f0

julia

typeof

(

x

)

Float32

julia

2.5f-

4

0.00025f0

Numbers can easily be converted to Float32 via constructors:

Example

julia

x =

Float32

(

1.5

)

-1.5f0

julia

typeof

(

x

)

Float32

Hexadecimal floating-point literals also exist, but only for values of type Float64, using p to denote a base-2 exponent:

Example

julia

0x1p0

1.0

julia

0x1.8p3

12.0

julia

x = 0x.4p-

1

0.125

julia

typeof

(

x

)

Float64

Julia also supports half-precision floating-point numbers (Float16); on hardware without half-precision instructions it is implemented via software emulation using Float32:

Example

julia

sizeof

(

Float16

(

4

.

)

)

2

julia

2

Float16

(

4

.

)

Float16

(

8.0

)

The underscore _ can be used as a digit separator:

Example

julia

10

_000,

0.000

_000_005, 0xdead_beef, 0b1011_0010

(

10000

, 5.0e-9, 0xdeadbeef, 0xb2

)

Zeros in floating-point numbers

Floating-point numbers have two zeros, positive zero and negative zero. They are equal to each other but have different binary representations, which can be viewed with the bitstring function:

Example

julia

0.0

== -

0.0

true

julia

bitstring

(

0.0

)

"0000000000000000000000000000000000000000000000000000000000000000"

julia

bitstring

(

0.0

)

"1000000000000000000000000000000000000000000000000000000000000000"

Special floating-point values

There are three specific standard floating-point values that do not correspond to any point on the real number line:

Float16 Float32 Float64 Name Description
Inf16 Inf32 Inf Positive infinity a number greater than all finite floating-point numbers
-Inf16 -Inf32 -Inf Negative infinity a number less than all finite floating-point numbers
NaN16 NaN32 NaN Not a number a value that is not equal (==) to any floating-point value, including itself

The following lists some examples of floating-point operations:

Example

julia

1

/Inf

0.0

julia

1

/

0

Inf

julia

5

/

0

-Inf

julia

0

/

0

NaN

julia

500

Inf

julia

Inf - Inf

NaN

julia

Inf

Inf

Inf

julia

Inf / Inf

NaN

julia

0

Inf

NaN

julia

NaN == NaN

false

julia

NaN

!

= NaN

true

We can also use the typemin and typemax functions to query the boundaries of floating-point types; the minimum of a floating-point type is negative infinity and the maximum is positive infinity:

Example

julia

(

typemin

(

Float16

)

, typemax

(

Float16

)

)

(

-Inf16, Inf16

)

julia

(

typemin

(

Float32

)

, typemax

(

Float32

)

)

(

-Inf32, Inf32

)

julia

(

typemin

(

Float64

)

, typemax

(

Float64

)

)

(

-Inf, Inf

)

Machine precision

Most real numbers cannot be represented exactly by floating-point numbers, so it is useful to know the distance between two adjacent representable floating-point numbers, commonly called machine precision.

Julia provides the eps function, which gives the difference between 1.0 and the next floating-point number Julia can represent:

Example

julia

eps

(

Float32

)

1.1920929f-

7

julia

eps

(

Float64

)

2.220446049250313e-16

julia

eps

(

)

Same as eps(Float64)

2.220446049250313e-16

These values are 2.0^-23 in Float32 and 2.0^-52 in Float64 respectively. The eps function can also take a floating-point value as an argument and give the absolute difference between that value and the next representable floating-point value:

Example

julia

eps

(

1.0

)

2.220446049250313e-16

julia

eps

(

1000

.

)

1.1368683772161603e-13

julia

eps

(

0.0

)

5.0e-324

The distance between two adjacent representable floating-point numbers is not constant: the smaller the value, the smaller the gap; the larger the value, the larger the gap. In other words, representable floating-point numbers are densest near zero on the real line and become exponentially sparser as you move away from zero.

Julia also provides the nextfloat and prevfloat functions, which return the next larger or smaller representable floating-point number based on the argument:

Example

julia

x = 1.25f0

1.25f0

julia

nextfloat

(

x

)

1.2500001f0

julia

prevfloat

(

x

)

1.2499999f0

Arbitrary-precision floating-point numbers (BigFloat)

Corresponding to BigInt, BigFloat provides arbitrary-precision floating-point arithmetic with a default precision of 256 bits:

Example

julia

BigFloat

(

2.0

^

66

)

/

3

2.459565876494606882133333333333333333333333333333333333333333333333333333333344e19

You can specify the precision (number of significant bits) with setprecision

julia

setprecision

(

40

)

do

           BigFloat

(

1

)

(

BigFloat,

"0.1"

)

       

end

1.1000000000004


Rounding modes

A number without an exact floating-point representation must be rounded to a suitable representable value.

The default mode used by Julia is always RoundNearest, meaning rounding to the nearest representable value; when the distances are equal (exactly in the middle), it rounds to the side with an even significand, a strategy also called "banker's rounding":

Example

julia

BigFloat

(

"1.510564889"

,

2

, RoundNearest

)

1.5

julia

BigFloat

(

"1.550564889"

,

2

, RoundNearest

)

1.5

julia

BigFloat

(

"1.560564889"

,

2

, RoundNearest

)

1.5


Literals for 0 and 1

Julia provides literal functions for 0 and 1 that return 0 and 1 of a specific type or of the type of a given variable:

function Description
zero(x) the zero literal of type x or of the type of variable x
one(x) the one literal of type x or of the type of variable x

These functions can be used in numeric comparisons and generic code to avoid the overhead of unnecessary type conversions:

Example

julia

zero

(

Float32

)

0.0f0

julia

zero

(

1.0

)

0.0

julia

one

(

Int32

)

1

julia

one

(

BigFloat

)

1.0


Numeric literal coefficients

To make mathematical formulas more natural to write, Julia allows placing a numeric literal directly before a variable to denote multiplication:

Example

julia

x =

3

3

2x^2 - 3x + 1 is equivalent to 2x^2 - 3x + 1

julia

2x^

2

1

10

Numeric literals can also multiply parenthesized expressions

julia

2

(

x -

1

)

^

2

3

(

x -

1

)

1

3

Note:The precedence of numeric literal coefficients is lower than unary negation but higher than other multiplication/division operations:-2x is parsed as (-2)*x, while 2^3x is parsed as 2^(3x). Moreover, two adjacent parenthesized expressions (such as (x-1)(x+1)) do not mean multiplication; they are treated as a function call and raise an error.


Type Conversion

Type conversion is converting a variable from one type to another data type. Julia supports three kinds of numeric conversion, which differ in how they handle inexact conversions.

The first: T(x) or convert(T, x)

All the above forms convert x to type T:

The second: x % T

x % T converts the integer x to integer type T; the result is consistent with x modulo 2^n (n is the number of bits of T), i.e. it truncates to the low n bits of the binary representation, without raising an error:

The third: rounding functions

Rounding functions accept an optional argument of type T; for example, round(Int, x) is a shorthand for Int(round(x)).

Example

julia

Int8

(

127

)

127

128 is outside the representable range of Int8, throwing InexactError

julia

Int8

(

128

)

ERROR: InexactError: trunc

(

Int8

,

128

)

Stacktrace:

[

...

]

3.14 cannot be represented exactly as an integer, also throwing InexactError

julia

Int8

(

3.14

)

ERROR: InexactError:

Int8

(

3.14

)

Stacktrace:

[

...

]

% conversion does not raise an error; 128 mod 256 gives -128

julia

127

%

Int8

127

julia

128

%

Int8

128

julia

round

(

Int8

,

127.4

)

127

127.6 rounds to 128, which is outside the Int8 range, throwing InexactError

julia

round

(

Int8

,

127.6

)

ERROR: InexactError:

Int8

(

128.0

)

Stacktrace:

[

...

]