Julia Core package

Coreはビルトイン(組込み)と見なされる全ての識別子を含む標準モジュールである。つまり、ライブラリではなく、Julia言語の一部である。これらの定義無しには何もできないため、全てのモジュールは暗黙的に「using Core」を含む。

目次

  1. 1 Constant
    1. 1.1 nothing
  2. 2 Type
    1. 2.1 AbstractFloat
    2. 2.2 AbstractIrrational
    3. 2.3 ArgumentError
    4. 2.4 Array
    5. 2.5 Bool
    6. 2.6 BoundsError
    7. 2.7 Complex
    8. 2.8 DivideError
    9. 2.9 DomainError
    10. 2.10 EOFError
    11. 2.11 ErrorException
    12. 2.12 Float16
    13. 2.13 Float32
    14. 2.14 Float64
    15. 2.15 Int8
    16. 2.16 Int16
    17. 2.17 Int32
    18. 2.18 Int64
    19. 2.19 Int128
    20. 2.20 Integer
    21. 2.21 InterruptException
    22. 2.22 Irrational
    23. 2.23 NamedTuple
    24. 2.24 Nothing
    25. 2.25 Number
    26. 2.26 Rational
    27. 2.27 Real
    28. 2.28 Signed
    29. 2.29 StringIndexError
    30. 2.30 SystemError
    31. 2.31 Tuple
    32. 2.32 TypeError
    33. 2.33 UInt8
    34. 2.34 UInt16
    35. 2.35 UInt32
    36. 2.36 UInt64
    37. 2.37 UInt128
    38. 2.38 UndefRefError
    39. 2.39 UndefVarError
    40. 2.40 Unsigned
    41. 2.41 Union
  3. 3 Function
    1. 3.1 tuple
    2. 3.2 typeof
  4. 4 Method
    1. 4.1 Float32
    2. 4.2 Float64
  5. 5 参考文献

1 Constant

1.1 Core.nothing

Nothing型のシングルトン・インスタンスで、(C言語のvoid関数のように)返す値がない場合や、変数やフィールドが値を持たない場合に慣習的に使われる。

julia> NullableInt64 = Union{Int64, Nothing}
Union{Nothing, Int64}

julia> x::NullableInt64 = nothing

julia> x = 1
1

2 Type

2.1 Core.AbstractFloat

すべての浮動小数点数の抽象スーパータイプ

Number (Abstract Type)
└─ Real (Abstract Type)
   └─ AbstractFloat (Abstract Type)
      ├─ Float16
      ├─ Float32
      ├─ Float64
      └─ BigFloat

2.2 Core.AbstractIrrational

他の数値との算術演算において、自動的に正しい精度に丸められる。

Number (Abstract Type)
└─ Real (Abstract Type)
   └─ AbstractIrrational (Abstract Type)
      └─ Irrational

2.3 Core.ArgumentError

Core.ArgumentError は、関数に渡された引数が無効であるときに発生するエラーである。

julia> Base.ascii("π")
ERROR: ArgumentError: invalid ASCII at index 1 in "π"

2.5 Core.Bool

Juliaにおける Core.Bool は、true (1) または false (0) を表すブール型である。

Bool <: Integer
Number (Abstract Type)
└─ Real (Abstract Type)
   └─ Integer (Abstract Type)
      └─ Bool

2.7 Core.Complex

Complex{T<:Real} <: Number

T型の実数部と虚数部を持つ複素数型。

Number (Abstract Type)
└─ Complex

2.12 Core.Float16

Juliaにおける Core.Float16 は、IEEE 754で規定されている半精度の浮動小数点数を表すデータ型である。

IEEE 754 half precision
ビット数
符号部 1 bit
指数部 5 bit
仮数部 10 bit

2.13 Core.Float32

Juliaにおける Core.Float32 は、IEEE 754で規定されている単精度の浮動小数点数を表すデータ型である。

