Zig从XX到放弃(2)数值类型
zig中的数值
整数
zig中有很丰富的整数类型。除isize和usize之外,这些类型的位宽都是固定的。isize和usize的位宽取决于编译器的目标架构。在32位系统上,isize和usize都是32位的。在64位系统上,isize和usize都是64位的。
类型 | 位宽 | C类型 |
---|---|---|
i8 | 8 | int8_t |
u8 | 8 | uinit8_t |
i16 | 16 | int16_t |
u16 | 16 | uint16_t |
i32 | 32 | int32_t |
u32 | 32 | uint32_t |
i64 | 64 | int64_t |
u64 | 64 | uint64_t |
i128 | 128 | __int128 |
u128 | 128 | unsigned __int128 |
isize | 32/64 | intptr_t |
usize | 32/64 | uintptr_t |
c_short | 16 | short |
c_ushort | 16 | unsigned short |
c_int | 32 | int |
c_uint | 32 | unsigned int |
c_long | 64 | long |
c_ulong | 64 | unsigned long |
c_longlong | 64 | long long |
c_ulonglong | 64 | unsigned long long |
值得注意的是,zig中没有char类型。zig使用UTF-8编码,所以字符是一个u8类型的值。字符串在下面的字符串部分中讨论。
用下面的代码,可以打印出zig中所有整数类型的最小值、最大值、大小和位宽:
const print = std.debug.print;
const std = @import("std");
const minInt = std.math.minInt;
const maxInt = std.math.maxInt;
pub fn main() void {
// integers
const integer_types = [_]type{ i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize, c_short, c_ushort, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong };
print("|{s:>12}|{s:>40}|{s:>40}|{s:>4}|{s:>4}|\n", .{ "type", "min", "max", "size", "bits" });
print("|{s:>12}|{s:>40}|{s:>40}|{s:>4}|{s:>4}|\n", .{ "-" ** 12, "-" ** 40, "-" ** 40, "-" ** 4, "-" ** 4 });
inline for (integer_types) |int_type| {
print("|{s:>12}|{d:>40}|{d:>40}|{d:>4}|{d:>4}|\n", .{ @typeName(int_type), minInt(int_type), maxInt(int_type), @sizeOf(int_type), @bitSizeOf(int_type) });
}
}
得到的结果如下:
type | min | max | size | bits |
---|---|---|---|---|
i8 | -128 | 127 | 1 | 8 |
u8 | 0 | 255 | 1 | 8 |
i16 | -32768 | 32767 | 2 | 16 |
u16 | 0 | 65535 | 2 | 16 |
i32 | -2147483648 | 2147483647 | 4 | 32 |
u32 | 0 | 4294967295 | 4 | 32 |
i64 | -9223372036854775808 | 9223372036854775807 | 8 | 64 |
u64 | 0 | 18446744073709551615 | 8 | 64 |
i128 | -170141183460469231731687303715884105728 | 170141183460469231731687303715884105727 | 16 | 128 |
u128 | 0 | 340282366920938463463374607431768211455 | 16 | 128 |
isize | -9223372036854775808 | 9223372036854775807 | 8 | 64 |
usize | 0 | 18446744073709551615 | 8 | 64 |
c_short | -32768 | 32767 | 2 | 16 |
c_ushort | 0 | 65535 | 2 | 16 |
c_int | -2147483648 | 2147483647 | 4 | 32 |
c_uint | 0 | 4294967295 | 4 | 32 |
c_long | -2147483648 | 2147483647 | 4 | 32 |
c_ulong | 0 | 4294967295 | 4 | 32 |
c_longlong | -9223372036854775808 | 9223372036854775807 | 8 | 64 |
c_ulonglong | 0 | 18446744073709551615 | 8 | 64 |
浮点数
zig中有数种浮点数类型,f16,f32,f64,f128,c_longdouble等。
同样,可以用程序输出他们的特性。
const minFloat = std.math.floatMin;
const maxFloat = std.math.floatMax;
//………
// floats
const float_types = [_]type{ f16, f32, f64, f128, c_longdouble };
print("|{s:>12}|{s:>40}|{s:>40}|{s:>40}|{s:>4}|{s:>4}|\n", .{ "type", "min", "max", "eps", "size", "bits" });
print("|{s:>12}|{s:>40}|{s:>40}|{s:>40}|{s:>4}|{s:>4}|\n", .{ "-" ** 12, "-" ** 40, "-" ** 40, "-" ** 40, "-" ** 4, "-" ** 4 });
inline for (float_types) |float_type| {
print("|{s:>12}|{:>40}|{:>40}|{:>40}|{d:>4}|{d:>4}|\n", .{ @typeName(float_type), minFloat(float_type), maxFloat(float_type), espFloat(float_type), @sizeOf(float_type), @bitSizeOf(float_type) });
}
结果如下:
type | min | max | eps | size | bits |
---|---|---|---|---|---|
f16 | 6.103515625e-05 | 6.5504e+04 | 9.765625e-04 | 2 | 16 |
f32 | 1.17549435e-38 | 3.40282346e+38 | 1.19209289e-07 | 4 | 32 |
f64 | 2.2250738585072014e-308 | 1.7976931348623157e+308 | 2.220446049250313e-16 | 8 | 64 |
f128 | 0.0e+00 | inf | 1.925929944387236e-34 | 16 | 128 |
c_longdouble | 0.0e+00 | inf | 1.0842021724855045e-19 | 16 | 80 |
结论
- Zig中的数值很有一致性。
- 名称也更加清晰。
- 为了保持与C的交互,定义了相应的数值类型。
- 提供了操作类型本身的函数,比如@typeName,@sizeOf,@bitSizeOf等。