数字类型 #

一、整数类型 #

1.1 整数类型列表 #

Julia提供了多种整数类型:

类型 位数 最小值 最大值
Int8 8 -128 127
Int16 16 -32768 32767
Int32 32 -2147483648 2147483647
Int64 64 -2^63 2^63-1
Int128 128 -2^127 2^127-1
UInt8 8 0 255
UInt16 16 0 65535
UInt32 32 0 4294967295
UInt64 64 0 2^64-1
UInt128 128 0 2^128-1
BigInt 任意 无限制 无限制

1.2 默认整数类型 #

系统默认的整数类型是Int

julia
typeof(42)
Sys.WORD_SIZE

1.3 整数字面量 #

julia
x = 42
y = 0xFF
z = 0o755
w = 0b1010

1.4 整数溢出 #

整数运算会溢出(wrap around):

julia
x = typemax(Int64)
x + 1
x + 1 == typemin(Int64)

使用BigInt避免溢出:

julia
x = big(typemax(Int64))
x + 1

1.5 整数类型转换 #

julia
Int8(127)
Int8(128)
UInt8(255)
UInt8(-1)

二、浮点数类型 #

2.1 浮点数类型列表 #

类型 位数 精度
Float16 16 半精度
Float32 32 单精度
Float64 64 双精度
BigFloat 任意 任意精度

2.2 浮点数字面量 #

julia
x = 3.14
y = 1.0e-10
z = 1.0f-10
w = 0.5

2.3 特殊浮点值 #

julia
Inf
-Inf
NaN
0.0
-0.0

检查特殊值:

julia
isinf(Inf)
isnan(NaN)
isfinite(3.14)
iszero(0.0)

2.4 浮点数精度 #

julia
0.1 + 0.2
0.1 + 0.2 == 0.3
isapprox(0.1 + 0.2, 0.3)

使用BigFloat提高精度:

julia
setprecision(256) do
    big(0.1) + big(0.2)
end

2.5 浮点数比较 #

julia
a = 0.1 + 0.2
b = 0.3

a == b
a ≈ b
isapprox(a, b)
isapprox(a, b, atol=1e-10)

三、复数 #

3.1 复数创建 #

Julia使用im后缀表示虚部:

julia
z1 = 1 + 2im
z2 = 3 - 4im
z3 = complex(1, 2)

3.2 复数运算 #

julia
z1 = 1 + 2im
z2 = 3 + 4im

z1 + z2
z1 - z2
z1 * z2
z1 / z2
z1^2

3.3 复数函数 #

julia
z = 3 + 4im

real(z)
imag(z)
conj(z)
abs(z)
abs2(z)
angle(z)

3.4 复数类型 #

julia
typeof(1 + 2im)
typeof(1.0 + 2.0im)
typeof(1f0 + 2f0im)

四、有理数 #

4.1 有理数创建 #

使用//运算符创建有理数:

julia
r1 = 1 // 2
r2 = 3 // 4
r3 = 6 // 8

4.2 有理数运算 #

julia
r1 = 1 // 2
r2 = 1 // 3

r1 + r2
r1 - r2
r1 * r2
r1 / r2
r1 ^ 3

4.3 有理数函数 #

julia
r = 6 // 8

numerator(r)
denominator(r)
float(r)

4.4 有理数与浮点数 #