IEEE 754 single precision
ビット数
符号部 1 bit
指数部 8 bit
仮数部 23 bit

型変換関数を使ってデータ型を変換することができる。

julia> Float32(pi)
3.1415927f0

julia> Float64(pi)
3.141592653589793

julia> BigFloat(pi)
3.141592653589793238462643383279502884197169399375105820974944592307816406286198

2.14 Float64

Juliaにおける Core.Float64 は、IEEE 754で規定されている倍精度の浮動小数点数を表すデータ型である。

IEEE 754 double precision
ビット数
符号部 1 bit
指数部 11 bit
仮数部 52 bit

データ型を指定して計算する。

julia> (pi * 2)::Float64
6.283185307179586

2.15 Core.Int8

Juliaにおける Core.Int8 は、8ビットの符号あり整数型である。最小値 -1×27 から最大値 27-1 までの整数を表すことができる。

ローカル変数のデータ型として Int8 を指定する例を次に示す。

local x::Int8

2.16 Core.Int16

Juliaにおける Core.Int16 は、16ビットの符号あり整数型である。最小値 -1×215 から最大値 215-1 までの整数を表すことができる。

引数のデータ型として Int16 を指定する例を次に示す。

function f(x::Int16)
  x / 2
end

2.17 Core.Int32

Juliaにおける Core.Int32 は、32ビットの符号あり整数型である。最小値 -1×231 から最大値 231-1 までの整数を表すことができる。

関数の戻り値のデータ型として Int32 を指定する例を次に示す。

function f(x)::Int32
  x * 2
end

2.18 Core.Int64

Juliaにおける Core.Int64 は、64ビットの符号あり整数型である。最小値 -1×263 から最大値 263-1 までの整数を表すことができる。

2.19 Core.Int128

Juliaにおける Core.Int128 は、128ビットの符号あり整数型である。最小値 -1×2127 から最大値 2127-1 までの整数を表すことができる。

2.20 Core.Integer

すべての整数を表す抽象スーパータイプ

Number (Abstract Type)
└─ Real (Abstract Type)
   └─ Integer (Abstract Type)
      ├─ Bool
      ├─ Signed (Abstract Type)
      │  ├─ Int8
      │  ├─ Int16
      │  ├─ Int32
      │  ├─ Int64
      │  ├─ Int128
      │  └─ BigInt
      └─ Unsigned (Abstract Type)
         ├─ UInt8
         ├─ UInt16
         ├─ UInt32
         ├─ UInt64
         └─ UInt128

2.22 Core.Irrational

Irrational{sym} <: AbstractIrrational

symで示される正確な無理数値を表す数値型

2.24 Core.Nothing

フィールドを持たない型

julia> NullableInt64 = Union{Int64, Nothing}
Union{Nothing, Int64}

julia> x::NullableInt64 = nothing

julia> x = 1
1

2.25 Core.Number

Core.Number は全ての数値型における抽象スーパータイプであり、以下に示す派生型がある。

Number (Abstract Type)
├─ Complex
└─ Real (Abstract Type)
   ├─ AbstractFloat (Abstract Type)
   │  ├─ Float16
   │  ├─ Float32
   │  ├─ Float64
   │  └─ BigFloat
   ├─ Integer (Abstract Type)
   │  ├─ Bool
   │  ├─ Signed (Abstract Type)
   │  │  ├─ Int8
   │  │  ├─ Int16
   │  │  ├─ Int32
   │  │  ├─ Int64
   │  │  ├─ Int128
   │  │  └─ BigInt
   │  └─ Unsigned (Abstract Type)
   │     ├─ UInt8
   │     ├─ UInt16
   │     ├─ UInt32
   │     ├─ UInt64
   │     └─ UInt128
   ├─ Rational
   └─ AbstractIrrational (Abstract Type)
      └─ Irrational

2.26 Core.Rational

Rational{T<:Integer} <: Real

分子と分母がT型の有理数型。有理数はオーバーフローがチェックされる。

