基本数据类型 #
一、整数类型 #
1.1 整数类型列表 #
| 类型 | 位数 | 范围 |
|---|---|---|
Int8 |
8位 | -128 到 127 |
Int16 |
16位 | -32768 到 32767 |
Int32 |
32位 | -2147483648 到 2147483647 |
Int64 |
64位 | -9223372036854775808 到 9223372036854775807 |
UInt8 |
8位 | 0 到 255 |
UInt16 |
16位 | 0 到 65535 |
UInt32 |
32位 | 0 到 4294967295 |
UInt64 |
64位 | 0 到 18446744073709551615 |
Int |
平台相关 | 平台字长 |
1.2 整数定义 #
mojo
def main():
let small: Int8 = 127
let medium: Int32 = 2147483647
let large: Int64 = 9223372036854775807
let unsigned_small: UInt8 = 255
let unsigned_large: UInt64 = 18446744073709551615
print(small, medium, large)
main()
1.3 整数字面量 #
mojo
def main():
let decimal = 42
let hex = 0x2A
let binary = 0b101010
let octal = 0o52
print(decimal)
print(hex)
print(binary)
print(octal)
main()
1.4 整数运算 #
mojo
def main():
let a: Int = 10
let b: Int = 3
print(a + b)
print(a - b)
print(a * b)
print(a // b)
print(a % b)
main()
1.5 溢出处理 #
mojo
def main():
var x: Int8 = 127
x = x + 1
main()
二、浮点数类型 #
2.1 浮点类型列表 #
| 类型 | 位数 | 精度 |
|---|---|---|
Float16 |
16位 | 半精度 |
Float32 |
32位 | 单精度 |
Float64 |
64位 | 双精度 |
2.2 浮点数定义 #
mojo
def main():
let single: Float32 = 3.14159
let double: Float64 = 3.14159265358979
print(single)
print(double)
main()
2.3 科学计数法 #
mojo
def main():
let large = 1.5e10
let small = 1.5e-10
print(large)
print(small)
main()
2.4 特殊浮点值 #
mojo
def main():
let inf = Float64("inf")
let neg_inf = Float64("-inf")
let nan = Float64("nan")
print(inf)
print(neg_inf)
print(nan)
main()
2.5 浮点运算 #
mojo
def main():
let a: Float64 = 10.0
let b: Float64 = 3.0
print(a + b)
print(a - b)
print(a * b)
print(a / b)
main()
三、布尔类型 #
3.1 布尔值定义 #
mojo
def main():
let is_true: Bool = True
let is_false: Bool = False
print(is_true)
print(is_false)
main()
3.2 布尔运算 #
mojo
def main():
let a = True
let b = False
print(a and b)
print(a or b)
print(not a)
main()
3.3 比较结果 #
mojo
def main():
let x = 10
let y = 20
let result1 = x < y
let result2 = x == y
let result3 = x >= y
print(result1)
print(result2)
print(result3)
main()
3.4 布尔转换 #
mojo
def main():
let num = 0
let bool_val = Bool(num)
print(bool_val)
main()
四、字符类型 #
4.1 字符定义 #
mojo
def main():
let letter: String = 'A'
let digit: String = '7'
let symbol: String = '@'
print(letter)
print(digit)
print(symbol)
main()
4.2 转义字符 #
| 转义序列 | 描述 |
|---|---|
\n |
换行 |
\t |
制表符 |
\\ |
反斜杠 |
\' |
单引号 |
\" |
双引号 |
\r |
回车 |
mojo
def main():
print("Hello\nWorld")
print("Tab\there")
print("Quote: \"test\"")
main()
五、类型转换 #
5.1 显式转换 #
mojo
def main():
let int_val: Int = 42
let float_val: Float64 = Float64(int_val)
let str_val: String = String(int_val)
print(int_val)
print(float_val)
print(str_val)
main()
5.2 数值类型转换 #
mojo
def main():
let small: Int8 = 100
let large: Int64 = Int64(small)
let float_val: Float64 = 3.7
let int_val: Int = Int(float_val)
print(large)
print(int_val)
main()
5.3 字符串转数值 #
mojo
def main():
let str_num = "12345"
let int_val = Int(str_num)
let str_float = "3.14159"
let float_val = Float64(str_float)
print(int_val)
print(float_val)
main()
六、类型推断 #
6.1 字面量推断 #
mojo
def main():
let int_val = 42
let float_val = 3.14
let bool_val = True
let str_val = "Hello"
main()
6.2 上下文推断 #
mojo
fn process(value: Float64):
print(value)
def main():
process(42)
main()
七、类型别名 #
7.1 定义类型别名 #
mojo
alias UserID = Int64
alias Price = Float64
def main():
let user_id: UserID = 12345
let price: Price = 99.99
print(user_id)
print(price)
main()
7.2 泛型类型别名 #
mojo
alias Vector3 = SIMD[DType.float64, 3]
def main():
let v = Vector3(1.0, 2.0, 3.0)
print(v)
main()
八、SIMD类型 #
8.1 SIMD基础 #
mojo
from SIMD import SIMD
def main():
let v = SIMD[DType.float64, 4](1.0, 2.0, 3.0, 4.0)
print(v)
main()
8.2 SIMD运算 #
mojo
from SIMD import SIMD
def main():
let a = SIMD[DType.float64, 4](1.0, 2.0, 3.0, 4.0)
let b = SIMD[DType.float64, 4](5.0, 6.0, 7.0, 8.0)
let sum = a + b
let product = a * b
print(sum)
print(product)
main()
九、总结 #
本章学习了:
- 整数类型及其范围
- 浮点数类型及精度
- 布尔类型
- 字符类型
- 类型转换
- 类型推断
- SIMD类型
下一章,我们将学习字符串类型。
最后更新:2026-03-27