julia
1 // 2 == 0.5
1 // 3 == 1 / 3
float(1 // 3)

五、数学运算 #

5.1 算术运算 #

julia
+    -    *    /    ÷    \    ^    %

示例:

julia
1 + 2
10 - 3
4 * 5
10 / 3
10 ÷ 3
10 \ 3
2 ^ 10
10 % 3

5.2 位运算 #

julia
~(0b1010)
0b1010 & 0b1100
0b1010 | 0b1100
0b1010 ⊻ 0b1100
0b1010 >> 2
0b1010 << 2

5.3 数学函数 #

julia
abs(-5)
sign(-5)
sqrt(16)
cbrt(27)
exp(1)
log(ℯ)
log10(100)
log2(8)

5.4 三角函数 #

julia
sin(π/6)
cos(π/3)
tan(π/4)
asin(0.5)
acos(0.5)
atan(1)

使用角度:

julia
sind(30)
cosd(60)
tand(45)

5.5 双曲函数 #

julia
sinh(1)
cosh(1)
tanh(1)

5.6 取整函数 #

julia
round(3.7)
floor(3.7)
ceil(3.7)
trunc(3.7)
trunc(-3.7)

指定小数位数:

julia
round(3.14159, digits=2)
round(3.14159, digits=4)

六、类型转换 #

6.1 显式转换 #

julia
Int(3.14)
Float64(10)
Int8(100)
Float32(3.14)

6.2 安全转换 #

使用truncround

julia
trunc(Int, 3.7)
round(Int, 3.7)
floor(Int, 3.7)
ceil(Int, 3.7)

6.3 字符串转换 #

julia
parse(Int, "42")
parse(Float64, "3.14")
string(42)
string(3.14)

6.4 promote #

promote将参数转换为共同类型:

julia
promote(1, 2.0)
promote(1, 2.0, 3f0)
promote(1//2, 0.5)

七、类型层次 #

7.1 数字类型层次 #

text
Number
├── Complex
└── Real
    ├── AbstractFloat
    │   ├── Float16
    │   ├── Float32
    │   ├── Float64
    │   └── BigFloat
    ├── Integer
    │   ├── Bool
    │   ├── Signed
    │   │   ├── Int8
    │   │   ├── Int16
    │   │   ├── Int32
    │   │   ├── Int64
    │   │   └── Int128
    │   └── Unsigned
    │       ├── UInt8
    │       ├── UInt16
    │       ├── UInt32
    │       ├── UInt64
    │       └── UInt128
    └── Rational

7.2 类型检查 #

julia
10 isa Integer
10 isa Real
10 isa Number
3.14 isa AbstractFloat
1 + 2im isa Complex

八、数学常量 #

8.1 内置常量 #

julia
π
ℯ
γ
φ
catalan

8.2 使用常量 #

julia
sin(π/2)
exp(ℯ)
log(ℯ)

九、随机数 #

9.1 随机数生成 #

julia
rand()
rand(1:100)
rand(1.0:0.1:10.0)
rand(Int)
rand(Float64)

9.2 随机数组 #

julia
rand(5)
rand(2, 3)
randn(5)
randn(2, 3)

9.3 随机种子 #

julia
using Random
Random.seed!(42)
rand(5)
Random.seed!(42)
rand(5)

十、实践练习 #

10.1 练习1:整数运算 #

julia
function factorial(n::Int)
    result = 1
    for i in 1:n
        result *= i
    end
    return result
end

factorial(20)
factorial(big(30))

10.2 练习2:浮点数比较 #

julia
function compare_floats(a::Float64, b::Float64, tol::Float64=1e-10)
    if abs(a - b) < tol
        println("$a ≈ $b")
    else
        println("$a ≠ $b")
    end
end

compare_floats(0.1 + 0.2, 0.3)

10.3 练习3:复数运算 #

julia
function mandelbrot(c::Complex, max_iter::Int=100)
    z = 0.0 + 0.0im
    for i in 1:max_iter
        z = z^2 + c
        if abs(z) > 2
            return i
        end
    end
    return max_iter
end

mandelbrot(-0.5 + 0.5im)

10.4 练习4:有理数运算 #

julia
function harmonic(n::Int)
    sum = 0 // 1
    for i in 1:n
        sum += 1 // i
    end
    return sum
end

harmonic(10)
float(harmonic(10))

十一、总结 #

本章我们学习了:

  1. 整数类型:各种大小的有符号和无符号整数
  2. 浮点数类型:Float16、Float32、Float64、BigFloat
  3. 复数:使用im后缀创建复数
  4. 有理数:使用//运算符创建有理数
  5. 数学运算:算术、位运算、数学函数
  6. 类型转换:显式转换和字符串转换
  7. 随机数:rand和randn函数

接下来让我们学习Julia的字符串类型!

最后更新:2026-03-27