2.27 Core.Real

すべての実数を表す抽象的なスーパータイプ

2.28 Core.Signed

すべての符号付き整数の抽象スーパータイプ

2.33 Core.UInt8

Juliaにおける Core.UInt8 は、8ビットの符号なし整数型である。最小値 0 から最大値 28-1 までの整数を表すことができる。

2.34 Core.UInt16

Juliaにおける Core.UInt16 は、16ビットの符号なし整数型である。最小値 0 から最大値 216-1 までの整数を表すことができる。

2.35 Core.UInt32

Juliaにおける Core.UInt32 は、32ビットの符号なし整数型である。最小値 0 から最大値 232-1 までの整数を表すことができる。

2.36 Core.UInt64

Juliaにおける Core.UInt64 は、64ビットの符号なし整数型である。最小値 0 から最大値 264-1 までの整数を表すことができる。

2.37 Core.UInt128

Juliaにおける Core.UInt128 は、64ビットの符号なし整数型である。最小値 0 から最大値 2128-1 までの整数を表すことができる。

2.40 Unsigned

すべての符号なし整数の抽象スーパータイプ

2.41 Core.Union

Core.Union は共用体を定義する型である。共用体とは、複数の型を受け入れることができる型である。

Core.Int64 および Core.Nothing の両方を受け入れる型と変数を宣言する例を以下に示す。

julia> NullableInt64 = Union{Int64, Nothing}
Union{Nothing, Int64}

julia> x::NullableInt64 = nothing

julia> x = 1
1

3 Function

3.1 Core.tuple

Core.tuple 関数を使って Core.Tuple を生成することができる。

tuple(xs...)
julia> t = tuple(1, 2, 3)
(1, 2, 3)

3.2 typeof

Julia のデータ型は Core.typeof 関数で確認できる。

typeof(x)
julia> typeof(1)
Int64

julia> typeof(1.0)
Float64

julia> typeof(true)
Bool

julia> typeof('A')
Char

julia>; typeof('あ')
Char

julia> typeof("ABC")
String

julia> typeof(r"AB*")
Regex

julia>

4 Method

4.1 Float32

Float32(x)
Float32(x, mode::RoundingMode)
x
数値
mode
丸めモードを Base.Rounding.RoundingMode 型で指定する。
RoundingMode
説明
RoundNearest default
RoundNearestTiesAway
RoundNearestTiesUp
RoundToZero
RoundFromZero
RoundUp
RoundDown

Core.Float32() は数値のデータ型を Core.Float32 に変換するメソッドである。端数は絶対値が大きい方へ丸められる。

julia> Float32(1/3)
0.33333334f0

julia> Float32(-1/3)
-0.33333334f0

第2引数に RoundDown を指定することで、端数を小さい方へ切り下げることができる。

julia> Float32(1/3, RoundDown)
0.3333333f0

julia> Float32(-1/3, RoundDown)
-0.33333334f0

第2引数に RoundUp を指定することで、端数を大きい方へ切り上げることができる。

julia> Float32(1/3, RoundUp)
0.33333334f0

julia> Float32(-1/3, RoundUp)
-0.3333333f0

4.2 Float64

Float64(x)
Float64(x, mode::RoundingMode)
x
数値
mode
丸めモードを Base.Rounding.RoundingMode 型で指定する。
RoundingMode
説明
RoundNearest default
RoundNearestTiesAway
RoundNearestTiesUp
RoundToZero
RoundFromZero
RoundUp
RoundDown

型変換関数を使ってデータ型を変換することができる。

julia> Float64(pi)
3.141592653589793

julia> Float64(pi * 2)
6.283185307179586

julia>

5 参考文献

Institute of Electrical and Electronics Engineers (2022) 754-1985 - IEEE Standard for Binary Floating-Point Arithmetic

Institute of Electrical and Electronics Engineers (2022) 754-2019 - IEEE Standard for Floating-Point Arithmetic