Zig从XX到放弃(2)数值类型

zig中的数值

整数

zig中有很丰富的整数类型。除isize和usize之外,这些类型的位宽都是固定的。isize和usize的位宽取决于编译器的目标架构。在32位系统上,isize和usize都是32位的。在64位系统上,isize和usize都是64位的。

类型位宽C类型
i88int8_t
u88uinit8_t
i1616int16_t
u1616uint16_t
i3232int32_t
u3232uint32_t
i6464int64_t
u6464uint64_t
i128128__int128
u128128unsigned __int128
isize32/64intptr_t
usize32/64uintptr_t
c_short16short
c_ushort16unsigned short
c_int32int
c_uint32unsigned int
c_long64long
c_ulong64unsigned long
c_longlong64long long
c_ulonglong64unsigned 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) });
    }
}

得到的结果如下:

typeminmaxsizebits
i8-12812718
u8025518
i16-3276832767216
u16065535216
i32-21474836482147483647432
u3204294967295432
i64-92233720368547758089223372036854775807864
u64018446744073709551615864
i128-17014118346046923173168730371588410572817014118346046923173168730371588410572716128
u128034028236692093846346337460743176821145516128
isize-92233720368547758089223372036854775807864
usize018446744073709551615864
c_short-3276832767216
c_ushort065535216
c_int-21474836482147483647432
c_uint04294967295432
c_long-21474836482147483647432
c_ulong04294967295432
c_longlong-92233720368547758089223372036854775807864
c_ulonglong018446744073709551615864

浮点数

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) });
}

结果如下:

typeminmaxepssizebits
f166.103515625e-056.5504e+049.765625e-04216
f321.17549435e-383.40282346e+381.19209289e-07432
f642.2250738585072014e-3081.7976931348623157e+3082.220446049250313e-16864
f1280.0e+00inf1.925929944387236e-3416128
c_longdouble0.0e+00inf1.0842021724855045e-191680

结论

  1. Zig中的数值很有一致性。
  2. 名称也更加清晰。
  3. 为了保持与C的交互,定义了相应的数值类型。
  4. 提供了操作类型本身的函数,比如@typeName,@sizeOf,@bitSizeOf等。
posted @ 2023-04-24 20:56  大福是小强  阅读(22)  评论(0编辑  收藏  举报  来源